473,666 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i m trying to implememt a stack with the template.

8 New Member
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.pllllllll zzzzzzzzz 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 1191
meetharry19
8 New Member
help me out plzzzzzzz.
Nov 17 '07 #2
Ganon11
3,652 Recognized Expert Specialist
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 Recognized Expert Contributor
Regardless of pointer errors, void main() is nonstandard. Use int main() instead.
Nov 17 '07 #4
Meetee
931 Recognized Expert Moderator Contributor
@OP:

#include<iostre am.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
meetharry19
8 New Member
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
30088
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 part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
4
2298
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. I tried two solutions: * first is to 'substract' the used element from existing tree
1
4071
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. My understanding of "stack" versus "heap" is that unless I use new/delete in order to specifically allocate memory for a variable's storage space that it will be allocated upon the stack, correct? If so, the following three declarations would...
3
1742
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> > >)': percul3.cpp:49: `iterator' is not a member of type `std::stack<latticeSite, std::deque<latticeSite, std::allocator<latticeSite> > >' percul3.cpp:49: parse error before `;' token
4
2616
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 is directly as in the book, except for where I added the comments at the lines I wanted to refer to or to skip sections of code. template <class Element_Type> class Stack {
4
6213
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
4438
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 in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
9
2538
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 constructor and get me an Array<int32> with a size 0? The memory used is the stack, right? ia = Array<int32>(10);
4
3030
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 wondering if it's basics/stack8.hpp example can be compiled under g++ 4. First a bit background about template template: 5.4 Template Template Parameters
0
8449
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8876
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.