0% found this document useful (0 votes)
9 views34 pages

DS Lab Manual Final

The document outlines a series of programming experiments for a B.Tech course in Data Structures, focusing on C/C++ implementations. It includes tasks such as searching in a 2D array, implementing a stack and a queue using arrays, and creating a singly linked list and a binary search tree. Each experiment is accompanied by sample code and expected outputs.

Uploaded by

Ghana Uni Help
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views34 pages

DS Lab Manual Final

The document outlines a series of programming experiments for a B.Tech course in Data Structures, focusing on C/C++ implementations. It includes tasks such as searching in a 2D array, implementing a stack and a queue using arrays, and creating a singly linked list and a binary search tree. Each experiment is accompanied by sample code and expected outputs.

Uploaded by

Ghana Uni Help
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

PATNA SAHIB GROUP OF COLLEGES

BHAGWANPUR, VAISHALI

COURSE: B.Tech. BRANCH: CSE SEM: 4th

SUB: Data Structure (051403)

LIST OF EXPERIMENTS:

1. Write a program in C/C++ for searching an element in 2D array (matrix)


using function.

2. Implementing a stack using array in C/C++.

3. Write a C/C++ program to implement Queue using an array.

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

5. Implement Binary Search Tree using C/C++.

6. Write a program to implement Quick sort using the concept of recursion


on a list provided by user.

SOFTWARE TOOL: C/C++ COMPILER

FACULTY NAME: SHIPRA SWATI


EXPERIMENT - 1

Ques: Write a program in C/C++ for searching an element in 2D array (matrix)


using function.

C PROGRAM:

/* Write a program in C/C++ for searching an element in 2D array


(matrix) using function. */

#include <stdio.h>

/* Function Definition */
void searchKeyIn2D(int *p, int k, int r, int c){
int i, j, flag=0;

/* Display the 2D array */


printf("The 2D array: \n");
for(i=0; i<r; i++){
for(j=0; j<c; j++){
printf("%d \t", (*p+i*c+j));
}
printf("\n");
}

/* Searching the entered key */


for(i=0; i<r; i++){
for(j=0; j<c; j++){
if (k == (*p+i*c+j)){
printf("\nSearch Key = %d is found at row = %d
and col = %d \n", k, i+1, j+1);
flag = 1;
break;
}
}
}

if (flag == 0){
printf("\nSearch Key = %d is not found in the given 2D
array\n",k);
}
}
void main(){

int arr[50][50], i, j, row, col, key, flag = 0;

printf("\nEnter the number of rows and columns for 2D


array:\t");
scanf("%d %d", &row, &col);

printf("Enter the elements of the 2D array:\t");

/* Enter the 2D array Elements */


for(i=0; i<row; i++){
for(j=0; j<col; j++){
scanf("%d", &arr[i][j]);
}
}

/* Display the 2D array */


printf("The 2D array: \n");
for(i=0; i<row; i++){
for(j=0; j<col; j++){
printf("%d \t", arr[i][j]);
//printf("%d \t", *(*(arr+i) + j));
//printf("%d \t", *(arr[i] + j));
//printf("%d \t", (*(arr + i))[j]);
}
printf("\n");
}

/* ask user to enter the search key */


printf("Enter the key you want to search: \t");
scanf("%d", &key);

/* Call the function to search elements linearly */


searchKeyIn2D(arr, key, row, col);
}
OUTPUT
EXPERIMENT - 2

Ques: Implementing a stack using array in C/C++.

C PROGRAM:

void main(){

int st[5], top = 0, i, ch, key, max = 5;


char opt;

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){

//Error condition check


if (top >= max){
printf("\n *** OverFlow.. ***\n");
continue;
}
else{
printf("Enter the value you want to insert:\t");
scanf("%d", &key);

st[top] = key;

top++;
//printf("\nAfter insertion..\t");
}

display(st, top);
}
//END OF PUSH

//POP OPERATION
else if(ch == 2){

//Error condition ­­ underflow


if(top == 0){
printf("\n *** Underflow.. *** \n");
}
else{
top­­;
display(st, top);
}

} //End of POP
else{
printf("\nWrong Choice..\n");
}

printf("do you want more? (Y/N) : \t");


getchar();
scanf("%c", &opt);

}while (opt == 'y');

void display (int *a, int 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@ship­Latitude­E5420:~/_MyDrive/Programming_Exp/C_Exp/4th_Sem$
gcc ­w stack.c
ship@ship­Latitude­E5420:~/_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

*** OverFlow.. ***

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

*** Underflow.. ***


do you want more? (Y/N) :
OUTPUT SNAPSHOT
PUSH

OVERFLOW

POP

UNDERFLOW
EXPERIMENT - 3

Ques: Write a C/C++ program to implement Queue using an array.

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");

printf("Enter your choice: \t");


scanf("%d", &opt);

switch(opt){

case 1: /* Enqueue­ insert in queue */


printf(" Enter the element you want to insert in
the queue: \t");
scanf("%d", &key);
ret = insQue(key);
if (ret ==1 )
display();
break;

case 2: /* Dequeue­ delete from queue */


deQue();
if (ret = 1)
display();
break;

case 3: /* Display the queue element */


display();
break;

case 4: /* Exit the menu */


exit(0);
break;

default:
printf("Wrong choice..\n");

} // End of switch
}while(1);

} // End of main

/* Function definition of insQue */


int insQue(int key){

int r, i;

if (front == 0 && rear == Max){

printf("\n \t *** Queue Overflow.. Insertion not


possible.. *** \n");
r = 0;
return r;
}
if (front == ­1 && rear == ­1){
//Queue is empty ­­ First time insertion
printf(" Empty queue ­­ 1st insertion.. \n");
front = rear = 0;
a[rear] = key;
rear++;
r = 1;

}
else if (front == 0 && rear < Max){
a[rear] = key;
rear ++;
r = 1;
}
else if (front > 0 && rear <= (Max)){
for (i = 0; i < (rear­front); 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

/* Function definition of display */


void display () {

int i;
if (front == ­1 && rear == ­1){
printf(" Queue is Empty.. \n");
return;
}

else{
printf(" Queue Elements are.. \n");
printf("
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­\n")
;
printf(" \t FRONT ­­­> ");

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


printf("| %d ", a[i]);
}
printf(" | <­­­ REAR \n");
printf("
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­\n")
;
}

} // 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:

/* Program of Linked List*/


#include<stdlib.h>

/* Creating node structure for linked list */


struct node{
int data;
struct node *next;
};
struct node *start, *temp;
void main(){

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***********************************");

printf("\n Enter your choice:\t \t \t");


scanf("%d", &opt);
switch(opt){

case 1: /* insert at starting */


printf(" Enter the element you want to
insert:\t ");
scanf("%d", &key);
insert_first(key);
break;

case 2: /* insert at last */


printf(" Enter the element you want to
insert:\t ");
scanf("%d", &key);
insert_last(key);
break;

case 3: /* insert at a given position */


printf(" Enter the element you want to insert:\t
");
scanf("%d", &key);
printf(" Enter the position, where you want
to enter the elements.. ");
scanf("%d", &pos);
ret = insert_pos(key, pos);
break;

case 4: /* delete 1st node */


delete_first();
break;

case 5: /* delete Last node */


delete_last();
break;

case 6: /* delete node, whose position is entered by


user */
printf(" Enter the position from where you want
to delete the elements.. ");
scanf("%d", &pos);
ret = delete_pos(pos);
if (ret==1)
printf(" Deletion Successful.. ");
else
printf(" No deletion performed...");

break;
default: /* No choice matched */
printf("\n Wrong choice..");

} //End of switch

/* printing the list */


display();

printf("\n Do you want more (Y / N ):\t");


getchar();
scanf("%c", &ch);
}while(ch=='y'); // End of do­while

/* 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;

/* check if the list is empty */


if (start == NULL) {
printf(" ** FIRST NODE INSERTED.. **");
start = temp;
return;
}

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;

/* check if the list is empty */


if (start == NULL) {
printf(" ** FIRST NODE INSERTED.. **");
start = temp;
return;
}
struct node *t = start;
/* Loop through the list till the last element */
while(t­>next != NULL){
t = t­>next;
}
t­>next = temp;
return;
} // End of insert_last

/* inserting at given position */


int insert_pos(int key, int pos){

int count = 1, ret = 0;


struct node *prev;
/* 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;
}

struct node *t = start;


while ((count < pos) && ((t­>next) != NULL)){
prev = t;
t = t­>next;
count = count + 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

/* Delete node at 1st position*/


void delete_first(){

if(start == NULL){
printf("\n List is Empty.. \n");
}
else{
temp = start;
start = start­>next;
free(temp);
}

}// End of delete_first

/* Delete node at last position */


void delete_last(){
struct node *t1;
if(start == NULL){
printf("\n List is Empty.. \n");
}
else{
temp = start;
while(temp­>next != NULL){
t1 = temp;
temp = temp­>next;
}
t1­>next = NULL;
free(temp);
}

} // End of delete_last

/* Delete node at any position enetered by user */

int delete_pos(int pos){


int count = 1, ret;
struct node *t1;
if(start == NULL){
printf("\n List is empty..");
ret = 0;
}
else if (pos == 1){
temp = start;
start = start­>next;
free(temp);
ret = 1;
}
else{
temp = start;
while (count < pos && temp­>next != NULL){
t1 = temp;
temp = temp­>next;
count ++;
}

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 (){

printf("\n LIST DETAILS :\n


­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­\n");
printf("\tdata \t\t Current Address \t Next Address");
printf(" \n
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­­­­­­\n");
temp = start;
while(temp­>next != NULL){
printf(" \t %d \t\t %u \t\t %u \n", temp­>data,
temp, temp­>next);
temp=temp­>next;
}
printf(" \t %d \t\t %u \t\t %u \n", temp­>data,
temp, temp­>next);

} // End of display
OUTPUT SNAPSHOT
st
INSERTION AT 1 POSITION

INSERTION AT LAST POSITION


INSERTION AT SPECIFIC POSITION

DELETION OF 1st NODE


DELETION OF LAST NODE

DELETION OF NODE AT SPECIFIC POSITION


EXPERIMENT - 5

Ques: Implement Binary Search Tree using C/C++.

C PROGRAM:

// 5. Implement Binary Search Tree using C/C++.

#include <stdio.h>
#include <stdlib.h>
struct tnode{
struct tnode *lchild;
int data;
struct tnode *rchild;
};

struct tnode *insert(struct tnode *p, int val){


struct tnode *temp1,*temp2;
if(p == NULL){
p = (struct tnode *) malloc(sizeof(struct tnode));
/* insert the new node as root node*/
if(p == NULL){
printf("Cannot allocate\n");
exit(0);
}

p­>data = val;
p­>lchild = p­>rchild = NULL;
}
else{
temp1 = p;

/* traverse the tree to get a pointer to that node whose


child will be the newly created node */
while(temp1 != NULL){
temp2 = temp1;
if( temp1 ­>data > val)
temp1 = temp1­>lchild;

else
temp1 = temp1­>rchild;
}

if( temp2­>data > val){


temp2­>lchild = (struct tnode*)malloc(sizeof(struct
tnode));
/* inserts the newly created node as left child */
temp2 = temp2­>lchild;
if(temp2 == NULL){
printf("Cannot allocate\n");
exit(0);
}
temp2­>data = val;
temp2­>lchild=temp2­>rchild = NULL;
}
else{
temp2­>rchild = (struct tnode*)malloc(sizeof(struct
tnode));

/*inserts the newly created node as left child*/


temp2 = temp2­>rchild;
if(temp2 == NULL){
printf("Cannot allocate\n");
exit(0);
}
temp2­>data = val;
temp2­>lchild=temp2­>rchild = NULL;
}
}
return(p);
}

/* a function to binary tree in inorder */


void inorder(struct tnode *p){
if(p != NULL){
inorder(p­>lchild);
printf("%d\t",p­>data);
inorder(p­>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:

// C program for Quick Sort

//Function declaration
void qSort (int a[], int, int);
int partition (int a[], int, int );

void main (void){


int a[20], n, i, j, temp;
printf("\n Enter the dimension of list: \n");
scanf("%d", &n);
printf("\n Enter the elements of list: \n");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}

//Quick SORT
qSort(a, 0, n­1);
printf("\n\nThe sorted list is: \n");
for(i=0; i<n; i++){
printf("%d \t", a[i]);
}
}

/*Function definition of qSort*/


void qSort(int a[], int low, int high){
int pivot, i;
printf("\n\nThe list is : \t");
for(i=0; i<=high; i++){
printf("%d \t", a[i]);
}
if(low < high){
pivot = partition (a, low, high);
qSort(a, low, pivot­1); // RECURSION
qSort(a, pivot+1, high); // RECURSION
}
} // qSort ended
/* Function definition of partitioning */
int partition (int a[], int low, int high){
int left, right, pivotItem, pivot, temp, i;
pivot = low;
pivotItem = a[low];
left = low;
right = high;
printf("\nLow: %d \t High: %d \t Pivot: %d", low,
high, right);

while (left < right){


while (a[left] <= pivotItem){
left++;
}

while (a[right] > pivotItem){


right­­;
}

if (left < right){


//swap
temp = a[left];
a[left] = a[right];
a[right] = temp;
}
}// while loop ends

a[low] = a[right];
a[right] = pivotItem;
printf("\n\nThe list is : \t");

for(i=0; i<=high; i++){


printf("%d \t", a[i]);
}

printf("\nLow: %d \t High: %d \t Pivot: %d", low,


high, right);

return right;

} // end of partitioning
OUTPUT

You might also like