meissner61

data structures

May 23rd, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. struct Li
  10. {
  11.     int value;
  12.     Li* nextitem;
  13.  
  14.  
  15.  
  16. };
  17.  
  18. typedef struct Li Listitem;
  19.  
  20. struct iL
  21. {
  22.     int nofitems;
  23.     Listitem* firstitem;
  24. };
  25.  
  26. typedef struct  iL intList;
  27.  
  28. Listitem* createnewlistitem(int x)
  29. {
  30.     //Listitem* newitem = (Listitem*)new(sizeof(Listitem));
  31.     Listitem* newitem = new Listitem[1];
  32.     if (newitem == NULL)
  33.     {
  34.         cout<<"Error";
  35.  
  36.         exit(-1);
  37.     }
  38.  
  39.     newitem->value=x;
  40.  
  41.     newitem->nextitem=NULL;
  42.  
  43.     return newitem;
  44. }
  45.  
  46. intList* createnewlist()
  47. {
  48.     //intList* newlist=new(sizeof(intList));
  49.     intList* newlist = new intList[1];
  50.     if (newlist == NULL)
  51.     {
  52.         cout<<"ERror #2";
  53.  
  54.         exit(-1);
  55.     }
  56.     newlist->nofitems=0;
  57.     newlist->firstitem=NULL;
  58.  
  59.     return newlist;
  60.  
  61. }
  62.  
  63. void additemtofront(intList* i, Listitem* l)
  64. {
  65.     if (i->nofitems==0)
  66.     {
  67.         i->firstitem=l;
  68.         i->nofitems=1;
  69.     }
  70.     else
  71.     {
  72.         l->nextitem=i->firstitem;
  73.         i->firstitem=l;
  74.         i->nofitems++;
  75.     }
  76. }
  77.  
  78. void deletefromback(intList* i)
  79. {
  80.     if(i->nofitems==0)
  81.     {
  82.         cout<<"Stop trying to delete"<<endl;
  83.         return;
  84.     }
  85.  
  86.     if(i->nofitems==1)
  87.     {
  88.         Listitem* onlyitem=i->firstitem;
  89.  
  90.         delete(onlyitem);
  91.  
  92.         onlyitem=NULL;
  93.  
  94.         i->firstitem=NULL;
  95.  
  96.         i->nofitems=0;
  97.     }
  98.  
  99.     else
  100.     {
  101.         Listitem* currentitem=i->firstitem;
  102.  
  103.         Listitem* seconditem=currentitem->nextitem;
  104.  
  105.         while(seconditem->nextitem!=NULL)
  106.         {
  107.             currentitem=seconditem;
  108.  
  109.             seconditem=currentitem->nextitem;
  110.         }
  111.  
  112.         delete(seconditem);
  113.  
  114.         seconditem=NULL;
  115.  
  116.         currentitem->nextitem=NULL;
  117.  
  118.         i->nofitems--;
  119.     }
  120.  
  121.  
  122. }
  123.  
  124. void additemtoback(intList* i, Listitem* l)
  125. {
  126.     if (i->nofitems==0)
  127.     {
  128.         additemtofront(i,l);
  129.  
  130.         return;
  131.     }
  132.  
  133.     Listitem* currentitem = i->firstitem;
  134.     while(currentitem->nextitem!=NULL)
  135.         currentitem=currentitem->nextitem;
  136.  
  137.     currentitem->nextitem=l;
  138.  
  139.     i->nofitems++;
  140. }
  141.  
  142. void printList(intList* l)
  143. {
  144.     if (l==NULL)
  145.     {
  146.         cout<<" NO LIST\n";
  147.         return;
  148.     }
  149.    
  150.     if (l->nofitems==0)
  151.     {
  152.         cout<<"No items to print!\n";
  153.         return;
  154.     }
  155.  
  156.     cout<<l->nofitems<<",";
  157.    
  158.  
  159.     Listitem* currentItem = l->firstitem;
  160.  
  161.     while(currentItem->nextitem!=NULL)
  162.     {
  163.         cout<<currentItem->value<<"->";
  164.         currentItem=currentItem->nextitem;
  165.     }
  166.  
  167.     cout<<currentItem->value<<endl;
  168.  
  169. }
  170.  
  171. int main()
  172. {
  173.     intList* mylist=createnewlist();
  174.  
  175.     printList(mylist);
  176.  
  177.     Listitem* myitem=createnewlistitem(13);
  178.  
  179.     additemtofront(mylist,myitem);
  180.  
  181.     printList(mylist);
  182.  
  183.     Listitem* myitemTwo=createnewlistitem(24);
  184.  
  185.     additemtofront(mylist,myitemTwo);
  186.  
  187.     printList(mylist);
  188.  
  189.     Listitem* myitemthree=createnewlistitem(10);
  190.  
  191.     additemtoback(mylist,myitemthree);
  192.  
  193.     printList(mylist);
  194.  
  195.     deletefromback(mylist);
  196.        
  197.     printList(mylist);
  198.  
  199.     int dummy=0;
  200.  
  201.     cin>>dummy;
  202.  
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment