C Stack - JohnHau/mis GitHub Wiki

image

image

C++ Stack Example Here are some example program demonstrating the concept of stack in C++ practically.

Pushing As already told, insertion in a stack is called as pushing.

Pushing in Stack-Array /* C++ Stack - Example Program of C++ Stack

  • This C++ program demonstrates the concept
  • of Pushing in the stack-array in C++ */

#include<iostream.h> #include<stdlib.h> #include<conio.h>

int push(int [], int &, int); void display(int [], int); const int SIZE = 50;

void main() { clrscr(); int stack[SIZE], item, top=-1, res; char ch='y'; while(ch=='y' || ch=='Y') { cout<<"Enter item for insertion: "; cin>>item; res = push(stack, top, item); if(res == -1) { cout<<"Overflow..!!..Aborting..Press a key to exit..\n"; getch(); exit(1); } cout<<"Element inserted successfully..!!\n"; cout<<"\nThe Stack now is:\n"; display(stack, top); cout<<"\nWant to enter more ? (y/n).. "; cin>>ch; } getch(); }

int push(int stack[], int &top, int elem) { if(top == SIZE-1) { return -1; } else { top++; stack[top] = elem; } return 0; } void display(int stack[], int top) { cout<<stack[top]<<" <-- "<<"\n"; for(int i=top-1; i>=0; i--) { cout<<stack[i]<<"\n"; } }

image

Pushing in Linked-Stack /* C++ Stack - Example Program of C++ Stack

  • This C++ program demonstrates the concept
  • of Pushing in the linked-stack in C++ */

#include<iostream.h> #include<stdlib.h> #include<conio.h>

struct node { int info; node *next; } *top, *newptr, *save, *ptr;

node *create_new_node(int); void push(node *); void display(node *);

void main() { clrscr(); int inf; char ch='y'; top=NULL; while(ch=='y' || ch=='Y') { cout<<"Enter information for the new node.. "; cin>>inf; newptr = create_new_node(inf); if(newptr == NULL) { cout<<"\nSorry..!!..Cannot create new node..!!..Aborting..!!\n"; cout<<"Press any key to exit..\n"; getch(); exit(1); } push(newptr); cout<<"\nNow the linked-stack is:\n"; display(top); cout<<"\nWant to enter more ? (y/n).. "; cin>>ch; } getch(); }

node *create_new_node(int x) { ptr = new node; ptr->info = x; ptr->next = NULL; return ptr; }

void push(node *n) { if(top==NULL) { top=n; } else { save = top; top = n; n->next = save; } }

void display(node *n) { while(n != NULL) { cout<info<<" -> "; n = n->next; } cout<<"!!\n"; }

image

Popping As already told, deletion from a stack is called as popping.

Popping from Array-Stack /* C++ Stack - Example Program of C++ Stack

  • This C++ program demonstrates the concept
  • of Popping from the stack-array in C++ */

#include<iostream.h> #include<stdlib.h> #include<conio.h>

int pop(int [], int &); int push(int [], int &, int); void display(int [], int); const int SIZE = 50;

void main() { clrscr(); int stack[SIZE], item, top=-1, res; char ch='y'; while(ch=='y' || ch=='Y') { cout<<"Enter item for insertion: "; cin>>item; res = push(stack, top, item); if(res == -1) { cout<<"Overflow..!!..Aborting..Press a key to exit..\n"; getch(); exit(1); } cout<<"\nThe Stack now is:\n"; display(stack, top); cout<<"\nWant to enter more ? (y/n).. "; cin>>ch; } cout<<"Now the deletion of elements starts..\n"; ch='y'; while(ch=='y' || ch=='Y') { res = pop(stack, top); if(res==-1) { cout<<"\nUnderflow..!!..Aborting..!!..Press a key to exit..\n"; getch(); exit(2); } else { cout<<"\nElement deleted is: "<<res<<endl; cout<<"\nThe Stack now is:\n"; display(stack, top); } cout<<"Want to delete more ? (y/n).. "; cin>>ch; } getch(); }

int push(int stack[], int &top, int elem) { if(top == SIZE-1) { return -1; } else { top++; stack[top] = elem; } return 0; }

int pop(int stack[], int &top) { int ret; if(top==-1) { return -1; } else { ret=stack[top]; top--; } return ret; }

void display(int stack[], int top) { if(top==-1) { return; } cout<<stack[top]<<" <-- "<<"\n"; for(int i=top-1; i>=0; i--) { cout<<stack[i]<<"\n"; } }

image

image

Popping from a Linked-Stack /* C++ Stack - Example Program of C++ Stack

  • This C++ program demonstrates the concept
  • of Popping from the linked-stack in C++ */

#include<iostream.h> #include<stdlib.h> #include<conio.h>

struct node { int info; node *next; } *top, *newptr, *save, *ptr;

node *create_new_node(int); void push(node *); void pop(); void display(node *);

void main() { clrscr(); int inf; char ch='y'; top=NULL; while(ch=='y' || ch=='Y') { cout<<"Enter information for the new node.. "; cin>>inf; newptr = create_new_node(inf); if(newptr == NULL) { cout<<"\nSorry..!!..Cannot create new node..!!..Aborting..!!\n"; cout<<"Press any key to exit..\n"; getch(); exit(1); } push(newptr); cout<<"\nWant to enter more ? (y/n).. "; cin>>ch; } clrscr(); do { cout<<"The Stack now is: \n"; display(top); cout<<"\nWant to pop an element ? (y/n).. "; cin>>ch; if(ch=='y' || ch=='Y') { pop(); } cout<<"\n"; }while(ch=='y' || ch=='Y');

getch(); }

node *create_new_node(int x) { ptr = new node; ptr->info = x; ptr->next = NULL; return ptr; }

void push(node *n) { if(top==NULL) { top=n; } else { save = top; top = n; n->next = save; } }

void pop() { if(top==NULL) { cout<<"\nUnderflow..!!..Press any key to exit..\n"; getch(); exit(2); } else { ptr = top; top = top->next; delete ptr; } }

void display(node *n) { while(n != NULL) { cout<info<<" -> "; n = n->next; } cout<<"!!\n"; }

image

image

⚠️ **GitHub.com Fallback** ⚠️