meissner61

data structures - homework

Jun 1st, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.03 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 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 addonetoall(intList* i)
  79. {
  80.     if(i->nofitems==0)
  81.     {
  82.         cout<<"nothing to add";
  83.     }
  84.  
  85.     Listitem* currentitem = i->firstitem;
  86.  
  87.     while(currentitem->nextitem != NULL)
  88.     {
  89.         currentitem->value+=1;
  90.         currentitem = currentitem->nextitem;
  91.     }
  92.  
  93.     currentitem->value+=1;
  94. }
  95.  
  96. int swap(intList* i, int x, int y)
  97. {
  98.     int replaced = 0;
  99.  
  100.     if(i->nofitems==0)
  101.     {
  102.         cout<<"Nothing to replace!";
  103.     }
  104.  
  105.     Listitem* currentitem = i->firstitem;
  106.  
  107.     while(currentitem->nextitem != NULL)
  108.     {
  109.         if(currentitem->value == x)
  110.         {
  111.             currentitem->value=y;
  112.             currentitem=currentitem->nextitem;
  113.             replaced++;
  114.         }
  115.  
  116.         else
  117.         {
  118.             currentitem=currentitem->nextitem;
  119.         }
  120.     }
  121.  
  122.     if(currentitem->value == x)
  123.     {
  124.         currentitem->value=y;
  125.         replaced++;
  126.     }
  127.  
  128.     return replaced;
  129. }
  130.  
  131. int addsum(intList* i)
  132. {
  133.     int sum = 0;
  134.  
  135.     if(i->nofitems==0)
  136.     {
  137.         return sum;
  138.     }
  139.    
  140.     Listitem* currentitem = i->firstitem;
  141.    
  142.     while(currentitem->nextitem!=NULL)
  143.     {
  144.         sum+=currentitem->value;
  145.  
  146.         currentitem = currentitem->nextitem;
  147.     }
  148.  
  149.     sum+=currentitem->value;
  150.  
  151.     return sum;
  152.  
  153. }
  154.  
  155. void deletefromfront(intList* i)
  156. {
  157.         if(i->nofitems==0)
  158.     {
  159.         cout<<"Stop trying to delete"<<endl;
  160.         return;
  161.     }
  162.  
  163.         if(i->nofitems==1)
  164.     {
  165.         Listitem* onlyitem=i->firstitem;
  166.  
  167.         delete(onlyitem);
  168.  
  169.         onlyitem=NULL;
  170.  
  171.         i->firstitem=NULL;
  172.  
  173.         i->nofitems=0;
  174.     }
  175.  
  176.             else
  177.     {
  178.         Listitem* firstitem=i->firstitem;
  179.  
  180.         i->firstitem=firstitem->nextitem;
  181.  
  182.         delete(firstitem);
  183.  
  184.         firstitem=NULL;
  185.  
  186.         i->nofitems--;
  187.     }
  188. }
  189.  
  190. void deletefromback(intList* i)
  191. {
  192.     if(i->nofitems==0)
  193.     {
  194.         cout<<"Stop trying to delete"<<endl;
  195.         return;
  196.     }
  197.  
  198.     if(i->nofitems==1)
  199.     {
  200.         Listitem* onlyitem=i->firstitem;
  201.  
  202.         delete(onlyitem);
  203.  
  204.         onlyitem=NULL;
  205.  
  206.         i->firstitem=NULL;
  207.  
  208.         i->nofitems=0;
  209.     }
  210.  
  211.     else
  212.     {
  213.         Listitem* currentitem=i->firstitem;
  214.  
  215.         Listitem* seconditem=currentitem->nextitem;
  216.  
  217.         while(seconditem->nextitem!=NULL)
  218.         {
  219.             currentitem=seconditem;
  220.  
  221.             seconditem=currentitem->nextitem;
  222.         }
  223.  
  224.         delete(seconditem);
  225.  
  226.         seconditem=NULL;
  227.  
  228.         currentitem->nextitem=NULL;
  229.  
  230.         i->nofitems--;
  231.     }
  232.  
  233.  
  234. }
  235.  
  236. void deletelist(intList * i)
  237. {
  238.     while(i->nofitems>0)
  239.     {
  240.         deletefromfront(i);
  241.  
  242.     }
  243.  
  244.     delete(i);
  245.  
  246.     i=NULL;
  247. }
  248.  
  249.  
  250.  
  251. void additemtoback(intList* i, Listitem* l)
  252. {
  253.     if (i->nofitems==0)
  254.     {
  255.         additemtofront(i,l);
  256.  
  257.         return;
  258.     }
  259.  
  260.     Listitem* currentitem = i->firstitem;
  261.     while(currentitem->nextitem!=NULL)
  262.         currentitem=currentitem->nextitem;
  263.  
  264.     currentitem->nextitem=l;
  265.  
  266.     i->nofitems++;
  267. }
  268.  
  269. void printList(intList* l)
  270. {
  271.     if (l==NULL)
  272.     {
  273.         cout<<" NO LIST\n";
  274.         return;
  275.     }
  276.    
  277.     if (l->nofitems==0)
  278.     {
  279.         cout<<"No items to print!\n";
  280.         return;
  281.     }
  282.  
  283.     cout<<l->nofitems<<",";
  284.    
  285.  
  286.     Listitem* currentItem = l->firstitem;
  287.  
  288.     while(currentItem->nextitem!=NULL)
  289.     {
  290.         cout<<currentItem->value<<"->";
  291.         currentItem=currentItem->nextitem;
  292.     }
  293.  
  294.     cout<<currentItem->value<<endl;
  295.  
  296. }
  297.  
  298. int main()
  299. {
  300.     Listitem * newitem;
  301.     int choice= 9;
  302.     intList * mylist=NULL;
  303.     int ivalue;
  304.  
  305.     int x=999;
  306.     int y=666;
  307.  
  308.  
  309.  
  310.     while(choice!= 0)
  311.     {
  312.  
  313.         cout<<"Choose what you want!: "<<endl;
  314.         cout<<"\t 0. Press 0 to exit."<<endl;
  315.         cout<<"\t 1. CreateList."<<endl;
  316.         cout<<"\t 2. add item to front."<<endl;
  317.         cout<<"\t 3. add item to back."<<endl;
  318.         cout<<"\t 4. print list."<<endl;
  319.         cout<<"\t 5. delete from back."<<endl;
  320.         cout<<"\t 6. delete from front."<<endl;
  321.         cout<<"\t 7. add all items in list: "<<endl;
  322.         cout<<"\t 8. add 1 to all items."<<endl;
  323.         cout<<"\t 9. replace \"x\" with \"y\": "<<endl;
  324.        
  325.         cin>>choice;
  326.  
  327.         switch(choice)
  328.         {
  329.             case 1:
  330.  
  331.                 if(mylist!=NULL)
  332.                     deletelist(mylist);
  333.  
  334.                 mylist=createnewlist();
  335.                 break;
  336.  
  337.             case 2:
  338.                 if(mylist==NULL)
  339.                     mylist=createnewlist();
  340.                 cout<<"Please enter a value: ";
  341.                 cin>>ivalue;
  342.                 newitem= createnewlistitem(ivalue);
  343.                 additemtofront(mylist, newitem);
  344.                 break;
  345.             case 3:
  346.                 if(mylist==NULL)
  347.                     mylist=createnewlist();
  348.                 cout<<"Please enter a value to add to back: ";
  349.                 cin>>ivalue;
  350.                
  351.                 newitem = createnewlistitem(ivalue);
  352.                 additemtoback(mylist,newitem);
  353.                 break;
  354.             case 4:
  355.                 if(mylist==NULL)
  356.                     mylist=createnewlist();
  357.                 printList(mylist);
  358.                 break;
  359.             case 5:
  360.                 if(mylist==NULL)
  361.                     mylist=createnewlist();
  362.                 deletefromback(mylist);
  363.                 break;
  364.             case 6:
  365.                 if(mylist==NULL)
  366.                     mylist=createnewlist();
  367.                 deletefromfront(mylist);
  368.                 break;
  369.             case 7:
  370.                 if(mylist==NULL)
  371.                     mylist=createnewlist();
  372.                 ivalue=addsum(mylist);
  373.                 cout<<"There are "<<ivalue<<" items in list"<<endl;
  374.                 break;
  375.             case 8:
  376.                 if(mylist==NULL)
  377.                     cout<<"create a list first!";
  378.                 addonetoall(mylist);
  379.                 break;
  380.             case 9:
  381.                 cout<<"Enter the value you want to find and replace: ";
  382.                 cin>>x;
  383.  
  384.                 cout<<"Enter the value to replace it with: ";
  385.                 cin>>y;
  386.  
  387.                 swap(mylist,x,y);
  388.                 break;
  389.                
  390.  
  391.  
  392.         }
  393.  
  394.     }
  395.  
  396.     cout<<"Deleting lists...";
  397.     if(mylist!=NULL)
  398.         deletelist(mylist);
  399.    
  400.  
  401.     return 0;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment