linked stack question

Please support our C++ advertiser: Intel Parallel Studio Home
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 question

 
0
  #1
Jul 26th, 2005
I'm confused as to why my program is turning out errors when I try to compile the code. Please help.

  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include<iostream.h>
  4. #include<string.h>
  5.  
  6. class link_stack
  7.  
  8. {
  9.  
  10. struct node
  11.  
  12. {
  13.  
  14. int id;
  15.  
  16. char name[10];
  17.  
  18. node *next;
  19.  
  20. };
  21.  
  22. node *top,*x,*ptr;
  23.  
  24. public:
  25.  
  26. link_stack()
  27.  
  28. {
  29.  
  30. top=x=ptr=NULL;
  31.  
  32. }
  33.  
  34. void push()
  35.  
  36. {
  37.  
  38. x=new node;
  39.  
  40. cout << "Enter an ID number and name: ";
  41.  
  42. cin >> x->id >> x->name;
  43.  
  44.  
  45. x->next=top;
  46.  
  47. top=x;
  48.  
  49. }
  50.  
  51. int pop(char n[])
  52.  
  53. { int result;
  54.  
  55. if(top==NULL)
  56.  
  57. cout<<"\nStack is Empty";
  58.  
  59. else
  60.  
  61. { result = top->id;
  62.  
  63. strcpy(n, top->name);
  64.  
  65. x=top;
  66.  
  67. top=top->next;
  68.  
  69. delete x;
  70.  
  71. return result;
  72.  
  73. }
  74.  
  75. }
  76.  
  77. void empty()
  78. {
  79.  
  80. char name[10];
  81.  
  82. while(!obj.empty())
  83.  
  84. {
  85.  
  86. cout << pop(name) << ": " << name << endl;
  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.Exit";
  113.  
  114. cout << "\nEnter your choice: ";
  115.  
  116. cin>>choice;
  117.  
  118. switch(choice)
  119.  
  120. {
  121.  
  122. case 1: obj.push();
  123.  
  124. break;
  125.  
  126. case 2: obj.pop();
  127.  
  128. obj.empty();
  129.  
  130. break;
  131.  
  132. case 3: cout << endl;
  133.  
  134. }
  135.  
  136. }
  137.  
  138. while (choice!=3);
  139.  
  140. getch();
  141.  
  142. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: linked stack question

 
0
  #2
Jul 27th, 2005
Here we go.
  1. #include<conio.h> //Non standard header do you really "need" it?
  2.  
  3. //This is the way the headers are no .h's
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<string>
  7. //Got to put it in the std namespace
  8. using namespace std;
  9. //I would recommend some minor style changes.
  10. class link_stack
  11. {
  12. struct node
  13. {
  14. int id;
  15. char name[10];
  16. node *next;
  17. };
  18.  
  19. node *top,*x,*ptr;
  20.  
  21. public:
  22. link_stack()
  23. {
  24. top = x = ptr =NULL;
  25. }
  26. void push()
  27. {
  28. x = new node;
  29.  
  30. cout << "Enter an ID number and name: ";
  31. cin >> x->id >> x->name;
  32.  
  33. x->next = top;
  34. top = x;
  35. }
  36. int pop(char n[])
  37. {
  38. int result;
  39. if(top==NULL)
  40. {
  41. cout<<"\nStack is Empty";
  42. }
  43. else
  44. {
  45. result = top->id;
  46. strcpy(n, top->name);
  47. x = top;
  48. top = top->next;
  49. delete x;
  50. return result;
  51. }
  52.  
  53. }
  54. void empty()//What is this function really doing?
  55. {
  56. char name[10];
  57.  
  58. //while(!obj.empty()) Where is obj declared?
  59. //{
  60. // cout << pop(name) << ": " << name << endl;
  61. //}
  62.  
  63. }
  64.  
  65. };
  66.  
  67. int main()//It is int main not void main!!!!!!!!!!!!!!!!!!!!!
  68. {
  69. link_stack obj;
  70. int choice;
  71. do
  72. {
  73. cout << "\n ----------MENU---------- \n";
  74. cout << "1.Push\n"
  75. << "2.Pop\n"
  76. << "3.Exit";
  77. cout << "\nEnter your choice: ";
  78. cin>>choice;
  79.  
  80. switch(choice)
  81. {
  82. case 1:
  83. obj.push();
  84. break;
  85. case 2:
  86. //obj.pop(); Is this how you defined pop?
  87. obj.empty();
  88. break;
  89. case 3:
  90. cout << endl;
  91. break;
  92. }
  93.  
  94. }
  95. while (choice != 3);
  96.  
  97. getch(); //Why not cin.get()?
  98. //cin.ignore();
  99. //cin.get();
  100. return 0; //Need to return a value from main.
  101. }
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
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 question

 
0
  #3
Jul 27th, 2005
Thanks for the feedback. I was trying to create a function empty() to display the entire list of entries after a pop, and if no entries then not to display anything.

I think I'm not doing it right?! any suggestions??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
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: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: linked stack question

 
0
  #4
Jul 27th, 2005
>I was trying to create a function empty() to display the
>entire list of entries after a pop
That's a poor name for the function. People expect a function with the name empty(), to tell them whether or not the stack is empty, not print the contents of the stack. But you can do it like this:
  1. void link_stack::print_all()
  2. {
  3. for ( node *p = top; p != 0; p = p->next )
  4. cout<< p->id <<": "<< p->name <<'\n';
  5. }
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:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC