You are on page 1of 17

DS Practical

Roll no:29

Practical No:3

1]Implementation of stacks using Linked List


a] Write a program to implement Stack using Array.
Program:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5]; int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top>=4)
{
cout<<"stack is full\n";
return;
}
stk[++top]=x;
DS Practical
Roll no:29

cout<<"Element Inserted";
}
void pop()
{
if(top <0)
{
cout<<"stack is empty\n";
return;
}
cout<<"Deleted " <<stk[top--]<<endl;
}
void display()
{
if(top<0)
{
cout<<"stack empty \n";
return;
}
for(int i=top;i>=0;i--)
{
cout<<"Inserted Number are:-";
cout<<stk[i] <<" "<<endl;
}
}
};
DS Practical
Roll no:29

int main()
{
cout<<"Roll no 29";
int ch;
stack st; while(1)
{
cout <<"\n\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit\n\nEnter Your choice : ";
cin >> ch;
switch(ch)
{
case 1: cout <<"Enter the element : ";
cin >> ch; st.push(ch);
break;
case 2: st.pop();
break;
case 3: st.display();
break;
case 4:exit(0);
break;
}
}
return (0);
}
DS Practical
Roll no:29

OUTPUT:

b] Write a program to implement


Stack using Linked List.
Program:
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node* link;
};

struct Node* top;


void push(int data)
{
struct Node* temp;
temp = new Node();
DS Practical
Roll no:29

if (!temp)
{
cout << "\nHeap Overflow";
exit(1);
}
temp->data = data;
temp->link = top;
top = temp;
}
int isEmpty()
{
return top == NULL;
}
int peek()
{
if (!isEmpty())
return top->data;
else
exit(1);
}
void pop()
{
struct Node* temp;
if (top == NULL)
{
cout << "\nStack Underflow" << endl;
exit(1);
}
else
{
temp = top;
top = top->link;
temp->link = NULL;
free(temp);
}
}
void display()
{
struct Node* temp;
if (top == NULL)
DS Practical
Roll no:29

{
cout << "\nStack Underflow";
exit(1);
}
else
{
temp = top;
while (temp != NULL)
{
cout << temp->data << "-> ";
temp = temp->link;
}
}
}
int main()
{
push(11);
push(22);
push(33);
push(44);
display();
cout << "\nTop element is "
<< peek() << endl;
pop();
pop();
display();
cout << "\nTop element is "
<< peek() << endl;

return 0;
}

OUTPUT:
DS Practical
Roll no:29

2]Implementation of Stack Applications


a] Write a program to implement Postfix evaluation.
Program:
#include <iostream>
using namespace std;
struct Stack
{
int top;
unsigned capacity;
int* array;
DS Practical
Roll no:29

};

struct Stack* createStack( unsigned capacity )


{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

if (!stack) return NULL;

stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));

if (!stack->array) return NULL;

return stack;
}

int isEmpty(struct Stack* stack)


{
return stack->top == -1 ;
}

char peek(struct Stack* stack)


{
return stack->array[stack->top];
DS Practical
Roll no:29

char pop(struct Stack* stack)


{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}

void push(struct Stack* stack, char op)


{
stack->array[++stack->top] = op;
}

int evaluatePostfix(char* exp)


{
struct Stack* stack = createStack(strlen(exp));
int i;
if (!stack) return -1;
for (i = 0; exp[i]; ++i)
{
if (isdigit(exp[i]))
push(stack, exp[i] - '0');
else
{
DS Practical
Roll no:29

int val1 = pop(stack);


int val2 = pop(stack);
switch (exp[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2/val1); break;
}
}
}
return pop(stack);
}

int main()
{
char exp[] = "231*+9-";
cout<<"postfix evaluation: "<< evaluatePostfix(exp);
return 0;
}

OUTPUT:
DS Practical
Roll no:29

b] Write a program to implement Balancing parenthesis.


Program:
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;

struct node
{
DS Practical
Roll no:29

int data;
struct node *next;
};
struct node *tmp=NULL;
struct node *tmp1=NULL;
struct node *top=NULL;
struct node *ptr=NULL;
int push(char x)
{
tmp = new node;
tmp->data=x;
tmp->next=NULL;
if(top == NULL)
{
top=tmp;
}
else
{
tmp1=top;
top=tmp;
tmp->next=tmp1;
}
}

char pop()
DS Practical
Roll no:29

{
if(top==NULL)
{
cout<<"Stack is empty.\n";
}
else
{
ptr=top;
top=top->next;
return(ptr->data);
delete(ptr);
}
}

int main()
{
int len,i;
char c,d,e;
char a[30];
cout<<"Enter expression :\n";
cin>>a;
len=strlen(a);

for(i=0;i<len;i++)
{
DS Practical
Roll no:29

if(a[i]=='{' || a[i]=='[' || a[i]=='(')


{
push(a[i]);
}
else
{
switch(a[i])
{
case ')':
c=pop();
if(c=='{' || c=='[')
{
getch();
}
break;

case ']':
d=pop();
if(d=='{' || d=='(')
{
getch();
}
break;

case '}':
DS Practical
Roll no:29

e=pop();
if(e=='(' || e=='[')
{
getch();
}
break;
default:
cout<<"Enter the correct choice";
getch();
}
}
}
if(top==NULL)
cout<<"Balanced\n";
else
cout<<"Unbalanced\n";
getch();
return 0;
}

OUTPUT:
DS Practical
Roll no:29
DS Practical
Roll no:29

You might also like