0% found this document useful (0 votes)
18 views

Data Structure 01

The document describes code for implementing a list data structure using arrays and linked lists. For arrays, it includes functions to create, insert, delete, and display list elements. For linked lists, it provides function prototypes to create, display, modify, and delete nodes in a linked list. The output shows sample input/output of running the array list code, creating a list of 3 elements and performing insertions, deletions, and displays on the list.

Uploaded by

123sanjaypurohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Data Structure 01

The document describes code for implementing a list data structure using arrays and linked lists. For arrays, it includes functions to create, insert, delete, and display list elements. For linked lists, it provides function prototypes to create, display, modify, and delete nodes in a linked list. The output shows sample input/output of running the array list code, creating a list of 3 elements and performing insertions, deletions, and displays on the list.

Uploaded by

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

Ex 1 : ARRAY IMPLEMENTATION OF LIST ADT {

case 1:
SOURCE CODE: create();
break;
#include<stdio.h> case 2:
#include<conio.h> insert();
break;
int a[20], n, i, j, ch, pos, elem; case 3:
del();
void create(); break;
void insert(); case 4:
void del(); display();
void display(); break;
void insertend(); case 5:
void insertatanypos(); printf("\nEnd");
void delend(); getch();
void delanypos(); break;
void delanyele(); }
} while (ch < 5);
void main() }
{
clrscr(); void create()
do {
{ printf("\nEnter the size of the list:\t");
printf("\nMain menu"); scanf("%d", &n);
printf("\n**********"); printf("Enter the elements in the list:\t");
printf("\n1. Create");
printf("\n2. Insert"); for(i = 0; i < n; i++)
printf("\n3. Delete"); {
printf("\n4. Display"); scanf("%d", &a[i]);
printf("\n5. Exit"); }
printf("\nEnter the choice:\t"); }
scanf("%d", &ch);
void display()
switch(ch) {

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


printf("\nThe elements in the list are: "); {
for(i = 0; i < n; i++) int temp;
{ n = n + 1;
printf("%d ", a[i]); printf("\nEnter the position to be inserted:\t");
} scanf("%d", &pos);
} printf("\nEnter the element to be inserted:\t");
scanf("%d", &elem);
void insert() for(j = n-1; j >= pos-1; j--)
{ {
printf("\n1. Insert at the end"); a[j] = a[j-1];
printf("\n2. Insert at any pos"); }
printf("\nEnter your choice:\t"); a[pos-1] = elem;
scanf("%d", &ch); display();
}
switch(ch)
{ void del()
case 1: {
insertend(); printf("\n1. Delete at the end");
break; printf("\n2. Delete at any pos");
case 2: printf("\n3. Delete any given element");
insertatanypos(); printf("\nEnter your choice:\t");
break; scanf("%d", &ch);
}
} switch(ch)
{
void insertend() case 1:
{ delend();
n = n + 1; break;
printf("\nEnter the element to be inserted:\t"); case 2:
scanf("%d", &elem); delanypos();
a[n-1] = elem; break;
display(); case 3:
} delanyele();
break;
void insertatanypos() }

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


} for(j = pos; j < n; j++)
{
void delend() a[j] = a[j+1];
{ }
elem = a[n-1]; }
printf("\nThe deleted element is %d", elem); }
n = n - 1; n = n - 1;
display(); display();
} }

void delanypos() OUTPUT:


{
printf("\nEnter the position to be deleted:\t"); Main menu
scanf("%d", &pos); **********
printf("\nThe deleted element is %d", a[pos-1]); 1. Create
2. Insert
for(j = pos-1; j < n; j++) 3. Delete
{ 4. Display
a[j] = a[j+1]; 5. Exit
}
n = n - 1; Enter your choice: 1
display();
} Enter the size of the list: 3
Enter the elements in the list: 5 6 9
void delanyele()
{ Main menu
display(); **********
printf("\nEnter the element to be deleted:\t"); 1. Create
scanf("%d", &elem); 2. Insert
3. Delete
for(i = 0; i < n; i++) 4. Display
{ 5. Exit
if(a[i] == elem)
{ Enter your choice: 2
pos = i; 1. Insert at the end

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


2. Insert at any pos
Enter the choice: 1 Enter your choice: 4
The elements in the list are: 5 6 7
Enter the element to be inserted: 7
Main menu
Main menu **********
********** 1. Create
1. Create 2. Insert
2. Insert 3. Delete
3. Delete 4. Display
4. Display 5. Exit
5. Exit
Enter your choice: 5
Enter your choice: 4 End
The elements in the list are: 5 6 9 7
EX 2 : LINKED LIST IMPLEMENTATION OF LIST ADT
Main menu
********** SOURCE CODE:
1. Create
2. Insert #include <stdio.h>
3. Delete #include <conio.h>
4. Display #include <stdlib.h>
5. Exit
struct node {
Enter your choice: 3 int info;
Enter the position to be deleted: 3 struct node *next;
The deleted element is 9 };

Main menu typedef struct node NODE;


**********
1. Create void create();
2. Insert void display();
3. Delete void modify();
4. Display void del();
5. Exit

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


NODE *head = NULL;
void create() {
void main() { NODE *temp, *t, *prev;
int ch; int i, j, ch, d, pos, n;
clrscr(); printf("\nEnter the size of elements:\t");
do { scanf("%d", &n);
clrscr(); for (i = 0; i < n; i++) {
printf("\nMain menu\n"); clrscr();
printf("************\n"); printf("\nEnter the element:\t");
printf("1. Creation\n"); scanf("%d", &d);
printf("2. Modification\n"); temp = (NODE *)malloc(sizeof(NODE));
printf("3. Deletion\n"); temp->next = NULL;
printf("4. Display\n"); temp->info = d;
printf("5. Exit\n"); if (head == NULL)
printf("Enter your choice:\t"); head = temp;
scanf("%d", &ch); else {
switch (ch) { printf("\n1. Insert at the beginning");
case 1: printf("\n2. Insert at the mid");
create(); printf("\n3. Insert at the end");
break; printf("\nEnter the choice:\t");
case 2: scanf("%d", &ch);
modify(); switch (ch) {
break; case 1:
case 3: temp->next = head;
del(); head = temp;
break; break;
case 4: case 2:
display(); t = head;
break; printf("\nEnter the position to insert:\t");
case 5: scanf("%d", &pos);
printf("\nBye"); for (j = 1; j < pos; j++) {
getch(); prev = t;
} t = t->next;
} while (ch < 5); }
} temp->next = prev->next;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


prev->next = temp;
break; void del() {
case 3: NODE *t, *prev;
t = head; int pos, ch, i;
while (t->next != NULL) t = head;
t = t->next; printf("\n1. Delete at beginning");
t->next = temp; printf("\n2. Delete at the mid");
break; printf("\n3. Delete at the end");
} printf("\nEnter the choice:\t");
printf("\nThe current list:\t"); scanf("%d", &ch);
display(); t = head;
} switch (ch) {
} case 1:
} head = head->next;
free(t);
void modify() { break;
NODE *t; case 2:
int old, new; printf("\nEnter the position to delete:\t");
t = head; scanf("%d", &pos);
printf("\nThe current list:\t"); for (i = 1; i < pos; i++) {
display(); prev = t;
printf("\nEnter the item to be modified:\t"); t = t->next;
scanf("%d", &old); }
while (t != NULL) { prev->next = t->next;
if (t->info == old) { free(t);
printf("\nEnter the new value:\t"); break;
scanf("%d", &new); case 3:
t->info = new; while (t->next != 0) {
} else { prev = t;
t = t->next; t = t->next;
} }
} prev->next = NULL;
printf("\nAfter modification:\n"); free(t);
display(); break;
} }

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


printf("\nAfter deletion:\t"); Enter the choice: 3
display();
} 1. Insert at beginning
2. Insert at mid
void display() { 3. Insert at end
NODE *t; Enter the choice: 3
t = head;
if (t == NULL) Enter the element to be inserted: 20
printf("\nList is empty\n");
else 1. Insert at beginning
while (t->next != NULL) { 2. Insert at mid
printf("%d ", t->info); 3. Insert at end
t = t->next; Enter the choice: 3
}
} The current list: 10 15 20

OUTPUT: MAIN MENU


***********
MAIN MENU 1. Creation
*********** 2. Modification
1. Creation 3. Deletion
2. Modification 4. Display
3. Deletion 5. Exit
4. Display
5. Exit Enter your choice: 3
1. Delete at beginning
Enter your choice: 2. Delete at mid
Enter the size of elements: 3 3. Delete at end
Enter the elements: 10 Enter the choice: 3
Enter the elements: 15
Enter the elements: 20 After deletion: 10 15

1. Insert at beginning MAIN MENU


2. Insert at mid ***********
3. Insert at end 1. Creation

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


2. Modification printf("\nMain Menu:");
3. Deletion printf("\n**********");
4. Display printf("\n1. Push");
5. Exit printf("\n2. Pop");
printf("\n3. Display");
Enter your choice: 4 printf("\n4. Exit");
10 15 printf("\nEnter your choice: ");
scanf("%d", &ch);
MAIN MENU printf("\n\n");
***********
1. Creation switch (ch) {
2. Modification case 1:
3. Deletion if (top >= n - 1) {
4. Display printf("\nStack Overflow");
5. Exit getch();
break;
Enter your choice: 5 }
printf("Enter the item: ");
scanf("%d", &item);
EX 3 : ARRAY IMPLEMENTATION OF STACK ADT top++;
SOURCE CODE: stack[top] = item;
break;
#include<stdio.h>
#include<conio.h> case 2:
if (top < 0) {
#define size 100 printf("\nStack Underflow");
getch();
void main() { break;
int top = -1, i, stack[size], n, ch, item; }
clrscr(); item = stack[top];
top--;
printf("\nEnter the size of the Stack:"); printf("\nThe popped element is %d", item);
scanf("%d", &n); getch();
break;
do {

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


case 3: **********
if (top < 0) { 1. Push
printf("\nStack is empty"); 2. Pop
getch(); 3. Display
break; 4. Exit
} Enter your choice: 1
printf("Stack elements: ");
for (i = top; i >= 0; i--) Enter the item: 20
printf("%d ", stack[i]);
getch(); Main Menu:
break; **********
1. Push
case 4: 2. Pop
printf("\nExiting..."); 3. Display
getch(); 4. Exit
break; Enter your choice: 3
}
} while (ch < 4); Stack elements: 20 10
} Main Menu:
**********
OUTPUT: 1. Push
2. Pop
Enter the size of the Stack: 5 3. Display
4. Exit
Main Menu: Enter your choice: 2
**********
1. Push The popped element is 20
2. Pop
3. Display Main Menu:
4. Exit **********
Enter your choice: 1 1. Push
2. Pop
Enter the item: 10 3. Display
4. Exit
Main Menu: Enter your choice: 3

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


clrscr();
Stack elements: 10 do
Main Menu: {
********** printf("\nMain Menu:");
1. Push printf("\n**********");
2. Pop printf("\n1.Push");
3. Display printf("\n2.Pop");
4. Exit printf("\n3.Display");
Enter your choice: 4 printf("\n4.Exit");
printf("\nEnter your choice: ");
Exiting... scanf("%d", &ch);
printf("\n\n");
switch (ch)
EX 4 : LINKED LIST IMPLEMENTATION OF STACK ADT {
SOURCE CODE: case 1:
push();
#include <stdio.h> break;
#include <conio.h> case 2:
#include <stdlib.h> pop();
break;
struct node case 3:
{ display();
int info; break;
struct node *next; case 4:
} *top = NULL; printf("\nExiting...");
break;
typedef struct node STACK; }
getch();
void push(); } while (ch < 4);
void pop(); }
void display();
void push()
void main() {
{ STACK *temp;
int ch; int d;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


printf("\nEnter the element to be pushed: "); if (t == NULL)
scanf("%d", &d); printf("\nStack is empty");
temp = (STACK *)malloc(sizeof(STACK)); else
temp->next = NULL; {
temp->info = d; printf("\nStack elements:\n");
if (top == NULL) while (t != NULL)
top = temp; {
else printf("%d\n", t->info);
{ t = t->next;
temp->next = top; }
top = temp; }
} }
printf("\nStack after pushing the element:\n");
display(); OUTPUT:
} Main Menu:
**********
void pop() 1.Push
{ 2.Pop
STACK *t; 3.Display
if (top == NULL) 4.Exit
printf("\nStack Underflow"); Enter your choice: 1
else
{ Enter the element to be pushed: 10
t = top;
top = top->next; Stack after pushing the element:
free(t); Stack elements:
printf("\nStack after popping an element:\n"); 10
display();
} Main Menu:
} **********
1.Push
void display() 2.Pop
{ 3.Display
STACK *t; 4.Exit
t = top; Enter your choice: 1

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


2.Pop
Enter the element to be pushed: 20 3.Display
4.Exit
Stack after pushing the element: Enter your choice: 4
Stack elements:
20 Exiting...
10
EX 5 : ARRAY IMPLEMENTATION OF QUEUE
Main Menu:
********** SOURCE CODE:
1.Push
2.Pop #include <stdio.h>
3.Display #include <stdlib.h>
4.Exit
Enter your choice: 3 #define size 100

Stack elements: int main() {


20 int front = -1, rear = -1, i, q[size], n, ch, item;
10 clrscr();
printf("\nEnter the size of the queue: ");
Main Menu: scanf("%d", &n);
**********
1.Push do {
2.Pop printf("\nMain Menu:");
3.Display printf("\n**********");
4.Exit printf("\n1.Insert");
Enter your choice: 2 printf("\n2.Delete");
printf("\n3.Display");
Stack after popping an element: printf("\n4.Exit");
Stack elements: printf("\nEnter your choice: ");
10 scanf("%d", &ch);

Main Menu: switch (ch) {


********** case 1:
1.Push if (rear > n - 1) {

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


printf("\nOverflow"); }
} else {
printf("\nEnter the item: "); } while (ch < 4);
scanf("%d", &item);
rear++; return 0;
q[rear] = item; }
if (front == -1)
front = 0; OUTPUT:
}
break; Enter the size of the queue: 5
case 2:
if (front == -1) { Main Menu:
printf("\nUnderflow"); **********
} else { 1.Insert
if (front == rear) { 2.Delete
front = rear = -1; 3.Display
} else { 4.Exit
front = front + 1; Enter your choice: 1
}
} Enter the item: 10
break;
case 3: Main Menu:
if (front == -1) { **********
printf("\nUnderflow"); 1.Insert
} else { 2.Delete
printf("\nQueue elements: "); 3.Display
for (i = front; i <= rear; i++) 4.Exit
printf("%d ", q[i]); Enter your choice: 1
}
break; Enter the item: 20
case 4:
printf("\nExiting..."); Main Menu:
break; **********
default: 1.Insert
printf("\nInvalid choice. Please enter a valid option."); 2.Delete

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


3.Display SOURCE CODE:
4.Exit #include <stdio.h>
Enter your choice: 3 #include <conio.h>
#include <stdlib.h>
Queue elements: 10 20
struct queue
Main Menu: {
********** int data;
1.Insert struct queue *next;
2.Delete } *front = NULL, *rear = NULL;
3.Display
4.Exit typedef struct queue q;
Enter your choice: 2
void create();
Main Menu: void display();
********** void del();
1.Insert
2.Delete void main()
3.Display {
4.Exit int ch;
Enter your choice: 3 clrscr();
do
Queue elements: 20 {
printf("\nMain Menu:");
Main Menu: printf("\n**********");
********** printf("\n1.Insertion");
1.Insert printf("\n2.Deletion");
2.Delete printf("\n3.Display");
3.Display printf("\n4.Exit");
4.Exit printf("\nEnter your choice: ");
Enter your choice: 4 scanf("%d", &ch);
printf("\n\n");
Exiting... switch (ch)
{
EX 6 : LINKED LIST IMPLEMENTATION OF QUEUE case 1:

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


create(); {
break; q *t;
case 2: if (front == NULL)
del(); printf("\nUnderflow");
break; else
case 3: {
display(); t = front;
break; front = front->next;
case 4: free(t);
printf("\nExiting...\n"); }
break; }
}
} while (ch < 4); void display()
getch(); {
} q *t;
void create() if (front == NULL)
{ printf("\nQueue is Underflow");
q *temp; else
printf("\nEnter the data: "); {
scanf("%d", &temp->data); t = front;
temp = (q *)malloc(sizeof(q)); while (t != NULL)
temp->next = NULL; {
if (rear == NULL) printf("\n%d", t->data);
{ t = t->next;
front = temp; }
rear = temp; }
} }
else
{ OUTPUT:
rear->next = temp; Main Menu:
rear = temp; **********
} 1.Insertion
} 2.Deletion
3.Display
void del() 4.Exit

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


Enter your choice: 1 1.Insertion
2.Deletion
Enter the data: 10 3.Display
4.Exit
Main Menu: Enter your choice: 3
**********
1.Insertion 20
2.Deletion
3.Display Main Menu:
4.Exit **********
Enter your choice: 1 1.Insertion
2.Deletion
Enter the data: 20 3.Display
4.Exit
Main Menu: Enter your choice: 4
**********
1.Insertion Exiting...
2.Deletion
3.Display EX 7 : ADDITION OF TWO POLYNOMIAL ADT
4.Exit SOURCE CODE:
Enter your choice: 3
#include<stdio.h>
10 #include<conio.h>
20 #include<malloc.h>

Main Menu: struct poly {


********** int coeff;
1.Insertion int exp;
2.Deletion struct poly* next;
3.Display };
4.Exit
Enter your choice: 2 typedef struct poly POLY;

Main Menu: POLY* First = NULL;


********** POLY* Second = NULL;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


POLY* Third = NULL; Second = temp;
else {
void create() { t = Second;
POLY* temp, * t; while (t->next != NULL)
int m, n, i, c, e; t = t->next;
printf("Enter the number of elements in Polynomial 1 and Polynomial 2:\n"); t->next = temp;
scanf("%d%d", &m, &n); }
}
printf("\nPolynomial 1:\n"); }
for (i = 0; i < m; i++) {
printf("Enter the coefficient and exponent in decreasing order:\t"); void add() {
scanf("%d%d", &c, &e); POLY* temp1, * temp2, * temp3, * p;
temp = (POLY*)malloc(sizeof(POLY)); temp1 = First;
temp->exp = e; temp2 = Second;
temp->coeff = c; while ((temp1 != NULL) && (temp2 != NULL)) {
temp->next = NULL; p = (POLY*)malloc(sizeof(POLY));
if (First == NULL) if (temp1->exp > temp2->exp) {
First = temp; p->exp = temp1->exp;
else { p->coeff = temp1->coeff;
t = First; temp1 = temp1->next;
while (t->next != NULL) } else if (temp1->exp < temp2->exp) {
t = t->next; p->exp = temp2->exp;
t->next = temp; p->coeff = temp2->coeff;
} temp2 = temp2->next;
} } else {
p->exp = temp1->exp;
printf("\nPolynomial 2:\n"); p->coeff = temp1->coeff + temp2->coeff;
for (i = 0; i < n; i++) { temp1 = temp1->next;
printf("Enter the coefficient and exponent in decreasing order:\t"); temp2 = temp2->next;
scanf("%d%d", &c, &e); }
temp = (POLY*)malloc(sizeof(POLY)); p->next = NULL;
temp->exp = e; if (Third == NULL)
temp->coeff = c; Third = p;
temp->next = NULL; else {
if (Second == NULL) temp3 = Third;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


while (temp3->next != NULL) }
temp3 = temp3->next;
temp3->next = p;
} OUTPUT:
}
} ADDITION OF POLYNOMIALS:
**********
void display(POLY* poly) { Enter the two polynomials:
while (poly != NULL) { Enter the number of elements in Polynomial 1 and Polynomial 2:
if (poly == First || poly == Second || poly == Third) 34
printf("%dx^%d", poly->coeff, poly->exp);
else { Polynomial 1:
if (poly->coeff > 0) Enter the coefficient and exponent in decreasing order: 3 4
printf(" + %dx^%d", poly->coeff, poly->exp); Enter the coefficient and exponent in decreasing order: 2 2
else Enter the coefficient and exponent in decreasing order: 1 0
printf(" %dx^%d", poly->coeff, poly->exp);
} Polynomial 2:
poly = poly->next; Enter the coefficient and exponent in decreasing order: 2 3
} Enter the coefficient and exponent in decreasing order: 4 2
} Enter the coefficient and exponent in decreasing order: 1 1
Enter the coefficient and exponent in decreasing order: 5 0
void main() {
clrscr(); Polynomial 1: 3x^4 + 2x^2 + 1x^0
printf("\nADDITION OF POLYNOMIALS:"); Polynomial 2: 2x^3 + 4x^2 + 1x^1 + 5x^0
printf("\n**********"); Added Polynomial: 3x^4 + 2x^3 + 6x^2 + 2x^1 + 6x^0
printf("\nEnter the two polynomials:");
create();
add();
printf("\nPolynomial 1:"); EX 8 : INFIX TO POSTFIX CONVERSION
display(First);
printf("\nPolynomial 2:"); SOURCE CODE:
display(Second);
printf("\nAdded Polynomial:"); #include <stdio.h>
display(Third); #include <conio.h>
getch();

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


char stack[20], infix[20], postfix[20]; }
int top = -1, i = 0, j = 0; push(infix[i]);
break;
void push(char); case '+':
char pop(); case '-':
while (stack[top] == '^' || stack[top] == '*' || stack[top] == '/' || stack[top] == '+' ||
void main() stack[top] == '-')
{ {
clrscr(); postfix[j] = pop();
printf("Enter the infix expression: "); j++;
gets(infix); }
while (infix[i] != '\0') push(infix[i]);
{ break;
switch (infix[i]) default:
{ postfix[j] = infix[i];
case '(': j++;
push('('); }
break; i++;
case ')': }
while (stack[top] != '(')
{ while (top >= 0)
postfix[j] = pop(); {
j++; postfix[j] = pop();
} j++;
pop(); }
break;
case '^': postfix[j] = '\0';
push('^'); printf("\nPostfix equivalent: %s", postfix);
break; getch();
case '*': }
case '/':
while (stack[top] == '*' || stack[top] == '/' || stack[top] == '^') void push(char ch)
{ {
postfix[j] = pop(); top++;
j++; stack[top] = ch;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


} printf("Enter the number of elements: ");
scanf("%d", &n);
char pop() printf("Enter the values: ");
{ for (i = 0; i < n; i++)
char val; {
if (top < 0) scanf("%d", &a[i]);
{ }
puts("Stack is empty"); printf("Enter the element to be searched: ");
val = '\0'; scanf("%d", &se);
}
else for (i = 0; i < n; i++)
{ {
val = stack[top]; if (se == a[i])
top--; {
} printf("%d is present at location %d", se, i + 1);
return val; break;
} }
}
OUTPUT:
if (i == n)
Enter the infix expression: (a+b)*c printf("\n%d is not present in the array", se);

Postfix equivalent: ab+c* getch();


}
OUTPUT:

EX 9 : LINEAR SEARCH Enter the number of elements: 5


SOURCE CODE: Enter the values: 4 8 12 6 15
#include <stdio.h> Enter the element to be searched: 6
#include <conio.h>
6 is present at location 4
void main()
{ Enter the number of elements: 7
int i, n, a[10], se; Enter the values: 3 7 11 5 9 13 17
clrscr(); Enter the element to be searched: 8

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


break;
8 is not present in the array }
else if (se < a[mid])
EX 10 : BINARY SEARCH {
u = mid - 1;
SOURCE CODE: }
else
#include <stdio.h> {
#include <conio.h> l = mid + 1;
}
void main() }
{
int a[10], i, n, se, u, l, mid, c = 0; if (c == 0)
clrscr(); {
printf("Enter the size of the array: "); printf("Element not found");
scanf("%d", &n); }
else
printf("Enter the sorted elements: "); {
for (i = 0; i < n; i++) printf("Element found");
{ }
scanf("%d", &a[i]);
} getch();
}
printf("Enter the element to be searched: ");
scanf("%d", &se); OUTPUT:

l = 0; Enter the size of the array: 5


u = n - 1; Enter the sorted elements: 3 7 11 15 19
Enter the element to be searched: 8
while (l <= u)
{ Element not found
mid = (l + u) / 2;
if (se == a[mid])
{ Enter the size of the array: 6
c = 1; Enter the sorted elements: 2 4 6 8 10 12

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


Enter the element to be searched: 8 }
}
Element found
printf("Sorted array: ");
EX 11 : INSERTION SORT for (i = 0; i < n; i++)
{
SOURCE CODE: printf("%d ", a[i]);
}
#include<stdio.h>
#include<conio.h> getch();
}
void main()
{ OUTPUT:
int a[50], i, j, n, temp;
clrscr(); Enter the size of the array: 4
Enter element 1: 30
printf("Enter the size of the array: "); Enter element 2: 10
scanf("%d", &n); Enter element 3: 20
Enter element 4: 15
for (i = 0; i < n; i++) Sorted array: 10 15 20 30
{
printf("Enter element %d: ", i + 1);
scanf("%d", &a[i]);
}
EX 12 : MERGE SORT
for (i = 0; i < n; i++)
{ SOURCE CODE:
for (j = 0; j < n - i - 1; j++)
{ #include<stdio.h>
if (a[j] > a[j + 1]) #include<conio.h>
{
temp = a[j]; void merge(int a[], int f1, int f2, int l1, int l2);
a[j] = a[j + 1]; void merge_split(int a[], int first, int last);
a[j + 1] = temp;
} void main()

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


{
int i, n; while (j <= l2)
clrscr(); b[k++] = a[j++];
printf("Enter the limit: ");
scanf("%d", &n); i = f1;
printf("Enter the elements: "); j = 0;
for (i = 0; i < n; i++)
scanf("%d", &a[i]); while (i <= l2 && j < k)
a[i++] = b[j++];
merge_split(a, 0, n - 1); }

printf("The sorted list is:\n"); void merge_split(int a[], int first, int last)
for (i = 0; i < n; i++) {
printf("%d\n", a[i]); int mid;
if (first < last)
getch(); {
} mid = (first + last) / 2;
merge_split(a, first, mid);
void merge(int a[], int f1, int f2, int l1, int l2) merge_split(a, mid + 1, last);
{ merge(a, first, mid + 1, mid, last);
int i, j, k = 0; }
i = f1; }
j = f2;

while (i <= l1 && j <= l2) OUTPUT:


{
if (a[i] < a[j]) Enter the limit: 5
b[k] = a[i++]; Enter the elements: 12 6 9 3 8
else
b[k] = a[j++]; The sorted list is:
k++; 3
} 6
8
while (i <= l1) 9
b[k++] = a[i++]; 12

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


printf("\n\n0. Create\n1. Insert\n2. Preorder\n3. Inorder\n4. Postorder\n5. Exit\n6.
EX 13 : BINARY SEARCH TREE Delete\n");
printf("\nEnter choice: ");
SOURCE CODE: scanf("%d", &m);

#include<stdio.h> switch (m)


#include<conio.h> {
#include<stdlib.h> case 0:
root = create();
struct node break;
{ case 1:
int data; insert(root, create());
struct node *rlink; break;
struct node *llink; case 2:
}; printf("\nDisplay tree in preorder traversal\n");
preorder(root);
typedef struct node NODE; break;
case 3:
NODE* create(); printf("\nDisplay tree in inorder\n");
void insert(NODE*, NODE*); inorder(root);
void preorder(NODE*); break;
void inorder(NODE*); case 4:
void postorder(NODE*); printf("\nDisplay tree in postorder\n");
void del(NODE*, int); postorder(root);
break;
void main() case 5:
{ exit(0);
int m; case 6:
NODE *root = NULL; {
int ele;
clrscr(); printf("Enter the number to be deleted: ");
scanf("%d", &ele);
do del(root, ele);
{ break;
}

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


} }
} while (m != 5); temp = temp->llink;
}
getch();
} if (newnode->data > temp->data)
{
NODE* create() if (temp->rlink == NULL)
{ {
NODE *newnode; temp->rlink = newnode;
int n; break;
}
newnode = (NODE*)malloc(sizeof(NODE)); temp = temp->rlink;
}
printf("\nEnter the data: "); }
scanf("%d", &n); }

