0% found this document useful (0 votes)
5 views66 pages

C & Data Structure Lab

Uploaded by

dhanush77879
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)
5 views66 pages

C & Data Structure Lab

Uploaded by

dhanush77879
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/ 66

EX NO:1(a)

PROGRAM USING I/O STATEMENTS AND EXPRESSIONS


Date:

AIM:
To write a C program to perform some expressions using I/O statements.

ALGORITHM:
1. Start
2. Declare variables as dec,str,ch,pi
3. Access the value of the variable using I/O statements
4. Print the values of dec,str,ch,pi
5. Stop
PROGRAM:
#include <stdio.h>
Void main()
{
int dec = 5;
char str[] = "abc";
char ch = 's';
float pi = 3.14;
printf("%d %s %f %c\n", dec, str, pi, ch);
}

Output:

5 abc 3.140000 c

RESULT:

C program using I/O statements are compiled and executed successfully.


EX NO:1( b)
PROGRAMS USING DECISION-MAKING CONSTRUCTS
Date:

AIM:

To write a C program to perform decision making using constructs.

ALGORITHM:
1. Start
2. Declare variables as x=15, y=18
3. Check the condition if(x>y)
If x is big print x is greater than y
If y is big print y is greater than x
4. Stop
PROGRAM:

#include <stdio.h>
Void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
Output:
y is greater than x

RESULT:

C program using I/O statements are compiled and executed successfully.


EX NO:1( c)
PROGRAMS USING ITERATIVE STATEMENTS
Date:

AIM:

To write a C program to perform iterative statement

ALGORITHM:
1. Start
2. i is initialized to 1.
3. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for
loop is executed. This will print the 1 (value of i) on the screen.
4. The update statement ++i is executed. Now, the value of i will be 2. Again, the
test expression is evaluated to true, and the body of for loop is executed. This will
print 2 (value of i) on the screen.
5. Again, the update statement ++i is executed and the test expression i < 11 is
evaluated. This process goes on until i becomes 11.
6. When i becomes 11, i < 11 will be false, and the for loop terminates.
7. Stop.

PROGRAM:

#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// for loop terminates when num is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

return 0;
}
Output:
Enter a positive integer: 10
Sum = 55

RESULT:

C program using iterative statements are compiled and executed successfully.


EX NO:2(a)
PROGRAMS USING FUNCTIONS
Date:

AIM:

To write a C program to perform functions.

ALGORITHM:
1. Start
2. Initially, the sum() is called from the main() function with number passed as an
argument.
3. Suppose, the value of n inside sum() is 3 initially. During the next function call, 2
is passed to the sum() function. This process continues until n is equal to 0.
4. When n is equal to 0, the if condition fails and the else part is executed returning
the sum of integers ultimately to the main() function.
5. Stop.

PROGRAM:

#include <stdio.h>
int sum(int n);

int main() {
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);

result = sum(number);

printf("sum = %d", result);


return 0;
}

int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Output:

Enter a positive integer:3


sum = 6

RESULT:

C program using functions are compiled and executed successfully.


EX NO:2(b)
PROGRAMS USING ARRAYS
Date:

AIM:

To write a C program to perform arrays.

ALGORITHM:
1. Start
2. Arrays have 0 as the first index, not 1. In this example, mark[0] is the first
element.
3. If the size of an array is n, to access the last element, the n-1 index is used. In this
example, mark[4]
4. Suppose the starting address of mark[0] is 2120d. Then, the address of the
mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
5. This is because the size of a float is 4 bytes.
6. Stop.

PROGRAM:

#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Output:

Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3

RESULT:

C program using arrays are compiled and executed successfully.


EX NO:3(a)
PROGRAMS USING POINTERS
Date:

AIM:

To write a C program to perform pointers.

ALGORITHM:
1. Start
2. Here, a pointer pc and a normal variable c, both of type int, is created.
3. Since pc and c are not initialized at initially, pointer pc points to either no address
or a random address. And, variable c has an address but contains random garbage
value.
4. This assigns the address of variable c to the pointer pc.
5. This change the value at the memory location pointed by the pointer pc to 2.
6. Stop.

PROGRAM:

