Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <stdlib.h>
- using namespace std;
- struct Li
- {
- int value;
- Li* nextitem;
- };
- typedef struct Li Listitem;
- struct iL
- {
- int nofitems;
- Listitem* firstitem;
- };
- typedef struct iL intList;
- void additemtoback(intList* i, Listitem* l);
- Listitem* createnewlistitem(int x)
- {
- //Listitem* newitem = (Listitem*)new(sizeof(Listitem));
- Listitem* newitem = new Listitem[1];
- if (newitem == NULL)
- {
- cout<<"Error";
- exit(-1);
- }
- newitem->value=x;
- newitem->nextitem=NULL;
- return newitem;
- }
- intList* createnewlist()
- {
- //intList* newlist=new(sizeof(intList));
- intList* newlist = new intList[1];
- if (newlist == NULL)
- {
- cout<<"ERror #2";
- exit(-1);
- }
- newlist->nofitems=0;
- newlist->firstitem=NULL;
- return newlist;
- }
- void additemtofront(intList* i, Listitem* l)
- {
- if (i->nofitems==0)
- {
- i->firstitem=l;
- i->nofitems=1;
- }
- else
- {
- l->nextitem=i->firstitem;
- i->firstitem=l;
- i->nofitems++;
- }
- }
- int addsum(intList * i)
- {
- int sum = 0;
- if(i->nofitems==0)
- {
- return sum;
- }
- Listitem * currentitem = i->firstitem;
- while(currentitem->nextitem!=NULL)
- {
- sum+=currentitem->value;
- currentitem = currentitem->nextitem;
- }
- sum+=currentitem->value;
- return sum;
- }
- void deletefromfront(intList* i)
- {
- if(i->nofitems==0)
- {
- cout<<"Stop trying to delete"<<endl;
- return;
- }
- if(i->nofitems==1)
- {
- Listitem* onlyitem=i->firstitem;
- delete(onlyitem);
- onlyitem=NULL;
- i->firstitem=NULL;
- i->nofitems=0;
- }
- else
- {
- Listitem* firstitem=i->firstitem;
- i->firstitem=firstitem->nextitem;
- delete(firstitem);
- firstitem=NULL;
- i->nofitems--;
- }
- }
- void deletefromback(intList* i)
- {
- if(i->nofitems==0)
- {
- cout<<"Stop trying to delete"<<endl;
- return;
- }
- if(i->nofitems==1)
- {
- Listitem* onlyitem=i->firstitem;
- delete(onlyitem);
- onlyitem=NULL;
- i->firstitem=NULL;
- i->nofitems=0;
- }
- else
- {
- Listitem* currentitem=i->firstitem;
- Listitem* seconditem=currentitem->nextitem;
- while(seconditem->nextitem!=NULL)
- {
- currentitem=seconditem;
- seconditem=currentitem->nextitem;
- }
- delete(seconditem);
- seconditem=NULL;
- currentitem->nextitem=NULL;
- i->nofitems--;
- }
- }
- void deletelist(intList * i)
- {
- while(i->nofitems>0)
- {
- deletefromfront(i);
- }
- delete(i);
- i=NULL;
- }
- void additemtoback(intList* i, Listitem* l)
- {
- if (i->nofitems==0)
- {
- additemtofront(i,l);
- return;
- }
- Listitem* currentitem = i->firstitem;
- while(currentitem->nextitem!=NULL)
- currentitem=currentitem->nextitem;
- currentitem->nextitem=l;
- i->nofitems++;
- }
- void printList(intList* l)
- {
- if (l==NULL)
- {
- cout<<" NO LIST\n";
- return;
- }
- if (l->nofitems==0)
- {
- cout<<"No items to print!\n";
- return;
- }
- cout<<l->nofitems<<",";
- Listitem* currentItem = l->firstitem;
- while(currentItem->nextitem!=NULL)
- {
- cout<<currentItem->value<<"->";
- currentItem=currentItem->nextitem;
- }
- cout<<currentItem->value<<endl;
- }
- int main()
- {
- Listitem * newitem;
- int choice= 9;
- intList * mylist=NULL;
- int ivalue;
- while(choice!= 0)
- {
- cout<<"Choose what you want!: "<<endl;
- cout<<"\t 1. CreateList."<<endl;
- cout<<"\t 2. add item to front."<<endl;
- cout<<"\t 3. add item to back."<<endl;
- cout<<"\t 4. print list."<<endl;
- cout<<"\t 5. delete from back."<<endl;
- cout<<"\t 6. delete from front."<<endl;
- cout<<"\t 0. Press 0 to exit."<<endl;
- cout<<"\t 7. add all items in list: "<<endl;
- cin>>choice;
- switch(choice)
- {
- case 1:
- if(mylist!=NULL)
- deletelist(mylist);
- mylist=createnewlist();
- break;
- case 2:
- if(mylist==NULL)
- mylist=createnewlist();
- cout<<"Please enter a value: ";
- cin>>ivalue;
- newitem= createnewlistitem(ivalue);
- additemtofront(mylist, newitem);
- break;
- case 3:
- if(mylist==NULL)
- mylist=createnewlist();
- cout<<"Please enter a value to add to back: ";
- cin>>ivalue;
- newitem = createnewlistitem(ivalue);
- additemtoback(mylist,newitem);
- break;
- case 4:
- if(mylist==NULL)
- mylist=createnewlist();
- printList(mylist);
- break;
- case 5:
- if(mylist==NULL)
- mylist=createnewlist();
- deletefromback(mylist);
- break;
- case 6:
- if(mylist==NULL)
- mylist=createnewlist();
- deletefromfront(mylist);
- break;
- case 7:
- if(mylist==NULL)
- mylist=createnewlist();
- ivalue=addsum(mylist);
- cout<<"There are "<<ivalue<<" items in list"<<endl;
- break;
- }
- }
- if(mylist!=NULL)
- deletelist(mylist);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment