DS Lab Manual Final
DS Lab Manual Final
BHAGWANPUR, VAISHALI
LIST OF EXPERIMENTS:
4. Implement a singly linked list of integers using C/C++ program that uses
functions for following menu driven program:
1. Insert at beginning
2. Insert at end
3. Insert at any given position
4. Deletion at begining
5. Deletion at end
6. Deletion at any given position
7. Display the list
C PROGRAM:
#include <stdio.h>
/* Function Definition */
void searchKeyIn2D(int *p, int k, int r, int c){
int i, j, flag=0;
if (flag == 0){
printf("\nSearch Key = %d is not found in the given 2D
array\n",k);
}
}
void main(){
C PROGRAM:
void main(){
do{
printf("\n 1. Push\n 2. Pop\n 3. Exit\n");
printf("\n Enter the operation you want to perform:\t ");
scanf("%d",&ch);
//PUSH OPERATION
if(ch == 1){
st[top] = key;
top++;
//printf("\nAfter insertion..\t");
}
display(st, top);
}
//END OF PUSH
//POP OPERATION
else if(ch == 2){
} //End of POP
else{
printf("\nWrong Choice..\n");
}
int i;
printf(" Status of Stack: \n");
printf("|\n|
");
for(i = 0; i < n; i++){
printf(" %d |", *a);
a++;
}
printf(" < TOP \n");
printf("|
\n");
}
OUTPUT
ship@shipLatitudeE5420:~/_MyDrive/Programming_Exp/C_Exp/4th_Sem$
gcc w stack.c
ship@shipLatitudeE5420:~/_MyDrive/Programming_Exp/C_Exp/4th_Sem$
./a.out
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 1
Enter the value you want to insert: 10
Status of Stack:
|
| 10 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 1
Enter the value you want to insert: 30
Status of Stack:
|
| 10 | 30 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 20
Wrong Choice..
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 1
Enter the value you want to insert: 20
Status of Stack:
|
| 10 | 30 | 20 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 1
Enter the value you want to insert: 70
Status of Stack:
|
| 10 | 30 | 20 | 70 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 1
Enter the value you want to insert: 50
Status of Stack:
|
| 10 | 30 | 20 | 70 | 50 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 1
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 2
Status of Stack:
|
| 10 | 30 | 20 | 70 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform:
2
Status of Stack:
|
| 10 | 30 | 20 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 2
Status of Stack:
|
| 10 | 30 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 2
Status of Stack:
|
| 10 | < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 2
Status of Stack:
|
| < TOP
|
do you want more? (Y/N) : y
1. Push
2. Pop
3. Exit
Enter the operation you want to perform: 2
OVERFLOW
POP
UNDERFLOW
EXPERIMENT - 3
C PROGRAM:
//3. Write a C/C++ program to implement Queue using an array.
#define Max 5
int a[Max];
int front, rear;
void main(){
int i, key, opt, ret;
char ch;
rear = front = 1;
do{
printf("\n===========================================\n");
printf(" 1. Insert in queue 'Enqueue'\n");
printf(" 2. Delete from queue 'Dequeue'\n");
printf(" 3. Display Queue \n");
printf(" 4. Exit");
printf("\n===========================================\n");
switch(opt){
default:
printf("Wrong choice..\n");
} // End of switch
}while(1);
} // End of main
int r, i;
}
else if (front == 0 && rear < Max){
a[rear] = key;
rear ++;
r = 1;
}
else if (front > 0 && rear <= (Max)){
for (i = 0; i < (rearfront); i++){
a[i] = a[front+i];
}
rear = rear front;
front = 0;
a[rear] = key;
rear ++;
r = 1;
}
return r;
} // End of insQue
/* Function definition of deQue */
int deQue(){
int r;
if (front == 1){
printf("\n \t *** Queue is underflow.. Deletion is not
possible.. *** \n");
r = 0;
}
else {
front = front + 1;
if (front > rear){
front = 1;
rear = 1;
}
r = 1;
}
return r;
} // End of deQue
int i;
if (front == 1 && rear == 1){
printf(" Queue is Empty.. \n");
return;
}
else{
printf(" Queue Elements are.. \n");
printf("
\n")
;
printf(" \t FRONT > ");
} // End of display
OUTPUT
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 1
Enter the element you want to insert in the queue: 10
Empty queue 1st insertion..
Queue Elements are..
FRONT > | 10 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 1
Enter the element you want to insert in the queue: 20
Queue Elements are..
FRONT > | 10 | 20 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 1
Enter the element you want to insert in the queue: 30
Queue Elements are..
FRONT > | 10 | 20 | 30 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
Queue Elements are..
FRONT > | 20 | 30 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 1
Enter the element you want to insert in the queue: 80
Queue Elements are..
FRONT > | 20 | 30 | 80 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 1
Enter the element you want to insert in the queue: 78
Queue Elements are..
FRONT > | 20 | 30 | 80 | 78 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 1
Enter the element you want to insert in the queue: 11
Queue Elements are..
FRONT > | 20 | 30 | 80 | 78 | 11 | <
REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 1
Enter the element you want to insert in the queue: 89
*** Queue Overflow.. Insertion not possible.. ***
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
Queue Elements are..
FRONT > | 30 | 80 | 78 | 11 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
Queue Elements are..
FRONT > | 80 | 78 | 11 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
Queue Elements are..
FRONT > | 78 | 11 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
Queue Elements are..
FRONT > | 11 | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
Queue Elements are..
FRONT > | < REAR
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
Queue is Empty..
===========================================
1. Insert in queue 'Enqueue'
2. Delete from queue 'Dequeue'
3. Display Queue
4. Exit
===========================================
Enter your choice: 2
*** Queue is underflow.. Deletion is not possible.. ***
Queue is Empty..
OUTPUT SNAPSHOT
INSERTION [ENQUEUE]
OVERFLOW
DELETION [DEQUEUE]
UNDERFLOW
EXPERIMENT - 4
Ques: Implement a singly linked list of integers using C/C++ program that uses
functions for following menu driven program:
1. Insert at beginning
2. Insert at end
3. Insert at any given position
4. Deletion at begining
5. Deletion at end
6. Deletion at any given position
C PROGRAM:
start = NULL;
int key, opt, pos, ret;
char ch;
do{
/* displaying menu */
printf("\n***********************************");
printf("\n1. Insert at starting position ");
printf("\n2. Insert at last position ");
printf("\n3. Insert at any given position");
printf("\n4. Delete 1st node");
printf("\n5. Delete last node");
printf("\n6. Delete node, whose position is entered by
user");
printf("\n***********************************");
break;
default: /* No choice matched */
printf("\n Wrong choice..");
} //End of switch
/* Insert at starting */
void insert_first(int key){
/* new node is created */
temp = (struct node*) malloc(sizeof(struct node));
temp>data = key;
temp>next = NULL;
temp>next = start;
start = temp;
return;
} // End of insert_first()
/* Insert at last */
void insert_last (int key){
/* new node is created */
temp = (struct node*) malloc(sizeof(struct node));
temp>data = key;
temp>next = NULL;
/* Wrong choice */
if(pos <=0 || (pos > 1 && start==NULL)){
printf("\n Wrong input for position..\n");
ret = 0;
}
/* pos =1 */
if(pos == 1){
temp>next = start;
start = temp;
ret = 1;
}
if(count == pos){
temp>next = t;
prev>next = temp;
ret = 1;
}
else{
printf("\n Wrong input..");
ret = 0;
}
return ret;
} // End of insert_pos
if(start == NULL){
printf("\n List is Empty.. \n");
}
else{
temp = start;
start = start>next;
free(temp);
}
} // End of delete_last
if(count == pos){
t1>next = temp >next;
free(temp);
ret = 1;
}
else {
printf("\n position entered is not valid..");
ret = 0;
}
}
return ret;
} // End of delete_pos
/* display function */
void display (){
} // End of display
OUTPUT SNAPSHOT
st
INSERTION AT 1 POSITION
C PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct tnode{
struct tnode *lchild;
int data;
struct tnode *rchild;
};
p>data = val;
p>lchild = p>rchild = NULL;
}
else{
temp1 = p;
else
temp1 = temp1>rchild;
}
void main(){
struct tnode *root = NULL;
int n, x;
printf("Enter the number of nodes: \t");
scanf("%d", &n);
while( n > 0){
printf("Enter the data value: \t");
scanf("%d", &x);
root = insert(root, x);
n;
}
printf("Tree elements are:\n");
inorder(root);
printf("\n");
}
OUTPUT
EXPERIMENT - 6
Ques: Write a program to implement Quick sort using the concept of recursion
on a list provided by user.
C PROGRAM:
//Function declaration
void qSort (int a[], int, int);
int partition (int a[], int, int );
//Quick SORT
qSort(a, 0, n1);
printf("\n\nThe sorted list is: \n");
for(i=0; i<n; i++){
printf("%d \t", a[i]);
}
}
a[low] = a[right];
a[right] = pivotItem;
printf("\n\nThe list is : \t");
return right;
} // end of partitioning
OUTPUT