#include <stdio.h>
int main()
{
int* pc, c;

c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22

pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22

c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11

*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output:

Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784


Content of pointer pc: 22

Address of pointer pc: 2686784


Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

RESULT:

C program using pointers are compiled and executed successfully.


EX NO:3(b)
PROGRAMS USING STRUCTURES
Date:

AIM:

To write a C program to perform structures.

ALGORITHM:
1. Start.
2. Like primitive types, we can have pointer to a structure.
3. If we have a pointer to structure, members are accessed using arrow ( -> )
operator.
4. Stop.

PROGRAM:

#include<stdio.h>

struct Point
{
int x, y;
};

int main()
{
struct Point p1 = {1, 2};

// p2 is a pointer to structure p1
struct Point *p2 = &p1;

// Accessing structure members using structure pointer


printf("%d %d", p2->x, p2->y);
return 0;
}

Output:

12
RESULT:

C program using structures are compiled and executed successfully.


EX NO:4
PROGRAMS USING FILES
Date:

AIM:
To write a C program to perform files.

ALGORITHM:
1. Start.
2. Firstly, It searches the file to be opened.
3. Then, it loads the file from the disk and place it into the buffer. The buffer is used
to provide efficiency for the read operations.
4. It sets up a character pointer which points to the first character of the file.
5. Stop.

PROGRAM:

#include< stdio.h >


int main()
{

FILE *fp; /* file pointer*/


char fName[20];

printf("Enter file name to create :");


scanf("%s",fName);

/*creating (open) a file, in “w”: write mode*/


fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
exit(0); /*exit from program*/
}

printf("File created successfully.");


return 0;
}
Output:

Run 1:
Enter file name to create : file1.txt
File created successfully.

Run 2:
Enter file name to create : d:/file1.txt
File created successfully.

“file will be created in d: drive”.

Run 3:
Run 1:
Enter file name to create : h:/file1.txt
File does not created!!!

RESULT:

C program using files are compiled and executed successfully.


EX NO:5
DEVELOPMENT OF REAL TIME C APPLICATION
Date:

AIM:
To write a C program to real time c application.

ALGORITHM:
1. Start.
2. When this occurs is determined by the location of the operators.
3. If the ++ or -- operator appears before the variable, the increment or decrement
operation occurs before any subsequent operations using that variable.
4. If the ++ or -- operator appears after the variable name, the increment or
decrement occurs after the indicated operation.
5. Stop.

PROGRAM:

#include <stdio.h>

int main(void)
{
int c;

while ((c = getchar()) != EOF) {


if (c == '.')
break;

switch(c) {
case '0':
printf("Numeral 0\n");
case '1':
printf("Numeral 1\n");
case '2':
printf("Numeral 2\n");
case '3':
printf("Numeral 3\n");
}
}
}

Output:
0
Numeral 0
Numeral 1
Numeral 2
Numeral 3
1
Numeral 1
Numeral 2
Numeral 3
2
Numeral 2
Numeral 3
3
Numeral 3
.

RESULT:

C program using real time c application are compiled and executed successfully.
EX NO: 6
ARRAY IMPLEMENTATION OF LIST ADT
Date:

AIM:

To write a C program for implement the concept of list ADT using arrays.

ALGORITHM:

Step 1: Start the program


Step 2: Declare the necessary functions for implementation.
Step 3: Get the input from the user and store it an array.
Step 4: In Insertion, half of the elements to be shifted upwards and in deletion half of the
elements to be shifted downwards.
Step 5: Display the output using an array.
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
inta,b[20], n, p, e, f, i, pos;
void main()
{
clrscr();
intch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:create();
break;
case 2:deletion();
break;
case 3:search();
break;
case 4:insert();
break;
case 5:display();
break;
case 6:exit();
break;
default: printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}
}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
}
}
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}
}

OUTPUT:

Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 1
Enter the number of elements: 4
Enter the elements:
10
20
30
40
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 2
Enter the element to delete:20
Elements after deletion: 10
30
40
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 3
Enter the element to search: 100
Element not found
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 4
Enter the element to insert: 15
Enter the position to insert:2
Elements after insertion:
10
15
30
40
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 4

Result:

Thus, a C program of list ADT using arrays was implemented successfully.


EX NO:7(a)
ARRAY IMPLEMENTATION OF STACK ADTS
Date:

AIM:
To write a C program to implement Stack operations such as push, pop and display using
array.

ALGORITHM:

Step 1: Start the program


