Program: Implement Stack ADT Using Array
Program: Implement Stack ADT Using Array
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
}
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
while(choice!=4);
return 0;
}
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
Output:
Program: Convert an Infix expression to Postfix expression using stack ADT.
#include<stdio.h>
#include<ctype.h>
char stack[100];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
return 2;
return 0;
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
{
while((x = pop()) != '(')
else
printf("%c ",pop());
push(*e);
e++;
while(top != -1)
printf("%c ",pop());
}return 0;
Output:
Program: Applications of Stack ADT.
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
}
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
while(choice!=4);
return 0;
}
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
Output:
Program: Implement Priority Queue ADT using array.
#include <bits/stdc++.h>
struct item {
int value;
int priority;
};
item pr[100000];
size++;
pr[size].value = value;
pr[size].priority = priority;
int peek()
if (highestPriority
== pr[i].priority
&& pr[ind].value
< pr[i].value) {
highestPriority = pr[i].priority;
ind = i;
else if (highestPriority
< pr[i].priority) {
highestPriority = pr[i].priority;
ind = i;
return ind;
void dequeue()
size--;
int main()
enqueue(10, 2);
enqueue(14, 4);
enqueue(16, 4);
enqueue(12, 3);
dequeue();
ind = peek();
dequeue();
ind = peek();
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
//addNode() will add a new node to the list
void addNode(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of singly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
display();
return 0;
}
Output:
Program: Implement Binary Search Tree ADT using Linked List
#include <stdio.h>
#include <malloc.h>
struct node {
struct node * left;
char data;
struct node * right;
};
struct node *constructTree( int );
void inorder(struct node *);
char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };
int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };
int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };
void main() {
struct node *root;
root = constructTree( 0 );
printf("In-order Traversal: \n");
inorder(root);
}
struct node * constructTree( int index ) {
struct node *temp = NULL;
if (index != -1) {
temp = (struct node *)malloc( sizeof ( struct node ) );
temp->left = constructTree( leftcount[index] );
temp->data = array[index];
temp->right = constructTree( rightcount[index] );
}
return temp;
}
void inorder( struct node *root ) {
if (root != NULL) {
inorder(root->left);
printf("%c\t", root->data);
inorder(root->right);
}
}
Output: