Program No - 1: Aim: Implementing Stacks and Queues Using Arrays
Program No - 1: Aim: Implementing Stacks and Queues Using Arrays
Program no -1
Aim : Implementing Stacks and queues using arrays
ALGORITHM
Push ( ): Description: Here STACK is an array with MAX locations. TOP points to the top most
element and ITEM is the value to be inserted.
1. If (TOP == MAX) Then [Check for overflow]
2. Print: Overflow
3. Else
4. Set TOP = TOP + 1 [Increment TOP by 1]
5. Set STACK[TOP] = ITEM [Assign ITEM to top of STACK]
6. Print: ITEM inserted [End of If]
7. Exit
Pop ( ): Description: Here STACK is an array with MAX locations. TOP points to the top most
element.
1. If (TOP == 0) Then [Check for underflow]
2. Print: Underflow
3. Else
4. Set ITEM = STACK[TOP] [Assign top of STACK to ITEM]
5. Set TOP = TOP - 1 [Decrement TOP by 1]
6. Print: ITEM deleted [End of If]
7. Exit
Insert ( ):Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear of the QUEUE. ITEM is the value to be inserted.
1.If (REAR == N)
2. Print: Overflow
3. Else
4. If (FRONT and REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1 (b) Set REAR = 1
5. Else
6. Set REAR = REAR + 1 [Increment REAR by 1] [End of Step 4 If]
7. QUEUE[REAR] = ITEM
8. Print: ITEM inserted [End of Step 1 If]
9. Exit
Delete ( ): Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear of the QUEUE.
1. If (FRONT == 0) Then [Check for underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [Check if only one element is left]
(a) Set FRONT = 0 (b) Set REAR = 0
6. Else
7. Set FRONT = FRONT + 1 [Increment FRONT by 1] [End of Step 5 If]
8. Print: ITEM deleted [End of Step 1 If]
9. Exit
PROGRAM
#include<stdio.h>
#define SIZE 10
1
PRACTICAL FILE
int stack[100],choice,n,top,x,i;
top=-1;
int value;
void push(void);
void pop(void);
void display(void);
voidenQueue(int);
voiddeQueue();
void display1();
void push()
if(top>=n-1)
else{
scanf("%d",&x);
top++;
stack[top]=x;
}}
void pop(){
if(top<=-1)
else
top--;
2
PRACTICAL FILE
}
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
voidenQueue(int value){
if(rear == SIZE-1)
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
voiddeQueue()
if(front == rear)
else{
3
PRACTICAL FILE
front++;
if(front == rear)
void display1(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
inti;
printf("\n%d ",queue[i]);
void main()
do{
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&n);
printf("\n\t--------------------------------");
4
PRACTICAL FILE
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
scanf("%d",&choice);
switch(choice)
case 1:push();
break;
case 2 : pop();
break;
case 3: display();
break;
break;
while(choice!=4);
break;
case 2:
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
5
PRACTICAL FILE
switch(choice){
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display1();
break;
case 4 : exit(0);
while(choice!=4);
}while(choice!=2);
OUTPUT
6
PRACTICAL FILE
7
PRACTICAL FILE
Program no-2
8
PRACTICAL FILE
Aim: Implementation of circular queues using arrays
ALGORITHM
Insert CircularQueue ( )
1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then
2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM inserted
[End of Step 1 If]
11. Exit
Delete CircularQueue ( )
1. If (FRONT == 0) Then [Check for Underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is left]
(a) Set FRONT = 0 (b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches end if QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT by 1]
[End of Step 5 If]
10. Print: ITEM deleted
[End of Step 1 If]
11. Exit
___________________________________________________________________________
PROGRAM
# include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
if (front == -1){
front = rear=0;
else
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
cqueue_arr[rear] = item ;
void del()
if (front == -1)
printf("Queue Underflow\n");
return ;
if(front == rear)
front = -1;
rear=-1;
else
if(front == MAX-1)
front = 0;
10
PRACTICAL FILE
else
front = front+1;
void display()
if(front == -1)
printf("Queue is empty\n");
return;
printf("%d\t\t ",cqueue_arr[front_pos]);
front_pos++;
else
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
front_pos = 0;
printf("%d ",cqueue_arr[front_pos]);
11
PRACTICAL FILE
front_pos++;
printf("\n");
int main()
int choice,item;
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
do
scanf("%d",&choice);
switch(choice)
case 1 :
scanf("%d", &item);
insert(item);
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(0);
12
PRACTICAL FILE
default:
printf("Wrong choice\n");
}while(choice!=4);
return 0;
Program No 3
Aim – Write A program to implement sparse matrix & check whether it is sparse or not.
ALGORITHM
1. First create an array a [ 10 ] [ 10 ] , i ,j , m , n & the program takes the number of rows and columns of
the matrix.
2. Then the elements are entered.
3. If the matrix contains maximum number of elements as 0, then it is a sparse matrix.
4. Else not.
5. The result is printed.
6. Exit.
#include<stdio.h>
#include<conio.h>
void main()
{
static int a[10][10];
int i,j,m,n;
13
PRACTICAL FILE
int count=0;
clrscr();
printf("Enter the order of the matrix \n");
scanf("%d%d",&m,&n);
printf("Enter the co-effients of the matrix \n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&a [ i ] [ j ] );
if(a [ i ] [ j ] ==0)
{
++count;
}
}
}
printf("\n Matrix is :-\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
if(count > ((m*n)/2))
{
printf("The given matrix is sparse matrix \n");
}
else
{
printf("The given matrix is not a sparse matrix \n");
}
printf("There are %d number of zeros",count);
getch();
}
14
PRACTICAL FILE
15
PRACTICAL FILE
OUTPUT
Enter the order of the matrix
3
3
Enter the co-effients of the matrix
1
2
0
3
6
7
0
2
7
Matrix is :-
1 2 0
3 6 7
0 2 7
The given matrix is not a sparse matrix
There are 2 number of zeros
Program No. 4
Create Singly linked list and traverse that list
16
PRACTICAL FILE
Algorithm:
1. Start
2. By definifing structure we can define a node in which value is the
data that want to store and *next is pointer of structure struct
node
{
int data;
struct node *next;
}*head;
3. Let n be number of nodes to be inputtes by user.
4. For insertion :
a. if(n>0)
then head = (struct node *)malloc(sizeof(struct node));
b. if (head = =NULL)
then print error message for not allocating memory
c. else
input data from user head ->value=data
and points heads to null by head->next=NULL
copy head to other pointer temp=head
d. Allocate memory and assign memory location
by newNode=(struct node*)malloc(sizeof()node)
e. newNode ->data =data
newNode->next =NULL
temp->next = newNode;
temp=temp->next;
f. Repeat for n-2 other nodes
5. For display:
a. If(head = =NULL)
a) print list is empty
b. While(temp!= NULL)
print temp->data
temp=temp->next
17