Step2: Initially top = -1;
Step 3: push operation increases top by one and writes pushed element to storage[top];
Step 4: pop operation checks that top is not equal to -1 and decreases top variable by 1;
Step 5: display operation checks that top is not equal to -1 and returns storage[top];
Step 6: Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 5
int item;
int s[10];
int top;
void display()
{
inti;
if(top==-1)
{
printf("\n stack is empty");
return;
}
printf("\n Content of stack is:\n");
for(i=0;i<=top ;i++)
printf("%d\t",s[i]);
}
void push()
{
if(top==size-1)
{
printf("\nStack is full");
return;
}
printf("\nEnter item:\n");
scanf("%d",&item);
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf("\nstack is empty");
return;
}
printf("\nDeleted item is: %d",s[top]);
top--;
}
void main()
{
intch;
top=-1;
clrscr();
printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");
do{
printf("\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter item:\n");
scanf("%d",&item);
push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong entry ! try again");
}}while(ch<=4);
getch();
}
OUTPUT:
1. Push 2.pop
3. Display 4.exit
Enter your choice:
1
Enter item:
100
Enter your choice:
1
Enter item:
200
Enter your choice:
1
Enter item:
300
Enter your choice:
2
Deleted item is: 300
Enter your choice:
3
Content of stack is:
100 200
Enter your choice: 4

RESULT:

Thus a C program for Stack using array was implemented successfully.


EX NO:7(b)
ARRAY IMPLEMENTATION OF QUEUE ADTS
Date :

AIM:
To write a C program to implement Queue operations such as enqueue, dequeue and
display using array.
ALGORITHM:
Step 1: Start the program.
Step 2: Initialize front=0; rear=-1.
Step 3: Enqueue operation moves a rear by one position and inserts a element at the rear.
Step 4: Dequeue operation deletes an element at the front of the list and moves the front by one
position
Step 5: Display operation displays all the element in the list.
Step 6: Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define SIZE 5 /* Size of Queue */
int Q[SIZE],f=0,r=-1; /* Global declarations */
Qinsert(intelem)
{ /* Function for Insert operation */
if( Qfull())
printf("\n\n Overflow!!!!\n\n");
else
{
++r;
Q[r]=elem;
}
}
intQdelete()
{ /* Function for Delete operation */
intelem;
if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
return(-1); }
else
{
elem=Q[f];
f=f+1;
return(elem);
}}
intQfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}
intQempty()
{ /* Function to Check Queue Empty */
if(f> r) return 1;
return 0;
}
display()
{ /* Function to display status of Queue */
inti;
if(Qempty()) printf(" \n Empty Queue\n");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",Q[i]);
printf("<-Rear");
}}
void main()
{ /* Main Program */
intopn,elem;
do
{
clrscr();
printf("\n ### Queue Operations using Arrays### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
Qinsert(elem);
break;
case 2: elem=Qdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3: printf("\n\nStatus of Queue\n\n");
display();
break;
case 4: printf("\n\n Terminating \n\n");
break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
getch();
}

OUTPUT:
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 1
Read the element to be Inserted ?100
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 1
Read the element to be Inserted ?200
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 1
Read the element to be Inserted ?300
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 2
Deleted Element is 100
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 3
Status of Queue
Front->200 300 <-Rear
Press a Key to Continue . . .

RESULT:
Thus a C program for Queue using array was implemented successfully.
EX NO : 8(a)
LINKED LIST IMPLEMENTATION OF LIST ADTS
Date:

AIM :
To write a C program for linked list implementation using list ADTs
ALGORITHM :
Step 1: Start the program
Step 2: Initialize the node and position of the list
Step 4: Insert and Delete function are used for inserting, deleting the element in the list
Step 5: Get the correct operation in the choice
Step 6: Display the elements in the list
Step 7 :Stop the program

PROGRAM :
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
int e;
Position next;
};
void Insert(int x, List l, Position p)
{
Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
printf("Memory out of space\n");
else
{
TmpCell->e = x;
TmpCell->next = p->next;
p->next = TmpCell;
}
}
int isLast(Position p)
{
return (p->next == NULL);
}
Position FindPrevious(int x, List l)
{
Position p = l;
while(p->next != NULL && p->next->e != x)
p = p->next;
return p;
}
void Delete(int x, List l)
{
Position p, TmpCell;
p = FindPrevious(x, l);

if(!isLast(p))
{
TmpCell = p->next;
p->next = TmpCell->next;
free(TmpCell);
}
else
printf("Element does not exist!!!\n");
}
void Display(List l)
{
printf("The list element are :: ");
Position p = l->next;
while(p != NULL)
{
printf("%d -> ", p->e);
p = p->next;
}
}
void Merge(List l, List l1)
{
int i, n, x, j;
Position p;
printf("Enter the number of elements to be merged :: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
p = l1;
scanf("%d", &x);
for(j = 1; j < i; j++)
p = p->next;
Insert(x, l1, p);
}
printf("The new List :: ");
Display(l1);
printf("The merged List ::");
p = l;
while(p->next != NULL)
{
p = p->next;
}
p->next = l1->next;
Display(l);
}
int main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->next = NULL;
List p = l;
printf("LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. MERGE\t 4. PRINT\t 5. QUIT\n\nEnter the choice :: ");
scanf("%d", &ch);
switch(ch)
{
case 1:p = l;
printf("Enter the element to be inserted :: ");
scanf("%d",&x);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;
case 2:p = l;
printf("Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;
case 3:l1 = (struct Node *) malloc(sizeof(struct Node));
l1->next = NULL;
Merge(l, l1);
break;
case 4:Display(l);
break;
}
}
while(ch<5);
return 0;
}
OUTPUT :

LINKED LIST IMPLEMENTATION OF LIST ADT

1. INSERT 2. DELETE 3. MERGE 4. PRINT 5. QUIT


Enter the choice :: 1
Enter the element to be inserted :: 10
Enter the position of the element :: 1
1. INSERT 2. DELETE 3. MERGE 4. PRINT 5. QUIT
Enter the choice :: 1
Enter the element to be inserted :: 20
Enter the position of the element :: 2
1. INSERT 2. DELETE 3. MERGE 4. PRINT 5. QUIT
Enter the choice :: 1
Enter the element to be inserted :: 30
Enter the position of the element :: 3
1. INSERT 2. DELETE 3. MERGE 4. PRINT 5. QUIT
Enter the choice :: 4
The list element are :: 10 -> 20 -> 30 ->
1. INSERT 2. DELETE 3. MERGE 4. PRINT 5. QUIT
Enter the choice :: 5

RESULT :

Thus a C program for list using linked list was implemented successfully.
EX NO : 8(b)
LINKED LIST IMPLEMENTATION OF STACK ADTS
Date:

AIM:

To write a C program to implement Stack operations such as push, pop and display using
linked list.

ALGORITHM:

Step 1: Start the program


Step 2: push operation inserts an element at the front.
Step 4: pop operation deletes an element at the front of the list;
Step 5: display operation displays all the elements in the list.
Step 6: Stop the program

PROGRAM:

#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
void pop();
void push(int value);
void display();
struct node
{
intdata;
struct node *link;
};
struct node *top=NULL,*temp;
void main()
{
int choice,data;
while(1) //infinite loop is used to insert/delete infinite number of elements in stack
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("\nEnterur choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: //To push a new element into stack
printf("Enter a new element :");
scanf("%d",&data);
push(data);
break;
case 2: // pop the element from stack
pop();
break;
case 3: // Display the stack elements
display();
break;
case 4: // To exit
exit(0);
}}
getch();
//return 0;
}
void display()
{
temp=top;
if(temp==NULL)
{
printf("\nStack is empty\n");
}
printf("\n The Contents of the Stack are...");
while(temp!=NULL)
{
printf(" %d ->",temp->data);
temp=temp->link;
}
}
void push(int data)
{
temp=(struct node *)malloc(sizeof(struct node)); // creating a space for the new element.
temp->data=data;
temp->link=top;
top=temp;
display();
}
void pop()
{
if(top!=NULL)
{
printf("The poped element is %d",top->data);
top=top->link;
}
else
{
printf("\nStack Underflow");
}
display();
}

OUTPUT:

1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :10
The Contents of the Stack are... 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :20
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :30
The Contents of the Stack are... 30 -> 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:2
The poped element is 30
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:3
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:4

RESULT:

Thus a C program for Stack using linked list was implemented successfully.
EX NO : 8(c)
LINKED LIST IMPLEMENTATION OF QUEUE ADTS
Date:

AIM:

To write a C program to implement Queue operations such as enqueue, dequeue and


display using linked list.

ALGORITHM:

Step 1: Start program


Step 2: enqueue operation inserts an element at the rear of the list.
Step 4: dequeueoperation deletes an element at the front of the list.
Step 5: display operation display all the element in the list.
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
voiddelet();
void display();
int item;
void main()
{
intch;
do
{
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:insert();
break;
case 2:delet();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\n\nInvalid choice. Please try again...\n");
}
} while(1);
getch();
}
void insert()
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else
{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}}
Voiddelet()
{
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
while(ptr != NULL)
{
printf("%d\t",ptr->info);
ptr = ptr->link;
}
}}
OUTPUT:

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Item deleted: 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice:3
15 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice:4

RESULT:

Thus a C program for Queue using linked list was implemented successfully.
EX NO : 9(a)
APPLICATIONS OF LIST ADTS
Date:

AIM:-
To write a ‘C ' program for implementing addition of two polynomials using list.

ALGORITHM:-

Step 1 : Start the program


Step 2 : Declare all the variables in the polynomial
Step 3 : Enter the first polynomial
Step 4 : Enter the second polynomial
Step 5 : To print the addition of polynomial
Step 6 : Stop the program

PROGRAM :-
#include <stdio.h>
typedef struct pnode
{
float coef;
int exp;
struct pnode *next;
}p;
p *getnode();
void main()
{
p *p1,*p2,*p3;
p *getpoly(),*add(p*,p*);
void display(p*);
clrscr();
printf(“\n enter first polynomial”);
p1=getpoly();
printf(“\n enter second polynomial”);
p2=getpoly();
printf(“\nthe first polynomial is”);
display(p1);
printf(“\nthe second polynomial is”);
display(p2);
p3=add(p1,p2);
printf(“\naddition of two polynomial is :\n”);
display(p3);
}
p *getpoly()
{
p *temp,*New,*last;
int flag,exp;
char ans;
float coef;
temp=NULL;
flag=1;
printf(“\nenter the polynomial in descending order of exponent”);
do
{
printf(“\nenter the coef & exponent of a term”);
scanf(“%f%d”,&coef,&exp);
New=getnode();
if(New==NULL)
printf(“\nmemory cannot be allocated”);
New->coef=coef;
New->exp=exp;
if(flag==1)
{
temp=New;
last=temp;
flag=0;
}
else
{
last->next=New;
last=New;
}
printf(“\ndou want to more terms”);
ans=getch();
}
while(ans==’y');
return(temp);
}
p *getnode()
{
p *temp;
temp=(p*) malloc (sizeof(p));
temp->next=NULL;
return(temp);
}
void display(p*head)
{
p*temp;
temp=head;
if(temp==NULL)
printf(“\npolynomial empty”);
while(temp->next!=NULL)
{
printf(“%0.1fx^%d+”,temp->coef,temp->exp);
temp=temp->next;
}
printf(“\n%0.1fx^%d”,temp->coef,temp->exp);
getch();
}
p*add(p*first,p*second)
{
p *p1,*p2,*temp,*dummy;
char ch;
float coef;
p *append(int,float,p*);
p1=first;
p2=second;
temp=(p*)malloc(sizeof(p));
if(temp==NULL)
printf(“\nmemory cannot be allocated”);
dummy=temp;
while(p1!=NULL&&p2!=NULL)
{
if(p1->exp==p2->exp)
{
coef=p1->coef+p2->coef;
temp=append(p1->exp,coef,temp);
p1=p1->next;
p2=p2->next;
}
else
if(p1->expexp)
{
coef=p2->coef;
temp=append(p2->exp,coef,temp);
p2=p2->next;
}
else
if(p1->exp>p2->exp)
{
coef=p1->coef;
temp=append(p1->exp,coef,temp);
p1=p1->next;
}
}
while(p1!=NULL)
{
temp=append(p1->exp,p1->coef,temp);
p1=p1->next;
}
while(p2!=NULL)
{
temp=append(p2->exp,p2->coef,temp);
p2=p2->next;
}
temp->next=NULL;
temp=dummy->next;
free(dummy);
return(temp);
}
p*append(int Exp,float Coef,p*temp)
{
p*New,*dum;
New=(p*)malloc(sizeof(p));
if(New==NULL)
printf(“\ncannot be allocated”);
New->exp=Exp;
New->coef=Coef;
New->next=NULL;
dum=temp;
dum->next=New;
dum=New;
return(dum);
}

OUTPUT :

Enter first polynomial


Enter the polynomial in descending order of exponent
Enter the coefficient& exponent of a term 3 3
Dou want to more terms (y/n)
Enter the coefficient& exponent of a term 2 2
Dou want to more terms (y/n)
Enter the coefficient& exponent of a term 1 1
Dou want to more terms (y/n)
Enter first polynomial
Enter the polynomial in descending order of exponent
Enter the coefficient& exponent of a term 5 5
Dou want to more terms (y/n)
Enter the coefficient& exponent of a term 3 2
Dou want to more terms (y/n)
Enter the coefficient& exponent of a term 7 0
Dou want to more terms (y/n)
The first polynomial is
3.0x^3+2.0x^2+1.0x^1
The second polynomial is
5.0x^5+3.0x^2+7.0x^0
The addition of two polynomials is…
5.0x^5+3.0x^3+5.0x^2+1.0x^1+7.0x^0

RESULT :
Thus the C program for implement the concept of addition of two polynomials using list
was implemented successfully.
EX NO : 9(b)
APPLICATIONS OF STACK ADTS
Date:

AIM:-
To write a ‘C ' program that checks if expression is correctly parenthesized using stack.

ALGORITHM:-

Step 1 : Start the program


Step 2 : Initialize all the variables of the program
Step 3 : Get the expression to check the parenthesized
Step 4: Push and pop function to check the expression
Step 5 : Given expression is balanced means valid expression
Step 6 : Given expression is unbalanced means invalid expression
Step 7 : Stop the program

PROGRAM :-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int top =-1;
char stack[100];
void push(char);
void pop();
void find_top();
void main()
{
int i;
char a[100];
printf("enter expression\n");
scanf("%s",&a);
for(i =0; a[i]!='\0';i++)
{
if(a[i]=='(')
{
push(a[i]);
}
elseif(a[i]==')')
{
pop(); }
}
find_top();
}
void push(char a)
{
stack[top]= a;
top++;
}
void pop()
{
if(top ==-1)
{
printf("expression is invalid\n");
exit(0);
}
else
{
top--;
}}
void find_top()
{
if(top ==-1)
printf("\nexpression is valid\n");
else
printf("\nexpression is invalid\n");
}

OUTPUT :

enter expression
(a+b)
expression is valid
enter expression
(a+b))
expression is invalid

RESULT :
Thus the C program that checks if expression is correctly parenthesized using
stack was implemented successfully.
EX NO : 9(c)
APPLICATIONS OF QUEUES ADTS
Date:

AIM:-
To write a C program for implementing the concept of priority queue.

ALGORITHM:-

Step 1 : Start the program


Step 2 : Declare all the variables in priority queue
Step 3 : Get the elements for insertion and deletion
Step 4 : Display the queue elements
Step 5 : If choice is correct enter the correct choice.
Step 6 : Stop the program

PROGRAM :-
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();
int pri_que[MAX];
int front, rear;
void main()
{
int n, ch;
printf("\n1 - Insert an element into queue");
printf("\n2 - Delete an element from queue");
printf("\n3 - Display queue elements");
printf("\n4 - Exit");
create();
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case1:printf("\nEnter value to be inserted : ");
scanf("%d",&n);
insert_by_priority(n);
break;
case2:printf("\nEnter value to delete : ");
scanf("%d",&n);
delete_by_priority(n);
break;
case3:
display_pqueue();
break;
case4:
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}
void create()
{
front = rear =-1;
}
void insert_by_priority(int data)
{
if(rear >= MAX -1)
{printf("\nQueue overflow no more elements can be inserted");
return;
}
if((front ==-1)&&(rear ==-1))
{
front++;
rear++;
pri_que[rear]= data;
return;
}
else
check(data);
rear++;
}
void check(int data)
{
int i,j;
for(i =0; i <= rear; i++)
{
if(data >= pri_que[i])
{
for(j = rear +1; j > i; j--)
{
pri_que[j]= pri_que[j -1];
}
pri_que[i]= data;
return;
}
}
pri_que[i]= data;
}
void delete_by_priority(int data)
{
int i;
if((front==-1)&&(rear==-1))
{
printf("\nQueue is empty no elements to delete");
return;
}
for(i =0; i <= rear; i++)
{
if(data == pri_que[i])
{
for(; i < rear; i++)
{
pri_que[i]= pri_que[i +1];
}
pri_que[i]=-99;
rear--;
if(rear ==-1)
front =-1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}
void display_pqueue()
{
if((front ==-1)&&(rear ==-1))
{
printf("\nQueue is empty");
return;
}
for(; front <= rear; front++)
{
printf(" %d ", pri_que[front]);
}
front =0;
}

OUTPUT :

1 - Insert an element into queue


2 - Delete an element from queue
3 - Display queue elements
4 - Exit
Enter your choice : 1
Enter value to be inserted : 20
Enter your choice : 1
Enter value to be inserted : 45
Enter your choice : 1
Enter value to be inserted : 89
Enter your choice : 3
89 45 20
Enter your choice : 1
Enter value to be inserted : 56
Enter your choice : 3
89 56 45 20
Enter your choice : 2
Enter value to delete : 45
Enter your choice : 3
89 56 20
Enter your choice : 4

RESULT :
Thus the C program for the concept of priority queue was implemented
successfully.
EX NO : 10 IMPLEMETATION OF BINARY TREES AND OPERATIONS OF BINARY
TREES
Date:

AIM:-

To write a ‘C ' program for implementing the binary tree and operations of binary tree.

ALGORITHM:-

Step 1: Start the process.


Step 2: Initialize and declare variables.
Step 3: Construct the Tree
Step 4: Data values are given which we call a key and abinary tree
Step 5: To search for the key in the given binary tree, start with the root node and
Compare the key with the data value of the root node.
Step 6: Otherwise, repeat the same process with the right sub tree until either a match is
found or the subtree under consideration becomes an empty tree.
Step 7: Stop the process.

PROGRAM :-
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}

void print_preorder(node * tree)


{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
node *root;
node *tmp;
//int i;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
/* Printing nodes of tree */
printf("Pre Order Display\n");
print_preorder(root);
printf("In Order Display\n");
print_inorder(root);
printf("Post Order Display\n");
print_postorder(root);
/* Search node into tree */
tmp = search(&root, 4);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
}

OUTPUT :
Pre Order Display
9
4
2
6
15
12
17
In Order Display
2
4
6
9
12
15
17
Post Order Display
2
6
4
12
17
15
9
Searched node=4

RESULT :
Thus the C program to implement the concept of binary tree and operations of
binary tree was implemented successfully.
EX NO :11
IMPLEMETATION OF BINARY SEARCH
Date :

AIM :
To write a C program to implement the binary search.

ALGORITHM :

Step 1 : Start the program.


Step 2 : Set two pointers low and high at the lowest and the highest positions respectively.
Step 3 : Find the middle element mid of the array ie. arr[(low + high)/2] = 6.
Step 4 : If x == mid, then return mid.Else, compare the element to be searched with m.
Step 5 : If x > mid, compare x with the middle element of the elements on the right side of mid.
This is done by setting low to low = mid + 1.
Step 6 : Stop .

PROGRAM :

// Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {


// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;

if (array[mid] == x)
return mid;

if (array[mid] < x)
low = mid + 1;

else
high = mid - 1;
}

