Advertisement
Guest User

problem with void arrange();

a guest
Apr 29th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct node
  4. {
  5.     int info;
  6.     struct node *link;
  7. } *start;
  8.  
  9. void createlist(int data);
  10. void display();
  11. void addtobeg(int data);
  12. void addtoend(int data);
  13. void addtopos(int data,int pos);
  14. void finddata();
  15. void arrange(){
  16.         struct node *ptr,*temp;
  17.         ptr = new node;
  18.         ptr = start;
  19.  
  20.         bool change = true;
  21.        
  22.         while(change==true)
  23.         {
  24.         ptr = start;   
  25.             while(ptr->link!=NULL){
  26.                 ptr = ptr->link;
  27.                 if((ptr->info)>(ptr->link->info)){
  28.                     temp = new node;
  29.                     temp->info = ptr->info;
  30.                     ptr->link->info = ptr->info;
  31.                     ptr->info = temp->info;
  32.                     delete temp;
  33.                     change = true;
  34.                    
  35.                 }
  36.                 else
  37.                     change = false;
  38.             }
  39.         }
  40.        
  41.         cout<<"\n\n--------------------Success---------------------\n\n";
  42.  
  43.  
  44. }
  45. void del();
  46. void newline();
  47.  
  48. void delatpos(int pos);
  49. int main()
  50. {
  51.     int choice, elem, i, num, addval,addvall, pos;
  52.     start = NULL;
  53.     restart:
  54.     while (1) {
  55.  
  56.         system("cls");
  57.         cout << "enter choice : \n1 - create link list \n2 - add at the beginning \n3 - add at the end \n4 - Add at certain position\n5 - Delete a value at position\n6 - delete at the end \n7 - find position of value\n8 - display \n9 - arrange values to ascending order\n20 - exit\n";
  58.         cout<<"Choice: ";cin >> choice;cout<<endl;
  59.  
  60.         switch (choice)
  61.         {
  62.         case 1: cout << "Create link list \n";
  63.             cout << "Enter the number of elements: \n";
  64.             cin >> num;
  65.             for (i = 0; i < num; i++)
  66.             {
  67.                 cout << "Enter the elements \n";
  68.                 cin >> elem;
  69.                 createlist(elem);
  70.             }
  71.             break;
  72.         case 2: if(start == NULL){
  73.             cout<<"No link list created\nProgram will now exit.";
  74.             system("pause");    exit(0);
  75.             }
  76.             else
  77.             cout << "Add the beginning" << endl;
  78.                 cin >> addval;
  79.                 addtobeg(addval);
  80.             break;
  81.         case 3:if(start == NULL){
  82.             cout<<"No link list created\nProgram will now exit.";
  83.             system("pause");    exit(0);
  84.             }
  85.             else  
  86.             cout << "Add to the end" << endl;
  87.             cin >> addval;
  88.             addtoend(addval);
  89.             break;
  90.         case 4:if(start == NULL){
  91.             cout<<"No link list created\nProgram will now exit.";
  92.             system("pause");    exit(0);
  93.             }
  94.             else
  95.             cout<<"Add at certain position\nNOTE: You can not add at the beginning. For that function please choose option [1] next time.\n";
  96.             cout<<"Input value to be added: ";
  97.             cin>>addvall;void newline();
  98.             cout<<"Input position: ";
  99.             cin>>pos;
  100.             cout<<"Value : \""<<addvall<<"\""<<" will be added to the position: ["<<pos<<"]\n";
  101.             system("pause"); addtopos(addvall,pos);
  102.             break;
  103.         case 5:if(start == NULL){
  104.             cout<<"No link list created\nProgram will now exit.";
  105.             system("pause");    exit(0);
  106.             }
  107.             else
  108.             display();
  109.             cout<<"\nDelete at position.\nInput position you want to delete: ";
  110.             cin>>pos;
  111.            
  112.             delatpos(pos);
  113.             break;
  114.         case 6:if(start == NULL){
  115.             cout<<"No link list created\nProgram will now exit.";
  116.             system("pause");
  117.                 exit(0);
  118.             }
  119.            
  120.             else  
  121.             del();
  122.             break;
  123.        
  124.         case 7:
  125.         finddata();
  126.         break;
  127.         case 8:
  128.                 if(start == NULL){
  129.             cout<<"No link list created\nPlease make one first.";
  130.             system("pause");
  131.                 goto restart;
  132.             }
  133.             else
  134.             display();
  135.             break;
  136.         case 9:
  137.             arrange();
  138.             break;
  139.            
  140.         case 20:
  141.             exit(0);
  142.        
  143.         default:
  144.             cout<<"Choice not found\n";system("pause");
  145.             goto restart;
  146.         }
  147. }
  148.     system("pause>0");
  149.     return 0;
  150. }
  151.  
  152. void createlist(int data)
  153. {
  154.     struct node *tail, *ptr;
  155.     ptr = new node;
  156.     ptr->info = data;
  157.     ptr->link = NULL;
  158.     if (start == NULL)
  159.         start = ptr;
  160.     else
  161.     {
  162.         tail = start;
  163.         while (tail->link != NULL)
  164.             tail = tail->link;
  165.         tail->link = ptr;
  166.     }
  167.  
  168. }
  169.  
  170. void addtobeg(int data)
  171. {
  172.     struct node *ptr;
  173.     ptr = new node;
  174.     ptr->info = data;
  175.     ptr->link = start;
  176.     start = ptr;
  177. }
  178. void display()
  179. {
  180.     struct node * tail;
  181.     tail = start;
  182.     while (tail != NULL)
  183.     {
  184.         cout << tail->info << ", ";
  185.         tail = tail->link;
  186.     }
  187.     system("pause>0");
  188. }
  189. void addtoend(int data)
  190. {
  191.     struct node *ptr, *m;
  192.     ptr = new node;
  193.     ptr->info = data;
  194.     m = start;
  195.     while (m->link != NULL)
  196.     {
  197.         m = m->link;
  198.     }
  199.     m->link = ptr;
  200.     ptr->link = NULL;
  201. }
  202. void del() {//at the end only
  203.     struct node *tail,*ptr,*tera;
  204.  
  205.      ptr = start;
  206.     while (ptr->link->link != NULL)
  207.     {
  208.        ptr = ptr->link;
  209.     }
  210.     delete ptr->link;
  211.     ptr->link = NULL;
  212.  
  213. }
  214.  
  215. void addtopos(int data,int pos){
  216.     struct node *ptr,*temp, *temp2;
  217.     ptr = new node;
  218.     ptr->info = data;
  219.     temp = start;
  220.     temp2 = start;
  221.    
  222.     for(int i = 0; i<pos-2;i++)
  223.     {
  224.         temp = temp->link;
  225.     }
  226.    
  227.     for(int i = 0;i<pos-1;i++)
  228.     {
  229.         temp2 = temp2->link;
  230.     }
  231.    
  232.     temp->link = ptr;
  233.     ptr->link = temp2;
  234.  
  235.    
  236. }
  237.  
  238. void delatpos(int pos){
  239.         struct node *temp, *ptr;
  240. if (pos != 1){
  241.     temp = start;
  242.     ptr = start;
  243.    
  244.     for(int i = 0; i<pos-2;i++)
  245.     {
  246.         temp = temp->link;
  247.    
  248.     }
  249.     for(int i = 0; i<pos;i++)
  250.     {
  251.         ptr = ptr->link;
  252.     }
  253.  
  254.    
  255.     delete temp->link;
  256.     temp->link = ptr;
  257. }
  258.  
  259. //1st value
  260.     else if (pos == 1){
  261.     ptr = start;
  262.     temp = ptr->link;
  263.     delete ptr;
  264.     ptr = temp;
  265.     start = ptr;
  266. }  
  267. }
  268.  
  269. void finddata(){
  270.     cout<<"Enter data you want to find: ";
  271.     int i = 1, j;
  272. //  d = &j;
  273.     node *ptr, *d, *y;
  274.     y = new node;
  275.     cin>>y->info;
  276. //  y->info = j;
  277.     ptr = start;
  278.     cout<<"The position[s] of the value you have entered are: ";
  279.     while (ptr->link!=NULL){
  280.         ptr = ptr->link;
  281.        
  282.         if(ptr->info == y->info){
  283.             cout<<"["<<i+1<<"]"<<" ";
  284.            
  285.         }i++;
  286.     }
  287.     cout<<"\n";
  288.     system("pause");
  289. }
  290.    
  291. void newline()
  292. {
  293.     char s;
  294.     do {
  295.         cin.get(s);
  296.     } while (s != '\n');
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement