meissner61

data structures v1.1

May 29th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Li
  9. {
  10.     int value;
  11.     Li* nextitem;
  12.  
  13.  
  14.  
  15. };
  16.  
  17. typedef struct Li Listitem;
  18.  
  19. struct iL
  20. {
  21.     int nofitems;
  22.     Listitem* firstitem;
  23. };
  24.  
  25. typedef struct  iL intList;
  26.  
  27. void additemtoback(intList* i, Listitem* l);
  28.  
  29. Listitem* createnewlistitem(int x)
  30. {
  31.     //Listitem* newitem = (Listitem*)new(sizeof(Listitem));
  32.     Listitem* newitem = new Listitem[1];
  33.     if (newitem == NULL)
  34.     {
  35.         cout<<"Error";
  36.  
  37.         exit(-1);
  38.     }
  39.  
  40.     newitem->value=x;
  41.  
  42.     newitem->nextitem=NULL;
  43.  
  44.     return newitem;
  45. }
  46.  
  47. intList* createnewlist()
  48. {
  49.     //intList* newlist=new(sizeof(intList));
  50.     intList* newlist = new intList[1];
  51.     if (newlist == NULL)
  52.     {
  53.         cout<<"ERror #2";
  54.  
  55.         exit(-1);
  56.     }
  57.     newlist->nofitems=0;
  58.     newlist->firstitem=NULL;
  59.  
  60.     return newlist;
  61.  
  62. }
  63.  
  64. void additemtofront(intList* i, Listitem* l)
  65. {
  66.     if (i->nofitems==0)
  67.     {
  68.         i->firstitem=l;
  69.         i->nofitems=1;
  70.     }
  71.     else
  72.     {
  73.         l->nextitem=i->firstitem;
  74.         i->firstitem=l;
  75.         i->nofitems++;
  76.     }
  77. }
  78.  
  79. int addsum(intList * i)
  80. {
  81.     int sum = 0;
  82.  
  83.     if(i->nofitems==0)
  84.     {
  85.         return sum;
  86.     }
  87.    
  88.     Listitem * currentitem = i->firstitem;
  89.    
  90.     while(currentitem->nextitem!=NULL)
  91.     {
  92.         sum+=currentitem->value;
  93.  
  94.         currentitem = currentitem->nextitem;
  95.     }
  96.  
  97.     sum+=currentitem->value;
  98.  
  99.     return sum;
  100.  
  101. }
  102.  
  103. void deletefromfront(intList* i)
  104. {
  105.         if(i->nofitems==0)
  106.     {
  107.         cout<<"Stop trying to delete"<<endl;
  108.         return;
  109.     }
  110.  
  111.         if(i->nofitems==1)
  112.     {
  113.         Listitem* onlyitem=i->firstitem;
  114.  
  115.         delete(onlyitem);
  116.  
  117.         onlyitem=NULL;
  118.  
  119.         i->firstitem=NULL;
  120.  
  121.         i->nofitems=0;
  122.     }
  123.  
  124.             else
  125.     {
  126.         Listitem* firstitem=i->firstitem;
  127.  
  128.         i->firstitem=firstitem->nextitem;
  129.  
  130.         delete(firstitem);
  131.  
  132.         firstitem=NULL;
  133.  
  134.         i->nofitems--;
  135.     }
  136. }
  137.  
  138. void deletefromback(intList* i)
  139. {
  140.     if(i->nofitems==0)
  141.     {
  142.         cout<<"Stop trying to delete"<<endl;
  143.         return;
  144.     }
  145.  
  146.     if(i->nofitems==1)
  147.     {
  148.         Listitem* onlyitem=i->firstitem;
  149.  
  150.         delete(onlyitem);
  151.  
  152.         onlyitem=NULL;
  153.  
  154.         i->firstitem=NULL;
  155.  
  156.         i->nofitems=0;
  157.     }
  158.  
  159.     else
  160.     {
  161.         Listitem* currentitem=i->firstitem;
  162.  
  163.         Listitem* seconditem=currentitem->nextitem;
  164.  
  165.         while(seconditem->nextitem!=NULL)
  166.         {
  167.             currentitem=seconditem;
  168.  
  169.             seconditem=currentitem->nextitem;
  170.         }
  171.  
  172.         delete(seconditem);
  173.  
  174.         seconditem=NULL;
  175.  
  176.         currentitem->nextitem=NULL;
  177.  
  178.         i->nofitems--;
  179.     }
  180.  
  181.  
  182. }
  183.  
  184. void deletelist(intList * i)
  185. {
  186.     while(i->nofitems>0)
  187.     {
  188.         deletefromfront(i);
  189.  
  190.     }
  191.  
  192.     delete(i);
  193.  
  194.     i=NULL;
  195. }
  196.  
  197.  
  198.  
  199. void additemtoback(intList* i, Listitem* l)
  200. {
  201.     if (i->nofitems==0)
  202.     {
  203.         additemtofront(i,l);
  204.  
  205.         return;
  206.     }
  207.  
  208.     Listitem* currentitem = i->firstitem;
  209.     while(currentitem->nextitem!=NULL)
  210.         currentitem=currentitem->nextitem;
  211.  
  212.     currentitem->nextitem=l;
  213.  
  214.     i->nofitems++;
  215. }
  216.  
  217. void printList(intList* l)
  218. {
  219.     if (l==NULL)
  220.     {
  221.         cout<<" NO LIST\n";
  222.         return;
  223.     }
  224.    
  225.     if (l->nofitems==0)
  226.     {
  227.         cout<<"No items to print!\n";
  228.         return;
  229.     }
  230.  
  231.     cout<<l->nofitems<<",";
  232.    
  233.  
  234.     Listitem* currentItem = l->firstitem;
  235.  
  236.     while(currentItem->nextitem!=NULL)
  237.     {
  238.         cout<<currentItem->value<<"->";
  239.         currentItem=currentItem->nextitem;
  240.     }
  241.  
  242.     cout<<currentItem->value<<endl;
  243.  
  244. }
  245.  
  246. int main()
  247. {
  248.     Listitem * newitem;
  249.     int choice= 9;
  250.     intList * mylist=NULL;
  251.     int ivalue;
  252.  
  253.  
  254.     while(choice!= 0)
  255.     {
  256.  
  257.         cout<<"Choose what you want!: "<<endl;
  258.         cout<<"\t 1. CreateList."<<endl;
  259.         cout<<"\t 2. add item to front."<<endl;
  260.         cout<<"\t 3. add item to back."<<endl;
  261.         cout<<"\t 4. print list."<<endl;
  262.         cout<<"\t 5. delete from back."<<endl;
  263.         cout<<"\t 6. delete from front."<<endl;
  264.         cout<<"\t 0. Press 0 to exit."<<endl;
  265.         cout<<"\t 7. add all items in list: "<<endl;
  266.        
  267.         cin>>choice;
  268.  
  269.         switch(choice)
  270.         {
  271.             case 1:
  272.  
  273.                 if(mylist!=NULL)
  274.                     deletelist(mylist);
  275.  
  276.                 mylist=createnewlist();
  277.                 break;
  278.  
  279.             case 2:
  280.                 if(mylist==NULL)
  281.                     mylist=createnewlist();
  282.                 cout<<"Please enter a value: ";
  283.                 cin>>ivalue;
  284.                 newitem= createnewlistitem(ivalue);
  285.                 additemtofront(mylist, newitem);
  286.                 break;
  287.             case 3:
  288.                 if(mylist==NULL)
  289.                     mylist=createnewlist();
  290.                 cout<<"Please enter a value to add to back: ";
  291.                 cin>>ivalue;
  292.                
  293.                 newitem = createnewlistitem(ivalue);
  294.                 additemtoback(mylist,newitem);
  295.                 break;
  296.             case 4:
  297.                 if(mylist==NULL)
  298.                     mylist=createnewlist();
  299.                 printList(mylist);
  300.                 break;
  301.             case 5:
  302.                 if(mylist==NULL)
  303.                     mylist=createnewlist();
  304.                 deletefromback(mylist);
  305.                 break;
  306.             case 6:
  307.                 if(mylist==NULL)
  308.                     mylist=createnewlist();
  309.                 deletefromfront(mylist);
  310.                 break;
  311.             case 7:
  312.                 if(mylist==NULL)
  313.                     mylist=createnewlist();
  314.                 ivalue=addsum(mylist);
  315.                 cout<<"There are "<<ivalue<<" items in list"<<endl;
  316.                 break;
  317.  
  318.  
  319.         }
  320.  
  321.     }
  322.  
  323.     if(mylist!=NULL)
  324.         deletelist(mylist);
  325.    
  326.  
  327.     return 0;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment