0% found this document useful (0 votes)
61 views

Datastructure Practical - 2

The document describes how to implement a stack data structure using arrays and structs in C. It includes functions to push, pop, peek, and display elements in the stack. The array implementation shows pushing, popping, and other operations without and with separate functions. The struct implementation defines a node struct with next pointers and uses pointers to the stack top to implement the stack.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Datastructure Practical - 2

The document describes how to implement a stack data structure using arrays and structs in C. It includes functions to push, pop, peek, and display elements in the stack. The array implementation shows pushing, popping, and other operations without and with separate functions. The struct implementation defines a node struct with next pointers and uses pointers to the stack top to implement the stack.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

20SOECE13007_DURAGIYA HITESH V.

DATA STRUCTURE(CE317)

Tutorial - 2

R.K. UNIVERSITY 1
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :

R.K. UNIVERSITY 2
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

1. Design a data structure “stack” with necessary members (array,top,size). Also


implement operations on stack (push, pop, peep, change). (Note:- with and
without function ).

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

void peek();

int main()

top=-1;

int r;

printf("Enter the size of STACK : ");

scanf("%d",&n);

printf("0.Without Function\n1.With Function\n-> ");

scanf("%d",&r);

if(r==0)

printf("\n STACK OPERATIONS USING ARRAY");

printf("\n--------------------------------");

printf("\n\t1.PUSH\n\t2.POP\n\t3.PEEK\n\t4.DISPLAY\n\t5.EXIT");

printf("\nEnter your choice : ");

scanf("%d",&choice);

R.K. UNIVERSITY 3
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

if(choice==1)

if(top==n)

printf("Stack is overflow.");

else

int data;

printf("Enter value to push : ");

scanf("%d",&data);

top = top + 1;

stack[top] = data;

else if(choice==2)

if(top==-1)

printf("Stack is underflow");

else

R.K. UNIVERSITY 4
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

int data;

stack[top] = data;

top = top - 1;

printf("Popped Element : %d",data);

else if(choice==3)

if(top==-1)

printf("Stack is underflow");

else

printf("Top Element = %d",stack[top]);

else if(choice==4)

if(top>=0)

printf("The elements in STACK -> ");

for(i=top; i>=0; i--)

R.K. UNIVERSITY 5
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

printf("\n%d",stack[i]);

else

printf("The STACK is empty");

else

printf ("\t Please Enter a Valid Choice(1/2/3/4/5)");

else

printf("\n STACK OPERATIONS USING ARRAY");

printf("\n--------------------------------");

printf("\n\t1.PUSH\n\t2.POP\n\t3.PEEEK\n\t4.DISPLAY\n\t5.EXIT");

do

printf("\nEnter the Choice:");

scanf("%d",&choice);

switch(choice)

R.K. UNIVERSITY 6
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

peek();

break;

case 5:

break;

R.K. UNIVERSITY 7
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

default:

printf ("\n\t Please Enter a Valid Choice(1/2/3/4/5)");

while(choice!=5);

return 0;

void push()

if(top>=n-1)

printf("\nSTACK is over flow");

else

printf("Enter a value to be pushed:");

scanf("%d",&x);

top++;

R.K. UNIVERSITY 8
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

stack[top]=x;

void pop()

if(top<=-1)

printf("\nStack is under flow");

else

printf("\nThe popped elements is %d",stack[top]);

top--;

void peek()

if(top==-1)

printf("Stack is underflow");

else

R.K. UNIVERSITY 9
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

printf("Top Element = %d",stack[top]);

void display()

if(top>=0)

printf("\nThe elements in STACK -> ");

for(i=top; i>=0; i--)

printf("\n%d",stack[i]);

else

printf("\nThe STACK is empty");

R.K. UNIVERSITY 10
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :

R.K. UNIVERSITY 11
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

2. Design a data structure “stack” using “struct” with necessary members


(array,top,size). Also implement operations on stack (push, pop, peep, change).
(Note:- with and without function ).

#include <stdio.h>

#include <stdlib.h>

struct node

int info;

struct node *ptr;

}*top,*top1,*temp;

int topelement();

void push(int data);

void pop();

void empty();

void display();

void destroy();

void stack_count();

void create();

int count = 0;

int main()

R.K. UNIVERSITY 12
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

int no, ch, e;

printf("\n 1 - Push");

printf("\n 2 - Pop");

printf("\n 3 - Top");

printf("\n 4 - Empty");

printf("\n 5 - Exit");

printf("\n 6 - Display");

printf("\n 7 - Stack Count");

printf("\n 8 - Destroy stack");

create();

while (1)

printf("\n Enter choice : ");

scanf("%d", &ch);

switch (ch)

case 1:

printf("Enter data : ");

scanf("%d", &no);

R.K. UNIVERSITY 13
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

push(no);

break;

case 2:

pop();

break;

case 3:

if (top == NULL)

printf("No elements in stack");

else

e = topelement();

printf("\n Top element : %d", e);

break;

case 4:

empty();

break;

case 5:

exit(0);

case 6:

display();

break;

case 7:

R.K. UNIVERSITY 14
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

stack_count();

break;

case 8:

destroy();

break;

default :

printf(" Wrong choice, Please enter correct choice ");

break;

return 0;

void create()

top = NULL;

void stack_count()

printf("\n No. of elements in stack : %d", count);

R.K. UNIVERSITY 15
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

void push(int data)

if (top == NULL)

top =(struct node *)malloc(1*sizeof(struct node));

top->ptr = NULL;

top->info = data;

else

temp =(struct node *)malloc(1*sizeof(struct node));

temp->ptr = top;

temp->info = data;

top = temp;

count++;

void display()

top1 = top;

if (top1 == NULL)

R.K. UNIVERSITY 16
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

printf("Stack is empty");

return;

while (top1 != NULL)

printf("%d ", top1->info);

top1 = top1->ptr;

void pop()

top1 = top;

if (top1 == NULL)

printf("\n Error : Trying to pop from empty stack");

return;

else

top1 = top1->ptr;

printf("\n Popped value : %d", top->info);

R.K. UNIVERSITY 17
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

free(top);

top = top1;

count--;

int topelement()

return(top->info);

void empty()

if (top == NULL)

printf("\n Stack is empty");

else

printf("\n Stack is not empty with %d elements", count);

void destroy()

top1 = top;

while (top1 != NULL)

R.K. UNIVERSITY 18
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

top1 = top->ptr;

free(top);

top = top1;

top1 = top1->ptr;

free(top1);

top = NULL;

printf("\n All stack elements destroyed");

count = 0;

R.K. UNIVERSITY 19
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :
Infix to postfix conversion

R.K. UNIVERSITY 20
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

3. Develop the following stack application

a. Infix to postfix conversion

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

struct Stack

int top;

unsigned capacity;

int* array;

};

struct Stack* createStack( unsigned capacity )

struct Stack* stack = (struct Stack*)

malloc(sizeof(struct Stack));

if (!stack)

return NULL;

stack->top = -1;

R.K. UNIVERSITY 21
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

stack->capacity = capacity;

stack->array = (int*) malloc(stack->capacity *

sizeof(int));

return stack;

int isEmpty(struct Stack* stack)

return stack->top == -1 ;

char peek(struct Stack* stack)

return stack->array[stack->top];

char pop(struct Stack* stack)

if (!isEmpty(stack))

return stack->array[stack->top--] ;

return '$';

void push(struct Stack* stack, char op)

R.K. UNIVERSITY 22
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

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

int isOperand(char ch)

return (ch >= 'a' && ch <= 'z') ||

(ch >= 'A' && ch <= 'Z');

int Prec(char ch)

switch (ch)

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

R.K. UNIVERSITY 23
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

return -1;

int infixToPostfix(char* exp)

int i, k;

struct Stack* stack = createStack(strlen(exp));

if(!stack)

return -1 ;

for (i = 0, k = -1; exp[i]; ++i)

if (isOperand(exp[i]))

exp[++k] = exp[i];

else if (exp[i] == '(')

push(stack, exp[i]);

R.K. UNIVERSITY 24
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

else if (exp[i] == ')')

while (!isEmpty(stack) && peek(stack) != '(')

exp[++k] = pop(stack);

if (!isEmpty(stack) && peek(stack) != '(')

return -1;

else

pop(stack);

else

while (!isEmpty(stack) &&

Prec(exp[i]) <= Prec(peek(stack)))

exp[++k] = pop(stack);

push(stack, exp[i]);

while (!isEmpty(stack))

exp[++k] = pop(stack );

exp[++k] = '\0';

R.K. UNIVERSITY 25
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

printf( "%s", exp );

int main()

char exp[] = "a+b+(c/d)*e/f";

infixToPostfix(exp);

return 0;

R.K. UNIVERSITY 26
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :
Evaluation of postfix expression

R.K. UNIVERSITY 27
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

b. Evaluation of postfix expression

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#include <stdlib.h>

struct Stack

int top;

unsigned capacity;

int* array;

};

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;

R.K. UNIVERSITY 28
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

return stack;

int isEmpty(struct Stack* stack)

return stack->top == -1 ;

char peek(struct Stack* stack)

return stack->array[stack->top];

char pop(struct Stack* stack)

if (!isEmpty(stack))

return stack->array[stack->top--] ;

return '$';

void push(struct Stack* stack, char op)

R.K. UNIVERSITY 29
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

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

int val1 = pop(stack);

int val2 = pop(stack);

switch (exp[i])

case '+': push(stack, val2 + val1); break;

case '-': push(stack, val2 - val1); break;

R.K. UNIVERSITY 30
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

case '*': push(stack, val2 * val1); break;

case '/': push(stack, val2/val1); break;

return pop(stack);

int main()

char exp[] = "153+8-3*";

printf ("postfix evaluation: %d", evaluatePostfix(exp));

return 0;

R.K. UNIVERSITY 31
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :
To demonstrate the use of multiple stack

R.K. UNIVERSITY 32
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

R.K. UNIVERSITY 33
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

c. To demonstrate the use of multiple stack

#include<stdio.h>

#include<conio.h>

#define MAX_X 5

#define MAX_Y 5

int topx=-1;

int topy=10;

void push_x(int *stack)

int info;

if(topx>=(MAX_X-1))

{ printf("\nStack OverFlow");

return;

else

{ printf("\nEnter The info To Push : ");

scanf("%d",&info);

topx++;

stack[topx]=info;

}}

R.K. UNIVERSITY 34
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

void push_y(int *stack)

int info;

if(topy<=(MAX_Y))

printf("\nStack OverFlow");

return;

else

printf("\nEnter The info To Push : ");

scanf("%d",&info);

topy--;

stack[topy]=info;

void pop_x(int *stack)

{ if(topx==-1)

printf("Stack X is Underflow");

return;

R.K. UNIVERSITY 35
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

else

printf("Item Poped from stack X is :%d\n",stack[topx]);

topx--;

void pop_y(int *stack)

{ if(topy==10)

{printf("Stack y is Underflow");

return;

else

{ printf("Item Poped from stack Y is:%d\n",stack[topy]);

topy++;

}}

void display_x(int *stack)

int i;

if(topx==-1)

R.K. UNIVERSITY 36
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

printf("Stack X is Empty");

return;

else

{ for(i=topx;i>=0;i--)

{printf("%d,",stack[i]);}

printf("\n");

}}

void display_y(int *stack)

int i;

if(topy==10)

{printf("Stack Y is Empty");

return;}

else

{for(i=topy;i<=9;i++)

printf("%d,",stack[i]);

printf("\n");

} }

R.K. UNIVERSITY 37
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

int main()

{ int choice;

char ch;

int stack[MAX_X+MAX_Y];

do

{ printf("1.Push_X\n2.Push_Y");

printf("\n3.Pop_X\n4.Pop_Y");

printf("\n5.Display_X\n6.Display_Y");

printf("\n7.Exit");

printf("\nEnter Choice : ");

scanf("%d",&choice);

switch(choice)

case 1: push_x(stack);break;

case 2: push_y(stack);break;

case 3: pop_x(stack);break;

case 4: pop_y(stack);break;

case 5: display_x(stack);break;

case 6: display_y(stack);break;

case 7: break;

default: printf("Wrong Option...");

R.K. UNIVERSITY 38
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

}while(choice!=7);

return 0;

R.K. UNIVERSITY 39

You might also like