Witam,
proszę o ocenę implementacji mojej listy jednokierunkowej. Wszystko w niej działa, ale no wiecie zawsze można coś usprawnić , przyspieszyć. Widziałem wiele implementacji takich list jednak ta jest dla mnie najbardziej zrozumiała. Proszę o sugestie.
Pozdrawiam
#include <iostream>
using namespace std;
struct user
{
int name;
user* w_next;
};
user* firstOfList = NULL;
void generateList(user*& w_list)
{
user* p = w_list;
for(int i=0; i<10; i++)
{
p = new user;
p->name = i;
p->w_next = w_list;
w_list = p;
}
}
void showList(user*& w_first)
{
user* p = w_first;
if(p)
{
while(p)
{
cout<<p->name<<"\n";
p = p->w_next;
}
}
else{
cout<<"Lista jest pusta!"<<"\n";
}
}
void addFront(user*& w_first)
{
user* p = w_first;
if(p)
{
p = new user;
p->name = 13;
p->w_next = w_first;
w_first = p;
}
}
void addBack(user*& w_first)
{
user* p = w_first;
user* n;
if(p)
{
while(p->w_next) p=p->w_next;
n = new user;
n->name = 99;
n->w_next = nullptr;
p->w_next = n;
}
else cout<<"Lista pusta! "<<"\n";
}
void deleteFront(user*& w_first)
{
user* p = w_first;
if(p)
{
w_first = p->w_next;
delete p;
}
else cout<<"List is empty !"<<"\n";
}
void deleteBack(user*& w_first)
{
user* p = w_first;
while(p->w_next->w_next) p=p->w_next;
if(p->w_next)
{
p->w_next = nullptr;
delete p;
}
}
void countElements(user*& w_first)
{
user* p = w_first;
int count = 1;
if(p)
{
while(p->w_next)
{
p=p->w_next;
count ++;
}
cout<<"List have: "<<count<<" elements"<<"\n";
}
else cout<<"Lista is empty! "<< "\n";
}
int main(int argc, const char * argv[]) {
int choice;
while(choice !=0)
{
cout<<"------MENU------"<<endl;
cout<<"1. Add new list"<<endl;
cout<<"2. Add front"<<endl;
cout<<"3. Add back"<<endl;
cout<<"4. Remove front"<<endl;
cout<<"5. Remove back"<<endl;
cout<<"6. Show list"<<endl;
cout<<"7. Count list elements"<<endl;
cout<<"Your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
generateList(firstOfList);
cout<<"Liste dodano"<<endl;
break;
}
case 2: addFront(firstOfList); break;
case 3: addBack(firstOfList); break;
case 4: deleteFront(firstOfList); break;
case 5: deleteBack(firstOfList); break;
case 6: showList(firstOfList); break;
case 7: countElements(firstOfList); break;
default: break;
}
}
return 0;
}