return -1;
}

int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}

OUTPUT :

Element is found at index 1,

RESULT :

Thus the C program for binary search was implemented successfully


EX NO :12
IMPLEMETATION OF SEARCHING TECHNIQUES
Date :

AIM :
To write a C program to implement of searching techniques.

ALGORITHM :

Step 1: Start the program.


Step 2: Sequential search starts at the beginning of the list and checks every element of the list.
Step 3: It is a basic and simple search algorithm.
Step 4: Sequential search compares the element with all the other elements given in the list.
Step 5: If the element is matched, it returns the value index, else it returns -1.
Step 6:Stop.

PROGRAM :

#include <stdio.h>
int main()
{
int arr[50], search, cnt, num;

printf("Enter the number of elements in array\n");


scanf("%d",&num);

printf("Enter %d integer(s)\n", num);

for (cnt = 0; cnt < num; cnt++)


scanf("%d", &arr[cnt]);

printf("Enter the number to search\n");


scanf("%d", &search);

for (cnt = 0; cnt < num; cnt++)


{
if (arr[cnt] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, cnt+1);
break;
}
}
if (cnt == num)
printf("%d is not present in array.\n", search);

return 0;
}
OUTPUT :

Enter the number of elements in array


5
Enter 5 integer(s)
20
30
6
46
78
Enter the number to search
46
46 is present at location 4.

RESULT :

Thus the C program for searching techniques was implemented successfully.

55
EX NO :13(a)
IMPLEMETATION OF INSERTION SORT
Date :

AIM :
To write a C program to implement the insertion sort.

ALGORITHM :

Step 1 : Start the program.


Step 2 : The first element in the array is assumed to be sorted. Take the second element and
store it separately in key.
Step 3 : Now, the first two elements are sorted.
Step 4 : Similarly, place every unsorted element at its correct position.
Step 5 : Stop.

PROGRAM :

#include <stdio.h>

// Function to print an array


void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
}

