Linked stack modification issue

Reply

Join Date: Jun 2005
Posts: 8
Reputation: yaan is an unknown quantity at this point 
Solved Threads: 0
yaan yaan is offline Offline
Newbie Poster

Linked stack modification issue

 
0
  #1
Jul 8th, 2005
I need to have my code modified, but am unsure how to write the code to do it. After the ID and name are poped from the stack, I need the program to automatically display the list created i.e. I don't want to use display as an option.

I think I need to have main function loop back after pop and display are done. How can I do this?

Here is the code:
  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include <iostream.h>
  4.  
  5. class link_stack
  6.  
  7. {
  8.  
  9. struct node
  10.  
  11. {
  12.  
  13. int id;
  14.  
  15. char name[10];
  16.  
  17. node *next;
  18.  
  19. };
  20.  
  21. node *top,*x,*ptr;
  22.  
  23. public:
  24.  
  25. link_stack()
  26.  
  27. {
  28.  
  29. top=x=ptr=NULL;
  30.  
  31. }
  32.  
  33. void push()
  34.  
  35. {
  36.  
  37. x=new node;
  38.  
  39. cout << "Enter an ID number and name: ";
  40.  
  41. cin >> x->id >> x->name;
  42.  
  43.  
  44. x->next=top;
  45.  
  46. top=x;
  47.  
  48. }
  49.  
  50. void pop()
  51.  
  52. {
  53.  
  54. if(top==NULL)
  55.  
  56. cout<<"\nStack is Empty";
  57.  
  58. else
  59.  
  60. {
  61.  
  62. x=top;
  63.  
  64. top=top->next;
  65.  
  66. delete x;
  67.  
  68. }
  69.  
  70. }
  71.  
  72. void display()
  73.  
  74. {
  75.  
  76. ptr=top;
  77.  
  78. while(ptr!=NULL)
  79.  
  80. {
  81.  
  82. cout << "\nName: "<< ptr->name;
  83.  
  84. cout<<"\nID number: "<<ptr->id;
  85.  
  86. ptr=ptr->next;
  87.  
  88. }
  89.  
  90. }
  91.  
  92. };
  93.  
  94. void main()
  95.  
  96. {
  97.  
  98. link_stack obj;
  99.  
  100. int choice;
  101.  
  102. do
  103.  
  104. {
  105.  
  106. cout << "\n ----------MENU---------- \n";
  107.  
  108. cout << "1.Push\n"
  109.  
  110. << "2.Pop\n"
  111.  
  112. << "3.Display\n"
  113.  
  114. << "4.Exit";
  115.  
  116. cout << "\nEnter your choice: ";
  117.  
  118. cin>>choice;
  119.  
  120. switch(choice)
  121.  
  122. {
  123.  
  124. case 1: obj.push();
  125.  
  126. break;
  127.  
  128. case 2: obj.pop();
  129.  
  130. break;
  131.  
  132. case 3: obj.display();
  133.  
  134. break;
  135.  
  136. case 4: cout << endl;
  137.  
  138. }
  139.  
  140. }
  141.  
  142. while (choice!=4);
  143.  
  144. getch();
  145.  
  146. }
Code tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Linked stack modification issue

 
0
  #2
Jul 8th, 2005
Use code tags for any future code. I've added them for you this time, but next time I may just feel the need to delete your post if you fail to do it properly.

>After the ID and name are poped from the stack, I need the program to
>automatically display the list created
Change this:
  1. case 2: obj.pop();
  2. break;
  3. case 3: obj.display();
  4. break;
To this:
  1. case 2: obj.pop();
  2. case 3: obj.display();
  3. break;
By default, cases will fall through to the next case if you don't use a break statement. So if you want to display the stack after every pop, simply omit the break on the pop case.

If that's not what you want, be more specific.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: yaan is an unknown quantity at this point 
Solved Threads: 0
yaan yaan is offline Offline
Newbie Poster

Re: Linked stack modification issue

 
0
  #3
Jul 8th, 2005
More specifically, how can I get rid of display () and still "display" poped entries after each pop.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Linked stack modification issue

 
0
  #4
Jul 8th, 2005
>More specifically, how can I get rid of display () and still "display" poped entries after each pop.
That's easy: you don't. Since the data members of your class are private (as they should be), the only way to display them is with a public member function or a friend function. Either way you would have the equivalent of link_stack::display().
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC