473,408 Members | 2,888 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,408 software developers and data experts.

Linked Stack

Just the if function executes in the functions and does'nt go to the next condition below the if condition.
What could have possibly gone wrong?

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct stack
{
int info;
stack *next;
};
stack *top=NULL;

void push()
{
int data;
cout<<"Enter data";
cin>>data;
//create a new node
stack*ptr=new stack;
ptr->info=data;
ptr->next=top;
}
void pop()
{
stack *ptr;
//empty
if(top==NULL)
{
cout<<"Stack empty";
return;
}
ptr=top;
cout<<ptr->info;
top=ptr->next;
delete ptr;
}

void display()
{
if(top==NULL)
{
cout<<"Stack empty";
return;
}
stack *ptr=top;
while(ptr!=NULL)
{
cout<<ptr->info;
ptr=ptr->next;
}
}

void main()
{
int op;
char cnt;
do
{
cout<<"1.Insert";
cout<<"2.Deletion";
cout<<"3.Display";
cout<<"4.Exit";

cout<<"Enter the choice";
cin>>op;
switch(op)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4: exit(0);
}
cout<<"Do you wanna continue ?";
cin>>cnt;
}
while(cnt=='Y'||cnt=='y');
}
Dec 2 '18 #1
1 1305
weaknessforcats
9,208 Expert Mod 8TB
First thing is to remove data entry from the push() function. You call push using the data. It's not the job of push() to come up with the data.


You also need to supply the address of the stack variable you are going to se to push the data


Expand|Select|Wrap|Line Numbers
  1. int push(stack* node, int data);
  2.  
etc for the other functions.

Also missing is the list itself. The code just uses node values

This is a typical list:

Expand|Select|Wrap|Line Numbers
  1. struct list
  2. {
  3.  
  4.    stack* top;   /*start of list */
  5.    stack* end;   /* end of list */
  6.    stack* current; /* the current position in the list.
  7. };

Now you can create a list:


Expand|Select|Wrap|Line Numbers
  1. list* theList = create();
this function creates the list on the heap and sets all the member pointers to zero.

Now you can:

Expand|Select|Wrap|Line Numbers
  1. AddToEnd(list* thelist, int data);

The user of the list does not know about the nodes. The user just creates the list and adds a value too the end of the list. The AddToEnd function creates the stack variable and places the data in the info member. The function also adjust the pointers in the list struct as necessary.

Start with main() and code what you want the user to do. Then write the functions to do it. Try to get a design in place before you actually start coding.
Dec 3 '18 #2

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

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
13
by: Kenneth Lantrip | last post by:
In Ansi-C C99 or Standard C or in GCC... Is there a way to push a value onto the stack and then later retrieve it within the same function? void foo(void) {
3
by: AMRaymo | last post by:
Can someone guide me in the right direction on how to enqueue and dequeue/pop and push within a linked list? I've figured out the basic idea, but getting the other options in it seems ot be a...
3
by: i | last post by:
Hi, I'm working with an array of nodes, numbering roughly in the thousands. Each node has at least one, but up to four, references to another node - North, South, East, or West. I'm trying to...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
2
by: hakimks | last post by:
You are provided with a sample C programs: calc.c, which implements a reverse polish notation calculator. Study it carefully. This program uses a stack (of course!) but the stack implementation is...
4
by: SP | last post by:
Hi, I've a Stack. Now I would use this to mantain a LIFO objects chain. My problem is that I want to limit the Stack dimension. If the Stack is full and I want to add a object, I eant to remove...
4
by: Beow | last post by:
Hello, I'm still very new to C++ and have encountered some trouble in understanding how exactly the copy constructor for a stack class (given as an example in the book I've been learning from)...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.