newnode->data = n; void postorder(NODE *tmp)


newnode->llink = NULL; {
newnode->rlink = NULL; if (tmp != NULL)
{
return newnode; postorder(tmp->llink);
} postorder(tmp->rlink);
printf("%d->", tmp->data);
void insert(NODE *root, NODE *newnode) }
{ }
NODE *temp = root;
void inorder(NODE *tmp)
while (1) {
{ if (tmp != NULL)
if (newnode->data < temp->data) {
{ inorder(tmp->llink);
if (temp->llink == NULL) printf("%d->", tmp->data);
{ inorder(tmp->rlink);
temp->llink = newnode; }
break; }

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


printf("Element identified: %d\n", temp->data);
void preorder(NODE *tmp)
{ if (temp->llink == NULL && temp->rlink == NULL)
if (tmp != NULL) {
{ printf("Node with no children\n");
printf("%d->", tmp->data); if (temp == prev->rlink)
preorder(tmp->llink); {
preorder(tmp->rlink); prev->rlink = NULL;
} }
} else
{
void del(NODE *root, int ele) prev->llink = NULL;
{ }
NODE *temp = root, *prev = NULL, *curr = NULL, *prev2 = NULL;
free(temp);
while (temp != NULL && temp->data != ele) }
{ else if (temp->llink != NULL && temp->rlink != NULL)
prev = temp; {
printf("Node with two children\n");
if (ele <= temp->data) curr = temp->rlink;
{
temp = temp->llink; while (curr->llink != NULL)
} {
else prev2 = curr;
{ curr = curr->llink;
temp = temp->rlink; }
}
} if (temp == prev->rlink)
{
if (temp == NULL) prev->rlink = curr;
{ }
printf("Element not found\n"); else
return; {
} prev->llink = curr;
}

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


}
prev2->llink = NULL; }
curr->llink = temp->llink;
curr->rlink = temp->rlink; OUTPUT:

free(temp); 0. Create
} 1. Insert
else 2. Preorder
{ 3. Inorder
printf("Node with one child\n"); 4. Postorder
if (prev->rlink == temp) 5. Exit
{ 6. Delete
if (temp->llink == NULL)
{ Enter choice: 0
printf("Target is right child\n");
prev->rlink = temp->rlink; Enter the data: 10
}
else 0. Create
{ 1. Insert
prev->rlink = temp->llink; 2. Preorder
} 3. Inorder
} 4. Postorder
else 5. Exit
{ 6. Delete
if (temp->llink == NULL)
{ Enter choice: 1
prev->llink = temp->rlink;
} Enter the data: 5
else
{ 0. Create
prev->llink = temp->llink; 1. Insert
} 2. Preorder
} 3. Inorder
4. Postorder
free(temp); 5. Exit

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


6. Delete 3. Inorder
4. Postorder
Enter choice: 1 5. Exit
6. Delete
Enter the data: 15
Enter choice: 4
0. Create
1. Insert Display tree in postorder
2. Preorder 5->15->10->
3. Inorder
4. Postorder 0. Create
5. Exit 1. Insert
6. Delete 2. Preorder
3. Inorder
Enter choice: 2 4. Postorder
5. Exit
Display tree in preorder traversal 6. Delete
10->5->15->
Enter choice: 6
0. Create
1. Insert Enter the number to be deleted: 10
2. Preorder Element identified: 10
3. Inorder Node with two children
4. Postorder
5. Exit 0. Create
6. Delete 1. Insert
2. Preorder
Enter choice: 3 3. Inorder
4. Postorder
Display tree in inorder 5. Exit
5->10->15-> 6. Delete

0. Create Enter choice: 2


1. Insert
2. Preorder Display tree in preorder traversal

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


15->5-> {
bool ht_inc;
0. Create int info;
1. Insert int choice;
2. Preorder
3. Inorder clrscr();
4. Postorder
5. Exit root = NULL;
6. Delete
printf("\n1. Insert\n2. Display\n3. Exit");
Enter choice: 5
while (1)
EX : 14 AVL TREE {
printf("\nEnter your choice: ");
SOURCE CODE: scanf("%d", &choice);

#include<stdio.h> switch (choice)


#include<conio.h> {
case 1:
typedef enum { FALSE, TRUE } bool; printf("\nEnter the value to be inserted: ");
scanf("%d", &info);
struct node if (search(root, info) == NULL)
{ root = insert(info, root, &ht_inc);
int info; else
int balance; printf("\nDuplicate value ignored");
struct node *lchild; break;
struct node *rchild; case 2:
} *root; if (root == NULL)
{
struct node* insert(int, struct node*, int*); printf("Tree is empty");
struct node* search(struct node*, int); continue;
void display(struct node*, int); }
void inorder(struct node*); printf("\nTree is:\n");
display(root, 1);
void main() printf("\n\nInorder traversal\n");

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


inorder(root); }
break;
case 3: if (info < pptr->info)
exit(0); {
default: pptr->lchild = insert(info, pptr->lchild, ht_inc);
printf("\nWrong choice");
} if (*ht_inc == TRUE)
} {
} switch (pptr->balance)
{
struct node* search(struct node* ptr, int info) case 1:
{ pptr->balance = 0;
if (ptr != NULL) *ht_inc = FALSE;
{ break;
if (info < ptr->info) case 0:
ptr = search(ptr->lchild, info); pptr->balance = 1;
else if (info > ptr->info) break;
ptr = search(ptr->rchild, info); case -1:
} aptr = pptr->lchild;
return ptr;
} if (aptr->balance == -1)
{
struct node* insert(int info, struct node* pptr, int* ht_inc) printf("\nRight to right rotation");
{ pptr->lchild = aptr->rchild;
struct node *aptr, *bptr; aptr->rchild = pptr;
pptr->balance = 0;
if (pptr == NULL) aptr->balance = 0;
{ pptr = aptr;
pptr = (struct node*)malloc(sizeof(struct node)); }
pptr->info = info; else
pptr->lchild = NULL; {
pptr->rchild = NULL; printf("\nRight to left rotation");
pptr->balance = 0; bptr = aptr->rchild;
*ht_inc = TRUE; aptr->rchild = bptr->lchild;
return pptr; bptr->lchild = aptr;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


pptr->lchild = bptr->rchild; aptr->balance = 0;
bptr->rchild = pptr; pptr = aptr;
}
if (bptr->balance == -1) else
pptr->balance = 1; {
else printf("\nLeft to right rotation");
pptr->balance = 0; bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
aptr->balance = -1; bptr->rchild = aptr;
bptr->balance = 0; pptr->rchild = bptr->lchild;
bptr->lchild = pptr;
pptr = bptr;
} if (bptr->balance == 1)
*ht_inc = FALSE; pptr->balance = -1;
} else
} pptr->balance = 0;
}
aptr->balance = 1;
if (info > pptr->info) bptr->balance = 0;
{
pptr->rchild = insert(info, pptr->rchild, ht_inc); pptr = bptr;
}
if (*ht_inc == TRUE) *ht_inc = FALSE;
{ break;
switch (pptr->balance) case 0:
{ pptr->balance = -1;
case 1: break;
aptr = pptr->rchild; case -1:
pptr->balance = 0;
if (aptr->balance == 1) *ht_inc = FALSE;
{ break;
printf("\nLeft to left rotation"); }
pptr->rchild = aptr->lchild; }
aptr->lchild = pptr; }
pptr->balance = 0;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


return pptr; 3. Exit
} Enter your choice: 1
Enter the number of elements: 4
void display(struct node* ptr, int level) Enter tree data: 7 12 4 9
{
int i; 1. Insert
2. Display
if (ptr != NULL) 3. Exit
{ Enter your choice: 2
display(ptr->rchild, level + 1); Preorder sequence: 7(bf=-1) 4(bf=0) 12(bf=1) 9(bf=0)
printf("\n"); Inorder sequence: 4(bf=0) 7(bf=-1) 9(bf=0) 12(bf=1)

for (i = 0; i < level; i++) 1. Insert


printf(" "); 2. Display
3. Exit
printf("%d", ptr->info); Enter your choice: 2
Enter a data: 7
display(ptr->lchild, level + 1);
} 1. Insert
} 2. Display
3. Exit
void inorder(struct node* ptr) Enter your choice: 2
{ Preorder sequence: 9(bf=0) 4(bf=0) 12(bf=0)
if (ptr != NULL) Inorder sequence: 4(bf=0) 9(bf=0) 12(bf=0)
{
inorder(ptr->lchild); 1. Insert
printf("%d", ptr->info); 2. Display
inorder(ptr->rchild); 3. Exit
} Enter your choice: 3
}
EX 15 : IMPLEMENTATION OF PRIORITY QUEUE
OUTPUT:
SOURCE CODE:
1. Insert
2. Display #include <stdio.h>

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


#include <conio.h> deletebypriority(n);
#include <stdlib.h> break;
#define MAX 5 case 3:
displayqueue();
void insertbypriority(int); break;
void deletebypriority(int); case 4:
void create(); break;
void check(int); default:
void displayqueue(); printf("\nEnter correct choice");
int priqueue[MAX]; }
int front, rear; } while (ch != 4);
}
void main()
{ void create()
int n, ch; {
printf("\n1. Insert an element into queue"); front = rear = -1;
printf("\n2. Delete an element from the queue"); }
printf("\n3. Display the queue elements");
printf("\n4. Exit"); void insertbypriority(int data)
create(); {
if (rear >= MAX - 1)
do {
{ printf("\nQueue overflow, no more element can be inserted");
printf("\nEnter your choice: "); return;
scanf("%d", &ch); }
switch (ch)
{ if ((front == -1) && (rear == -1))
case 1: {
printf("\nEnter the element to insert: "); front++;
scanf("%d", &n); priqueue[rear] = data;
insertbypriority(n); return;
break; }
case 2: else
printf("\nDeleted value is: "); {
scanf("%d", &n); check(data);

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


rear++; for (i = i; i < rear; i++)
} {
} priqueue[i] = priqueue[i + 1];
}
void check(int data) rear--;
{
int i, j; if (rear == -1)
for (i = 0; i <= rear; i++) {
{ front = -1;
if (data >= priqueue[i]) }
{ return;
for (j = rear + 1; j > i; j--) }
{ }
priqueue[j] = priqueue[j - 1];
} printf("\n%d not found in queue to delete", data);
priqueue[i] = data; }
return;
} void displayqueue()
} {
priqueue[i] = data; if ((front == -1) && (rear == -1))
} {
printf("\nQueue is empty");
void deletebypriority(int data) return;
{ }
int i;
if ((front == -1) && (rear == -1)) for (front = 0; front <= rear; front++)
{ {
printf("\nQueue is empty"); printf("\n%d", priqueue[front]);
return; }
} front = 0;
}
for (i = 0; i <= rear; i++)
{ OUTPUT:
if (data == priqueue[i])
{ 1. Insert an element into the queue

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


2. Delete an element from the queue void linear_probing(int[], int, int);
3. Display the queue elements void disp(int []);
4. Exit
int main()
Enter your choice: 1 {
Enter the element to insert: 5 int a[maximum], num, key, i;
Enter your choice: 1 char ans;
Enter the element to insert: 2 clrscr();
printf("\n Collision handling by linear probing\n");
Enter your choice: 1
Enter the element to insert: 8 for(i = 0; i < maximum; i++)
Enter your choice: 3 a[i] = -1;
Queue elements:
2 do
5 {
8 printf("\n Enter the next element in the list: ");
key = create(num);
Enter your choice: 2 linear_probing(a, key, num);
Deleted value is: 5 printf("\n Do you want to continue? (y/n): ");
Enter your choice: 3 scanf("%s", &ans);
Queue elements: }
2 while(ans == 'y' || ans == 'Y');
8
disp(a);
Enter your choice: 4 getch();
EX 16 : IMPLEMENTION OF OPEN ADDRESSING – LINEAR PROBING return 0;
}
SOURCE CODE:
int create(int num)
#include<stdio.h> {
#include<stdlib.h> int key = num % maximum;
#include<conio.h> return key;
#define maximum 10 }

int create(int); void linear_probing(int a[maximum], int key, int num)

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


{ Do you want to continue? (y/n): y
int probe = 0; Enter the next element in the list: 25

if (a[key] == -1) Do you want to continue? (y/n): y


{ Enter the next element in the list: 35
a[key] = num;
Do you want to continue? (y/n): y
probe++;
} Enter the next element in the list: 45
Do you want to continue? (y/n): y
while (a[key] != -1)
{ Enter the next element in the list: 55
key = (key + 1) % maximum; Do you want to continue? (y/n): n
probe++;
}
The hash table is:
a[0] 5
if (probe == -1)
a[1] -1
printf("\n Elements successfully added..");
a[2] 15
else
a[3] 25
printf("\n Hash table is full");
a[4] 35
}
a[5] 45
a[6] 55
void disp(int a[maximum])
a[7] -1
{
a[8] -1
int i;
a[9] -1
printf("\n The hash table is:\n");

for(i = 0; i < maximum; i++)


printf("\n a[%d]\t%d", i, a[i]);
EX 17 : DIJKSTRA’S ALGORITHM
}
SOURCE CODE:
#include <stdio.h>
OUTPUT:
#include <limits.h>
Enter the next element in the list: 5
#define MAX 10
Do you want to continue? (y/n): y
Enter the next element in the list: 15 void dijkstra(int G[MAX][MAX], int n, int startnode);

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


visited[startnode] = 1;
int main() { count = 1;
int G[MAX][MAX], i, j, n, u;
while (count < n - 1) {
printf("Enter no. of vertices: "); mindistance = INT_MAX;
scanf("%d", &n);
for (i = 0; i < n; i++)
printf("Enter the adjacency matrix:\n"); if (distance[i] < mindistance && !visited[i]) {
for (i = 0; i < n; i++) mindistance = distance[i];
for (j = 0; j < n; j++) nextnode = i;
scanf("%d", &G[i][j]); }

printf("Enter the starting node: "); visited[nextnode] = 1;


scanf("%d", &u); count++;

dijkstra(G, n, u); for (i = 0; i < n; i++)


if (!visited[i] && mindistance + cost[nextnode][i] < distance[i]) {
return 0; distance[i] = mindistance + cost[nextnode][i];
} pred[i] = nextnode;
}
void dijkstra(int G[MAX][MAX], int n, int startnode) { }
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j; for (i = 0; i < n; i++)
if (i != startnode) {
for (i = 0; i < n; i++) printf("\nDistance to node %d = %d", i, distance[i]);
for (j = 0; j < n; j++) printf("\nPath = %d", i);
cost[i][j] = (G[i][j] == 0) ? INT_MAX : G[i][j]; j = i;

for (i = 0; i < n; i++) { do {


distance[i] = cost[startnode][i]; j = pred[j];
pred[i] = startnode; printf("<-%d", j);
visited[i] = 0; } while (j != startnode);
} }
}
distance[startnode] = 0; OUTPUT:

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


Enter no. of vertices: 5 printf("Enter no. of vertices:");
Enter the adjacency matrix: scanf("%d", &n);
03010 printf("\nEnter the adjacency matrix:\n");
30750 for (i = 0; i < n; i++)
07028 for (j = 0; j < n; j++)
15204 scanf("%d", &G[i][j]);
00840 total_cost = prims();
Enter the starting node: 0 printf("\nspanning tree matrix:\n");
for (i = 0; i < n; i++) {
Distance to node 1 = 3 printf("\n");
Path = 1<-0 for (j = 0; j < n; j++)
Distance to node 2 = 10 printf("%d\t", spanning[i][j]);
Path = 2<-3<-0 }
Distance to node 3 = 1
Path = 3<-0 printf("\n\nTotal cost of spanning tree = %d", total_cost);
Distance to node 4 = 4 getch();
Path = 4<-3<-0 }

int prims() {
int cost[MAX][MAX];
int u, v, min_distance, distance[MAX], from[MAX];
EX 18 : PRIM’S ALGORITHM int visited[MAX], no_of_edges, i, min_cost, j;
SOURCE CODE:
#include <stdio.h> // Create cost[][] matrix, spanning[][]
#include <stdlib.h> for (i = 0; i < n; i++)
#define infinity 9999 for (j = 0; j < n; j++) {
#define MAX 20 if (G[i][j] == 0)
cost[i][j] = infinity;
int G[MAX][MAX], spanning[MAX][MAX], n; else
cost[i][j] = G[i][j];
int prims(); spanning[i][j] = 0;
}
void main() { // Initialise visited[], distance[] and from[]
int i, j, total_cost; distance[0] = 0;
clrscr(); visited[0] = 1;

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001


for (i = 1; i < n; i++) {
distance[i] = cost[0][i]; OUTPUT:
from[i] = 0;
visited[i] = 0; Enter no. of vertices: 5
} Enter the adjacency matrix:
02060
min_cost = 0; // Cost of spanning tree 20385
no_of_edges = n - 1; // No. of edges to be added 03007
68009
while (no_of_edges > 0) { 05790
min_distance = infinity; Spanning tree matrix:
for (i = 1; i < n; i++) 0 2 0 6 0
if (visited[i] == 0 && distance[i] < min_distance) { 2 0 3 0 5
v = i; 0 3 0 0 0
min_distance = distance[i]; 6 0 0 0 0
} 0 5 0 0 0

u = from[v]; Total cost of spanning tree = 16


// Insert the edge in spanning tree
spanning[u][v] = distance[v];
spanning[v][u] = distance[v];
no_of_edges--;
visited[v] = 1;

// Update the distance[] array


for (i = 1; i < n; i++)
if (visited[i] == 0 && cost[i][v] < distance[i]) {
distance[i] = cost[i][v];
from[i] = v;
}

min_cost = min_cost + cost[u][v];


}
return (min_cost);
}

ABINANTHAN V 311622104001 ABINANTHAN V 311622104001

You might also like