Stack-Array Implementation: CS6202-Pragramming and Data Structures
Stack-Array Implementation: CS6202-Pragramming and Data Structures
STACK-ARRAY IMPLEMENTATION
#include<stdio.h>
#define STACK_SIZE 5
int top,s[10],item;
int pop()
{
if(top==-1)
return -1;
return s[top--];
}
void display()
{
int i;
if(top==-1)
{
printf("Stack is empty\n");
return;
}
printf("Contents of the stack :\n");
for(i=0;i<=top;i++)
printf("%d\n",s[i]);
}
void main()
{
int item_deleted,item;
int choice;
top=-1;
for(;;)
{
printf("1.Push\t2.Pop\t3.Display\t4.Exit\n");
1
CS6202-Pragramming and data structures
case 2:
item_deleted=pop();
if(item_deleted==-1)
printf("Stack is empty\n");
else
printf("Item deleted=%d\n",item_deleted);
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}
OUTPUT:
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:
1
Enter the item to be inserted:
2
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:
1
Enter the item to be inserted:
3
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:
1
Enter the item to be inserted:
4
1.Push 2.Pop 3.Display 4.Exit
2
CS6202-Pragramming and data structures
3
CS6202-Pragramming and data structures
4
CS6202-Pragramming and data structures
#include <stdio.h>
#define MAX_SIZE 5
typedef struct xyz STACK;
struct xyz
{
int element;
struct xyz *next;
}*top=NULL;
int count;
void push()
{
if(count==MAX_SIZE)
printf("STACK IS FULL\n");
else
{
STACK *node;
node=(STACK*)malloc(sizeof(STACK));
printf("\nEnter the element\n");
scanf("%d", &node->element);
node->next=top;
top=node;
count++;
}
}
void pop()
{
STACK *temp;
temp=top;
if(top==NULL)
{
printf("\nSTACK IS EMPTY");
}
else
{
int deleted_ele=temp->element;
top=top->next;
free(temp);
printf("\nThe poped element is: %d ",deleted_ele);
}
}
5
CS6202-Pragramming and data structures
void display()
{
STACK *temp;
temp=top;
while(temp->next!=NULL)
{
printf("\n%d", temp->element);
temp=temp->next;
}
printf("\n%d", temp->element);
}
void main()
{
int choice;
while(1)
{
printf("\n1.Push\t2.Pop\t3.Display\t4.Exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid Choice\n:");
}
}
6
CS6202-Pragramming and data structures
OUTPUT:
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:1
Enter the element
1
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:1
Enter the element
2
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:1
Enter the element
3
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:1
Enter the element
4
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:1
Enter the element
5
1.Push 2.Pop 3.Display 4.Exit
Enter the choice:1
STACK IS FULL
7
CS6202-Pragramming and data structures
8
CS6202-Pragramming and data structures
#include<stdio.h>
int q[10],rear=0,front=0,size=5;
void insert()
{
if(rear==size)
printf("\nQUEUE IS FULL");
else
{
rear++;
printf("\nenter the element ");
scanf("%d",&q[rear]);
printf("\n%d is Inserted",q[rear]);
}
}
void delete()
{
int i;
if(rear==front)
printf("\nQUEUE IS EMPTY");
else
{
printf("\nDeleted element is %d",q[front+1]);
for(i=front+1;i<rear;i++)
q[i]=q[i+1];
rear--;
}
}
void status()
{
if(rear==size)
printf("\nQueue is full ");
else if(rear==front)
printf("\nQueue is empty ");
else
printf("\nQueue has %d element(s)",rear);
}
void display()
{
9
CS6202-Pragramming and data structures
int i;
if(rear>size)
printf("\nQueue is full ");
else if(rear==front)
printf("\nQueue is empty ");
else
{
printf("The Queue has the element(s) \n");
printf("\t-------------------------------------\n");
for(i=front+1;i<=rear;i++)
printf("\t%d",q[i]);
printf("\n\t-------------------------------------\n");
}
}
void main()
{
int op,i;
while(1)
{
printf("\n1.INSERT\t2.DELETE\t3.STATUS\t4.DISPLAY\t5.EXIT");
printf("\nEnter your option:\n");
scanf("%d",&op);
switch(op)
{
case 1: insert(); break;
case 2: delete(); break;
case 3: status(); break;
case 4: display(); break;
case 5: exit(0);
}
}
}
OUTPUT:
1.INSERT 2.DELETE 3.STATUS 4.DISPLAY 5.EXIT
Enter your option:
1
enter the element 1
1 is Inserted
1.INSERT 2.DELETE 3.STATUS 4.DISPLAY 5.EXIT
Enter your option:
1
enter the element 2
2 is Inserted
10
CS6202-Pragramming and data structures
11
CS6202-Pragramming and data structures
12
CS6202-Pragramming and data structures
void insert()
{
if(count==MAX_SIZE)
printf("\nQUEUE IS FULL");
else
{
QUEUE *s;
s=malloc(sizeof(QUEUE));
printf("\nEnter the element ");
scanf("%d",&s->element);
s->next=NULL;
if(front==NULL)
rear=front=s;
else
{
rear->next=s;
rear=s;
}
count++;
printf("\n%d is inserted",s->element);
}
}
void delete()
{
int item;
if(front==NULL)
printf("\nQUEUE IS EMPTY");
else
{
item=front->element;
front=front->next;
13
CS6202-Pragramming and data structures
printf("\n%d is deleted",item);
}
}
void display()
{
QUEUE *i;
if(front==NULL)
printf("\nQUEUE IS EMPTY");
else
{
printf("\nQueue has the element(s)\n");
printf("\n\t---------------------------------------\n");
for(i=front;i!=NULL;i=i->next)
printf("\t%d",i->element);
printf("\n\t---------------------------------------\n");
}
}
void main()
{
int op;
while(1)
{
printf("\n1.INSERT\t2.DELETE\t3.DISPLAY\t4.EXIT");
printf("\nEnter your option ");
scanf("%d",&op);
switch(op)
{
case 1: insert(); break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
}
}
}
OUTPUT:
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
Enter the element 1
1 is inserted
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
14
CS6202-Pragramming and data structures
15
CS6202-Pragramming and data structures
void insertQ()
{
int item;
if(count==QSIZE)
printf("QUEUE IS FULL\n");
else
{
printf("Enter the item to be inserted:\n");
scanf("%d",&item);
rear=(rear+1)%QSIZE;
q[rear]=item;
count++;
}
void deleteQ()
{
int item;
if(count==0)
printf("QUEUE IS EMPTY\n");
else
{
item=q[front];
front=(front+1)%QSIZE;
count--;
printf("deleted element is %d\n",item);
}
}
void displayQ()
{
int i;
int temp=front;
if(count==0)
printf("QUEUE IS EMPTY\n");
else
{
printf("________________________________\n");
16
CS6202-Pragramming and data structures
for(i=1;i<=count;i++)
{
printf("%d\t",q[temp]);
temp=(temp+1)%QSIZE;
}
printf("\n_______________________________\n");
}
}
void main()
{
int op;
while(1)
{
printf("\n1.INSERT\t2.DELETE\t3.DISPLAY\t4.EXIT");
printf("\nEnter your option ");
scanf("%d",&op);
switch(op)
{
case 1: insertQ(); break;
case 2: deleteQ(); break;
case 3: displayQ(); break;
case 4: exit(0);
}
}
}
OUTPUT:
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
Enter the item to be inserted:
2
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
Enter the item to be inserted:
3
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 3
________________________________
2 3
_______________________________
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
Enter the item to be inserted:
17
CS6202-Pragramming and data structures
4
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
Enter the item to be inserted:
5
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
Enter the item to be inserted:
6
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 1
QUEUE IS FULL
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 3
________________________________
2 3 4 5 6
_______________________________
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 2
deleted element is 2
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 2
deleted element is 3
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 2
deleted element is 4
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 3
________________________________
5 6
_______________________________
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 2
deleted element is 5
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 2
deleted element is 6
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your option 2
QUEUE IS EMPTY
18
CS6202-Pragramming and data structures
void insert()
{
QUEUE *s;
s=malloc(sizeof(QUEUE));
printf("\nEnter the element ");
scanf("%d",&s->element);
s->next=NULL;
if(rear==NULL)
rear=front=s;
else
{
rear->next=s;
rear=s;
}
rear->next=front; //changes
printf("\n%d is inserted",s->element);
}
void delete()
{
QUEUE *temp;
temp=front;
if(front==NULL)
printf("\nQUEUE IS EMPTY");
else
{
if(front==rear) //changes
{
printf("\n%d is deleted",front->element);
front=rear=NULL;
}
else
{
printf("\n%d is deleted",front->element);
19
CS6202-Pragramming and data structures
front=front->next;
rear->next=front; //changes
}
temp->next=NULL;
free(temp);
}
}
void display()
{
QUEUE *i;
if(front==NULL)
printf("\nQUEUE IS EMPTY");
else
{
printf("\nQueue has the element(s)\n");
printf("\n\t---------------------------------------\n");
for(i=front;i!=rear;i=i->next) //changes
printf("\t%d",i->element);
printf("\t%d",i->element);
printf("\n\t---------------------------------------\n");
}
}
void main()
{
int op;
while(1)
{
printf("\n1.INSERT\t2.DELETE\t3.DISPLAY\t4.EXIT");
printf("\nEnter your option ");
scanf("%d",&op);
switch(op)
{
case 1: insert(); break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
}
}
}
OUTPUT:
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
20
CS6202-Pragramming and data structures
21
CS6202-Pragramming and data structures
void add_item_front()
{
int num;
printf("\n Enter item to insert:");
scanf("%d",&num);
if(front<=1)
{
printf("\n Cannot add item at front end");
return;
}
else
{
front--;
q[front]=num;
}
}
void add_item_rear()
{
int num;
printf("\n Enter Item to insert : ");
scanf("%d",&num);
if(rear==MAX)
{
printf("\n Queue is Overflow");
return;
}
else
{
rear++;
q[rear]=num;
if(rear==0)
rear=1;
if(front==0)
front=1;
}
}
22
CS6202-Pragramming and data structures
void delete_item_front()
{
int num;
if(front==0)
{
printf("\n Queue is Underflow\n");
return;
}
else
{
num=q[front];
printf("\n Deleted item is %d\n",num);
if(front==rear)
{
front=0;
rear=0;
}
else
{
front++;
}
}
}
void delete_item_rear()
{
int num;
if(rear==0)
{
printf("\n Cannot delete item at rear end\n");
return;
}
else
{
num=q[rear];
if(front==rear)
{
front=0;
rear=0;
}
else
{
rear--;
23
CS6202-Pragramming and data structures
void display()
{
int i;
printf("ELEMENTS OF DOUBLE ENDED QUEUE ARE:\n");
printf("_____________________________________\n");
for(i=front;i<=rear;i++)
{
printf("%d\t",q[i]);
}
printf("\n_____________________________________\n");
}
void main()
{
int op;
while(1)
{
printf("\n1.INSERT FRONT\t2.INSERT REAR\n3.DELETE
FRONT\t4.DELETE REAR\n5.DISPLAY\t6.EXIT\n");
printf("\nEnter your option ");
scanf("%d",&op);
switch(op)
{
case 1: add_item_front(); break;
case 2: add_item_rear(); break;
case 3: delete_item_front(); break;
case 4: delete_item_rear(); break;
case 5: display(); break;
case 6: exit(0);
}
}
}
24
CS6202-Pragramming and data structures
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev, *next;
};
struct node *head = NULL, *tail = NULL;
/*
* create sentinel (dummy head & tail) that helps us to do insertion and deletion
operation at front and rear so easily. And these dummy head and tail wont get deleted
till the end of execution of this program
*/
void createSentinels() {
head = createNode(0);
tail = createNode(0);
head->next = tail;
tail->prev = head;
}
25
CS6202-Pragramming and data structures
newnode = createNode(data);
temp = tail->prev;
tail->prev = newnode;
newnode->next = tail;
newnode->prev = temp;
temp->next = newnode;
}
void dequeueAtRear() {
struct node *temp;
if (tail->prev == head) {
printf("Queue is empty\n");
} else {
temp = tail->prev;
tail->prev = temp->prev;
temp->prev->next = tail;
free(temp);
}
return;
}
if (head->next == tail) {
printf("Queue is empty\n");
26
CS6202-Pragramming and data structures
return;
}
temp = head->next;
while (temp != tail) {
printf("%-3d", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int data, ch;
createSentinels();
while (1) {
printf("1. Enqueue at front\n2. Enqueue at rear\n");
printf("3. Dequeue at front\n4. Dequeue at rear\n");
printf("5. Display\n6. Exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the data to insert:");
scanf("%d", &data);
enqueueAtFront(data);
break;
case 2:
printf("Enter ur data to insert:");
scanf("%d", &data);
enqueueAtRear(data);
break;
case 3:
dequeueAtFront();
break;
case 4:
dequeueAtRear();
break;
case 5:
display();
break;
27
CS6202-Pragramming and data structures
case 6:
exit(0);
default:
printf("Pls. enter correct option\n");
break;
}
}
return 0;
}
OUTPUT:
1. Enqueue at front 2. Enqueue at rear
3. Dequeue at front 4. Dequeue at rear
5. Display 6. Exit
Enter your choice:1
Enter the data to insert:2
1. Enqueue at front 2. Enqueue at rear
3. Dequeue at front 4. Dequeue at rear
5. Display 6. Exit
Enter your choice:1
Enter the data to insert:3
1. Enqueue at front 2. Enqueue at rear
3. Dequeue at front 4. Dequeue at rear
5. Display 6. Exit
Enter your choice:1
Enter the data to insert:4
1. Enqueue at front 2. Enqueue at rear
3. Dequeue at front 4. Dequeue at rear
5. Display 6. Exit
Enter your choice:5
4 3 2
1. Enqueue at front 2. Enqueue at rear
3. Dequeue at front 4. Dequeue at rear
5. Display 6. Exit
Enter your choice:2
Enter ur data to insert:6
1. Enqueue at front 2. Enqueue at rear
3. Dequeue at front 4. Dequeue at rear
5. Display 6. Exit
Enter your choice:2
Enter ur data to insert:7
1. Enqueue at front 2. Enqueue at rear
28
CS6202-Pragramming and data structures
29
CS6202-Pragramming and data structures
ROUGH
#include<stdio.h>
#include<stdlib.h>
struct node
float coef;
int expo;
};
main( )
start1=create(start1);
start2=create(start2);
30
CS6202-Pragramming and data structures
printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
poly_add(start1, start2);
poly_mult(start1, start2);
}/*End of main()*/
int i,n,ex;
float co;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%f",&co);
scanf("%d",&ex);
start=insert_s(start,co,ex);
return start;
}/*End of create()*/
31
CS6202-Pragramming and data structures
tmp->coef=co;
tmp->expo=ex;
tmp->link=start;
start=tmp;
else
ptr=start;
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
return start;
}/*End of insert()*/
tmp->coef=co;
tmp->expo=ex;
32
CS6202-Pragramming and data structures
if(start==NULL)
tmp->link=start;
start=tmp;
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
tmp->link=ptr->link;
ptr->link=tmp;
return start;
}/*End of insert()*/
if(ptr==NULL)
printf("Zero polynomial\n");
return;
while(ptr!=NULL)
printf("(%.1fx^%d)", ptr->coef,ptr->expo);
33
CS6202-Pragramming and data structures
ptr=ptr->link;
if(ptr!=NULL)
printf(" + ");
else
printf("\n");
}/*End of display()*/
start3=NULL;
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
else if(p1->expo==p2->expo)
start3=insert(start3,p1->coef+p2->coef,p1->expo);
34
CS6202-Pragramming and data structures
p1=p1->link;
p2=p2->link;
while(p1!=NULL)
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
while(p2!=NULL)
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
display(start3);
}/*End of poly_add() */
start3=NULL;
if(p1==NULL || p2==NULL)
35
CS6202-Pragramming and data structures
return;
while(p1!=NULL)
p2=p2_beg;
while(p2!=NULL)
start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
p1=p1->link;
display(start3);
}/*End of poly_mult()*/
36
CS6202-Pragramming and data structures
}
int pop()
{ /* Function for POP operation */
return(s[top--]);
}
main()
{ /* Main Program */
char pofx[50],ch;
int i=0,op1,op2;
printf("\n\nRead the Postfix Expression ? ");
scanf("%s",pofx);
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch)) push(ch-'0'); /* Push the operand */
else
{ /* Operator,pop two operands */
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}
printf("\n Given Postfix Expn: %s\n",pofx);
printf("\n Result after Evaluation: %d\n",s[top]);
}
OUTPUT:
Read the Postfix Expression ? 234+*5*
Given Postfix Expn: 234+*5*
Result after Evaluation: 70
BALANCING SYMBOLS
#include <stdio.h>
#define max 50
int top = -1;
char stk[max],exp[100];
37
CS6202-Pragramming and data structures
top++;
stk[top]= element;
}
char pop()
{
int element=stk[top];
return element;
}
void main()
{
int i;
printf("\nEnter an infix expression ");
gets(exp);
for(i=0; exp[i] != '\0'; i++)
{
if( exp[i]=='(' || exp[i] =='[' || exp[i] == '{' )
{
push(exp[i]);
}
else if ( exp[i] == ')' )
{
if( pop() == '(' )
top--;
else
{
printf("Unbalanced exp");
exit(0);
}
}
else if ( exp[i] == ']' )
{
if( pop() == '[' )
top--;
else
{
printf("Unbalanced exp");
exit(0);
}
}
else if ( exp[i] == '}' )
{
38
CS6202-Pragramming and data structures
OUTPUT:
Enter an infix expression (a+(b*c)-(D/O))
The expression (a+(b*c)-(D/O)) is balancedlinux-yx0y:/home/root1/DS # ./a.out
39
CS6202-Pragramming and data structures
char infix[30],postfix[30];
printf("Enter the infix expression:\n");
scanf("%s",infix);
infix2postfix(infix,postfix);
printf("The postfix expression is:\n");
printf("%s",postfix);
return 0;
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-': return 1;
case '*':
case '/': return 3;
case '$':
case '^': return 6;
case '(': return 9;
case ')':return 0;
default: return 7;
}
}
int F(char symbol)
{
switch(symbol)
{
case'+':
case '-':return 2;
case '*':
case '/': return 4;
case '$':
case '^': return 5;
case '(': return 0;
case '#': return -1;
default:return 8;
}
}
void infix2postfix(char * infix, char * postfix)
{
int top=-1,i,j=0;
char s[30],symbol;
s[++top]='#';
for(i=0;i<strlen(infix);i++)
40
CS6202-Pragramming and data structures
{
symbol=infix[i];
while(F(s[top])>G(symbol))
postfix[j++]=s[top--];
if(F(s[top])!=G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
postfix[j++]=s[top--];
postfix[j]='\0';
}
OUTPUT:
Enter the infix expression:
(a+b)*c/d
The postfix expression is:
ab+c*d/
Enter the infix expression:
A/B^C+D*E-F*G
The postfix expression is:
ABC^/DE*+FG*-
Enter the infix expression:
(B^2-4*A*C)^(1/2)
The postfix expression is:
B2^4A*C*-12/^
41