void insertionSort(int array[], int size) {


for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;

// Compare key with each element on the left of it until an element smaller than
// it is found.
// For descending order, change key<array[j] to key>array[j].
while (key < array[j] && j >= 0) {
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}

// Driver code
int main() {
int data[] = {9, 5, 1, 4, 3};

56
int size = sizeof(data) / sizeof(data[0]);
insertionSort(data, size);
printf("Sorted array in ascending order:\n");
printArray(data, size);
}

OUTPUT :

Sorted array in ascending order:

13459

RESULT :

Thus the C program for insertion sort was implemented successfully.

57
EX NO :13(b)
IMPLEMETATION OF MERGE SORT
Date :

AIM :
To write a C program to implement the merge sort.

ALGORITHM :

Step 1 : Start the program.


Step 2 : Create duplicate copies of sub-arrays to be sorted.
Step 3 : Maintain current index of sub-arrays and main array.
Step 4 : Until we reach the end of either L or M, pick larger among elements L and M and
place them in the correct position at A[p..r].
Step 5 : When we run out of elements in either L or M, pick up the remaining elements and
put in A[p..r].
Step 6 : Stop .

PROGRAM :

#include <stdio.h>

// Merge two subarrays L and M into arr


void merge(int arr[], int p, int q, int r) {

// Create L ← A[p..q] and M ← A[q+1..r]


int n1 = q - p + 1;
int n2 = r - q;

int L[n1], M[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];

// Maintain current index of sub-arrays and main array


int i, j, k;
i = 0;
j = 0;
k = p;

// Until we reach either end of either L or M, pick larger among


// elements L and M and place them in the correct position at A[p..r]
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i];

58
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}

// When we run out of elements in either L or M,


// pick up the remaining elements and put in A[p..r]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = M[j];
j++;
k++;
}
}

// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {

// m is the point where the array is divided into two subarrays


int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

// Merge the sorted subarrays


merge(arr, l, m, r);
}
}

// Print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);

59
mergeSort(arr, 0, size - 1);

printf("Sorted array: \n");


printArray(arr, size);
}

OUTPUT :

Sorted array:

1 5 6 9 10 12

RESULT :

Thus the C program for merge sort was implemented successfully

60
EX NO :13(c)
IMPLEMETATION OF QUICK SORT
Date :

AIM :
To write a C program to implement the quick sort.

ALGORITHM :

Step 1: Start the program.


Step 2: A pointer is fixed at the pivot element. The pivot element is compared with the
elements beginning from the first index.
Step 3: If the element is greater than the pivot element, a second pointer is set for that
element.
Step 4: Again, the process is repeated to set the next greater element as the second pointer.
And, swap it with another smaller element.
Step 5:Stop.

PROGRAM :

#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);

for (int j = start; j <= end - 1; j++)


{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}

/* function to implement quick sort */


void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end =
Ending index */
{

61
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}

void printArr(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);

return 0;
}

OUTPUT :

Before sorting array elements are -


24 9 29 14 19 27
After sorting array elements are -
9 14 19 24 27 29

RESULT :

Thus the C program for quick sort was implemented successfully.

62
EX NO : 14
HASHING – ANY TWO COLLISION TECHNIQUES
Date:

AIM:
To write a C program to implement the concept of Hash functions.
ALGORITHM:
Step 1: Start the program
Step 2: Choose any element of the array to be the pivot.
Step 3: Divide all other elements (except the pivot) into two partitions.
o All elements less than the pivot must be in the first partition.
o All elements greater than the pivot must be in the second partition.
Step 4: Use recursion to sort both partitions.
Step 5: Join the first sorted partition, the pivot, and the second sorted partition.
Step 6: Tables are sorted in linear partition.
Step 6: Stop the program

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n, value;
int temp, hash;
clrscr();
printf("nEnter the value of n(table size):");
scanf("%d", &n);
do {
printf("nEnter the hash value");
scanf("%d", &value);
hash = value % n;
if (a[hash] == 0) {
a[hash] = value;
printf("na[%d]the value %d is stored", hash, value);
}
else
{
for (hash++; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
63
}}
for (hash = 0; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}}
printf("nnERRORn");
printf("nEnter '0' and press 'Enter key' twice to exit");
}
menu:
printf("n Do u want enter more");
scanf("%d", &temp);
}
while (temp == 1);
getch();
}

OUTPUT:
Collision Handling By Linear Probing
Enter The Number:131
Do you wish to continue? (y/n):y
Enter The Number:21
Do you wish to continue? (y/n):y
Enter The Number:3
Do you wish to continue? (y/n):y
Enter The Number:4
Do you wish to continue? (y/n):y
Enter The Number:5
Do you wish to continue? (y/n):y
Enter The Number:8
Do you wish to continue? (y/n):y
Enter The Number:9
Do you wish to continue? (y/n):n
The hash table is…
0 18
1 131
2 21
3 3
4 4
5 5
6 -1
7 -1
8 8
9 9

64
RESULT:
Thus a C program for the concept of Hash function and collision resolution was implemented
successfully.

65
66

You might also like