0% found this document useful (0 votes)
21 views4 pages

Saqib Cheema 5059 Task 3

Uploaded by

saqibyu961
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Saqib Cheema 5059 Task 3

Uploaded by

saqibyu961
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

‭Question #1 :A program that implements a stack operations using array.

‭ include <iostream>‬
#
‭#include <bits/stdc++.h>‬
‭using namespace std;‬
‭#define MAX 1000‬
‭class Stack {‬
‭int top;‬
‭int a[MAX]; // Maximum size of Stack‬
‭public:‬
‭Stack() { top = -1; }‬
‭void push(int x);‬
‭int pop();‬
‭int peek();‬
‭bool isEmpty();‬
‭bool isFull();‬
‭void display();‬
‭};‬
‭void Stack::push(int x)‬
‭{‬
‭if (isFull()) {‬
‭cout << "Stack Overflow";‬
‭}‬
‭else {‬
‭a[++top] = x;‬
‭cout << x << " pushed into stack\n";‬
‭}‬
‭}‬
‭int Stack::pop()‬
‭{‬
‭if (isEmpty()) {‬
‭cout << "Stack Underflow";‬
‭return 0;‬
‭}‬
‭else {‬
‭int x = a[top--];‬
‭return x;‬
‭}‬
‭}‬
‭int Stack::peek()‬
‭{‬
‭if (isEmpty()) {‬
‭cout << "Stack is Empty";‬
‭return 0;‬
‭}‬
‭else {‬
‭int x = a[top];‬
‭return x;‬
}‭ ‬
‭}‬
‭bool Stack::isEmpty()‬
‭{‬
‭return (top < 0);‬
‭}‬
‭bool Stack::isFull()‬
‭{‬
‭return(top>=MAX-1);‬
‭}‬
‭void Stack::display()‬
‭{‬
‭cout<<"Stack elements are\n";‬
‭for(int i=top;i>=0;i--)‬
‭cout<<a[i]<<" ";‬
‭}‬
‭// Driver program to test above functions‬
‭int main()‬
‭{‬
‭class Stack s;‬
‭s.push(10);‬
‭s.push(20);‬
‭s.push(30);‬
‭cout << s.pop() << " Popped from stack\n";‬
‭s.display();‬
‭return 0;‬
‭}‬

‭Question #2 Program to implement Linked Stack.‬

‭ include<iostream>‬
#
‭using namespace std;‬
‭struct Node‬
‭{‬
‭int data;‬
‭Node *next;‬
‭}*top=NULL,*p;‬
‭Node* newnode(int x)‬
‭{‬
‭p=new Node;‬
‭p->data=x;‬
‭p->next=NULL;‬
‭return(p);‬
‭}‬
‭void push(Node *q)‬
‭{‬
i‭f(top==NULL)‬
‭top=q;‬
‭else‬
‭{‬
‭q->next=top;‬
‭top=q;‬
‭}‬
‭}‬
‭void pop(){‬
‭if(top==NULL){‬
‭cout<<"Stack is empty!!";‬
‭}‬
‭else{‬
‭cout<<"Deleted element is "<<top->data;‬
‭p=top;‬
‭top=top->next;‬
‭delete(p);‬
‭}‬
‭}‬
‭void showstack()‬
‭{‬
‭Node *q;‬
‭q=top;‬
‭if(top==NULL){‬
‭cout<<"Stack is empty!!";‬
‭}‬
‭else{‬
‭while(q!=NULL)‬
‭{‬
‭cout<<q->data<<" ";‬
‭q=q->next;‬
‭}‬
‭}‬
‭}‬
‭int main()‬
‭{‬
‭int ch,x;‬
‭Node *nptr;‬
‭while(1)‬
‭{‬
‭cout<<"\n\n1.Push\n2.Pop\n3.Display\n4.Exit";‬
‭cout<<"\nEnter your choice(1-4):";‬
‭cin>>ch;‬
‭switch(ch){‬
‭case 1: cout<<"\nEnter data:";‬
‭cin>>x;‬
‭nptr=newnode(x);‬
‭push(nptr);‬
‭ reak;‬
b
‭case 2: pop();‬
‭break;‬
‭case 3: showstack();‬
‭break;‬
‭case 4: exit(0);‬
‭default: cout<<"\nWrong choice!!";‬
‭}‬
‭}‬
‭return 0;‬
‭}‬

You might also like