473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

i m trying to implememt a stack with the template.

i m trying to implememt a stack with the template.but i m using a user defined data type called (struct) node in class stack.i have declared a pointer of struct node type which is then used in main function.i m facing a problem in accessing this pointer there.pllllllllzzzzzzzzz help me out but giving your valuable suggestions.i have the code below.thnx.

here is the code




Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. template <class T>
  3. class stack
  4. {
  5.  struct node
  6.  {
  7.   T data;
  8.     node *link;
  9.  }*top;
  10.  
  11.   public:
  12.   stack()
  13.   {
  14.      top=NULL;
  15.   }
  16.  
  17.   //struct node *push(T a);
  18.   //struct node *pop();
  19.  // display();
  20.  // };
  21.  
  22.  
  23.   node *push(T a)
  24.   { struct node* temp;
  25.      if(top==NULL)
  26.      {
  27.       temp=new node;
  28.       top=temp;
  29.       temp->data=a;
  30.       temp->link=NULL;
  31.       }
  32.  
  33.       else
  34.         {
  35.          temp=new node;
  36.          temp->data=a;
  37.          temp->link=top;
  38.          top=temp;
  39.          }
  40.      return top;
  41.     }
  42.  
  43.      node pop()
  44.     { struct node* temp;
  45.       if(top==NULL)
  46.       {
  47.         cout<<"stack is already empty \n " ;
  48.         return top;
  49.  
  50.         }
  51.  
  52.       else
  53.       {
  54.         temp=top;
  55.         top=top->link;
  56.         cout<<"element deleted is\n" <<temp->data;
  57.         delete(temp) ;
  58.         return top;
  59.         }
  60.  
  61.         }
  62.       display ()
  63.       {
  64.          if(top==NULL)
  65.          cout<<"stack is vacant \n" ;
  66.          else
  67.          {
  68.          while(top->link!=NULL)
  69.           {
  70.             cout<<top->data;
  71.             top=top->link;
  72.             }
  73.          }
  74.       }};
  75.  
  76.  
  77.  
  78.  
  79.  
  80.   void main()
  81.      { stack <int> s1;
  82.       int a,d;
  83.       char ch='y';
  84.       while(ch=='y')
  85.       {
  86.         cout<<"1. push the value \n 2. pop the value \n 3.display  elements \n";
  87.         cin>>d;
  88.         switch (d)
  89.         {
  90.          case 1:
  91.          cout<<"push the element\n";
  92.          cin>>a;
  93.          s1.*top=s1.push(a) ;
  94.          break;
  95.  
  96.          case 2:
  97.          cout<<"last element will b deleted \n";
  98.          s1.*top=s1.pop() ;
  99.          break;
  100.  
  101.          case 3:
  102.          cout<<"elements are \n" ;
  103.          s1.display();
  104.          break;
  105.  
  106.          default:cout<<"wrong choice" ;
  107.  
  108.          }
  109.  
  110.         cout<<"do wnt to continue" ;
  111.         cin>>ch;
  112.         if(ch=='y')
  113.         continue;
  114.         else
  115.         break;
  116.  
  117.         }
  118.  
  119.       }
Nov 17 '07 #1
5 1185
help me out plzzzzzzz.
Nov 17 '07 #2
Ganon11
3,652 Expert 2GB
1) You waited a total of 2 minutes before posting the last message. It may interest you to know that TSDN is a site in which experts participate from around the globe, on countless schedules - not your own. I, myself, was not even present to be able to answer your question in those 2 minutes. Others are working; others are eating; others aren't even awake! I must ask you to have some more patience with the volunteers who give their time to help out here at TSDN.

2) I'm unclear as to what your trouble is. You say you are facing a problem accessing the pointer - which pointer? What kind of error? Is your code compiling, but not running correctly? Is it giving you a segfault error? A compilation-time error? With a little more detail, the experts here will be infinitely more helpful to you.
Nov 17 '07 #3
Laharl
849 Expert 512MB
Regardless of pointer errors, void main() is nonstandard. Use int main() instead.
Nov 17 '07 #4
Meetee
931 Expert Mod 512MB
@OP:

#include<iostream.h> is an old version practice. Use
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
Please post the error message and your problem in detail.

Regards
Nov 17 '07 #5
thanks to all for their precious suggestions.
i have solved the problem by my ownself
Nov 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
4
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end....
1
by: Chris Cranford | last post by:
I am in the process of implementing a stack machine virtual environment where each entry on the stack is a 32-bit value which then can reference to any memory pointer, numeric value, or alike. ...
3
by: Pascal Steiss | last post by:
Hi All I don't understand the error that g++ tells me: --- percul3.cpp: In function `void OutputLattice(std::stack<latticeSite, std::deque<latticeSite, std::allocator<latticeSite> > >)':...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
4
by: Christian Christmann | last post by:
Hi, I'd like to store structs on an STL stack. Here is a piece of my code: #inclue <stack> ... struct storeInfo {
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
9
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
4
by: * Tong * | last post by:
First of all, thanks mlimber for answering my previous question. Now... I'm following the example in "C++ Templates: The Complete Guide", section 5.4 Template Template Parameters, and I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.