944,038 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2300
  • C++ RSS
Jul 8th, 2005
0

Linked stack modification issue

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yaan is offline Offline
8 posts
since Jun 2005
Jul 8th, 2005
0

Re: Linked stack modification issue

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:
C++ Syntax (Toggle Plain Text)
  1. case 2: obj.pop();
  2. break;
  3. case 3: obj.display();
  4. break;
To this:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 8th, 2005
0

Re: Linked stack modification issue

More specifically, how can I get rid of display () and still "display" poped entries after each pop.

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yaan is offline Offline
8 posts
since Jun 2005
Jul 8th, 2005
0

Re: Linked stack modification issue

>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().
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ help with Julian day program revised
Next Thread in C++ Forum Timeline: using stringstream for tokenizing a string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC