Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
55 views

Program No - 1: Aim: Implementing Stacks and Queues Using Arrays

The document describes algorithms for implementing stacks, queues, and circular queues using arrays. It provides pseudocode for push, pop, insert, and delete operations on stacks and queues. The program section implements these operations on stacks and queues using arrays in C programming language. It includes functions for push, pop, display on stacks and enqueue, dequeue, display on queues. The main function runs a menu loop to call these functions and test the implementations.

Uploaded by

Achint Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Program No - 1: Aim: Implementing Stacks and Queues Using Arrays

The document describes algorithms for implementing stacks, queues, and circular queues using arrays. It provides pseudocode for push, pop, insert, and delete operations on stacks and queues. The program section implements these operations on stacks and queues using arrays in C programming language. It includes functions for push, pop, display on stacks and enqueue, dequeue, display on queues. The main function runs a menu loop to call these functions and test the implementations.

Uploaded by

Achint Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 17

PRACTICAL FILE

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 queue[SIZE], front = -1, rear = -1;

int value;

void push(void);

void pop(void);

void display(void);

voidenQueue(int);

voiddeQueue();

void display1();

void push()

if(top>=n-1)

printf("\nSTACK is over flow”);

else{

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

scanf("%d",&x);

top++;

stack[top]=x;

}}

void pop(){

if(top<=-1)

printf("\n Stack is under flow");

else

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

top--;

2
PRACTICAL FILE
}

void display()

if(top>=0)

printf("\n The elements in STACK \n");

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

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

else

printf("\n The STACK is empty");

voidenQueue(int value){

if(rear == SIZE-1)

printf("\nQueue is Full!!! Insertion is not possible!!!");

else{

if(front == -1)

front = 0;

rear++;

queue[rear] = value;

voiddeQueue()

if(front == rear)

printf("\nQueue is Empty!!! Deletion is not possible!!!");

else{

printf("\nDeleted : %d", queue[front]);

3
PRACTICAL FILE
front++;

if(front == rear)

front = rear = -1;

void display1(){

if(rear == -1)

printf("\nQueue is Empty!!!");

else{

inti;

printf("\nQueue elements are:\n");

for(i=front; i<=rear; i++)

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

void main()

printf("\n\t\t\t IMPLEMENTING STACKS AND QUEUES USING ARRAYS");

printf("\n 1.IMPLEMENT STACKS ");

printf("\n 2.IMPLEMENT QUEUES");

do{

printf("\n Enter the choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("\n Enter the size of STACK[MAX=100]:");

scanf("%d",&n);

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

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

4
PRACTICAL FILE
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do

printf("\n Enter the Choice:");

scanf("%d",&choice);

switch(choice)

case 1:push();

break;

case 2 : pop();

break;

case 3: display();

break;

case 4: printf("\n\t EXIT POINT ");

break;

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

while(choice!=4);

break;

case 2:

printf("\n\t\ QUEUE IMPLEMTATION USING STACK");

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

printf("\n1. ENQUEUE\n2. DEQUEUE\n3. DISPLAY\n4. EXIT");

do

printf("\nEnter your choice: ");

scanf("%d",&choice);

5
PRACTICAL FILE
switch(choice){

case 1: printf("\nEnter the value to be inserted: ");

scanf("%d",&value);

enQueue(value);

break;

case 2: deQueue();

break;

case 3: display1();

break;

case 4 : exit(0);

default:printf("\nWrong selection!!! Try again!!!");

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];

int front = -1;

int rear = -1;

void insert(int item)

if((front == 0 && rear == MAX-1) || (front == rear+1))

{ printf("Queue Overflow \n");


9
PRACTICAL FILE
return;}

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 ;

printf("Element deleted from queue is : %d\n",cqueue_arr[front]);

if(front == rear)

front = -1;

rear=-1;

else

if(front == MAX-1)

front = 0;

10
PRACTICAL FILE
else

front = front+1;

void display()

int front_pos = front,rear_pos = rear;

if(front == -1)

printf("Queue is empty\n");

return;

printf("Queue elements :\n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos)

printf("%d\t\t ",cqueue_arr[front_pos]);

front_pos++;

else

while(front_pos <= MAX-1)

printf("%d ",cqueue_arr[front_pos]);

front_pos++;

front_pos = 0;

while(front_pos <= rear_pos)

printf("%d ",cqueue_arr[front_pos]);

11
PRACTICAL FILE
front_pos++;

printf("\n");

int main()

int choice,item;

printf("\n\t\t\t IMPLEMENTING CIRCULAR QUEUES USING ARRAYS ");

printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");

do

printf("\nEnter your choice : ");

scanf("%d",&choice);

switch(choice)

case 1 :

printf("\nInput the element for insertion in queue : ");

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

You might also like