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

Programs

The document contains code for implementing a double linked list data structure in C programming language. It includes functions to insert a node, delete a node, find a node, display all nodes, and check if a node is the last node. The main function initializes an empty list and provides a menu to test the list operations by calling the respective functions and taking user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Programs

The document contains code for implementing a double linked list data structure in C programming language. It includes functions to insert a node, delete a node, find a node, display all nodes, and check if a node is the last node. The main function initializes an empty list and provides a menu to test the list operations by calling the respective functions and taking user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Program for Binary Search

#include<stdio.h>
#include<conio.h>

#define MAX_SIZE 5

void binary_search(int[],int);

int main() {
int arr_search[MAX_SIZE], i,element;

printf("Simple Binary Search Example - Array and Functions\n");


printf("\nEnter %d Elements for Searching : \n", MAX_SIZE);
for (i = 0; i < MAX_SIZE; i++)
scanf("%d", &arr_search[i]);

printf("Enter Element to Search : ");


scanf("%d", &element);

binary_search(arr_search,element);
getch();
}

void binary_search(int fn_arr[],int element) {


int f = 0, r = MAX_SIZE,mid;

while (f <= r) {
mid = (f+r)/2;

if (fn_arr[mid] == element) {
printf("\nSearch Element : %d : Found : Position : %d.\n", element,
mid+1);
break;
}
else if (fn_arr[mid] < element)
f = mid + 1;
else
r = mid - 1;
}

if (f > r)
printf("\nSearch Element : %d : Not Found \n", element);
}

Sample Output
Simple Binary Search Example - Array and Functions

Enter 5 Elements for Searching :


1001
1020
3002
4001
5000
Enter Element to Search : 3002

Search Element : 3002 : Found : Position : 3.


Program for Linear Search

#include<stdio.h>
#include<conio.h>

#define MAX_SIZE 5

void linear_search(int[], int);

int main() {
int arr_search[MAX_SIZE], i, element;

printf("Simple Linear Search Example - Array and Functions\n");


printf("\nEnter %d Elements for Searching : \n", MAX_SIZE);
for (i = 0; i < MAX_SIZE; i++)
scanf("%d", &arr_search[i]);

printf("Enter Element to Search : ");


scanf("%d", &element);

//Linear Search Function


linear_search(arr_search, element);

getch();
}

void linear_search(int fn_arr[], int element) {


int i;

/* for : Check elements one by one - Linear */


for (i = 0; i < MAX_SIZE; i++) {
/* If for Check element found or not */
if (fn_arr[i] == element) {
printf("Linear Search : %d is Found at array : %d.\n", element, i + 1);
break;
}
}

if (i == MAX_SIZE)
printf("\nSearch Element : %d : Not Found \n", element);
}

Sample Output:
Output 1:

Simple Linear Search Example - Array and Functions

Enter 5 Elements for Searching :


900
333
21
16
24
Enter Element to Search : 16
Linear Search : 16 is Found at array : 4.

Output 2:

Simple Linear Search Example - Array and Functions

Enter 5 Elements for Searching :


90
32
323
11
22
Enter Element to Search : 33 Search Element : 33 : Not Found

Program for Stack


1. #include <stdio.h>
2. #define MAXSIZE 5
3.
4. struct stack
5. {
6. int stk[MAXSIZE];
7. int top;
8. };
9. typedef struct stack STACK;
10. STACK s;
11.
12. void push(void);
13. int pop(void);
14. void display(void);
15.
16. void main ()
17. {
18. int choice;
19. int option = 1;
20. s.top = -1;
21.
22. printf ("STACK OPERATION\n");
23. while (option)
24. {
25. printf ("------------------------------------------\n");
26. printf (" 1 --> PUSH \n");
27. printf (" 2 --> POP \n");
28. printf (" 3 --> DISPLAY \n");
29. printf (" 4 --> EXIT \n");
30. printf ("------------------------------------------\n");
31.
32. printf ("Enter your choice\n");
33. scanf ("%d", &choice);
34. switch (choice)
35. {
36. case 1:
37. push();
38. break;
39. case 2:
40. pop();
41. break;
42. case 3:
43. display();
44. break;
45. case 4:
46. return;
47. }
48. fflush (stdin);
49. printf ("Do you want to continue(Type 0 or 1)?\n");
50. scanf ("%d", &option);
51. }
52. }
53. /* Function to add an element to the stack */
54. void push ()
55. {
56. int num;
57. if (s.top == (MAXSIZE - 1))
58. {
59. printf ("Stack is Full\n");
60. return;
61. }
62. else
63. {
64. printf ("Enter the element to be pushed\n");
65. scanf ("%d", &num);
66. s.top = s.top + 1;
67. s.stk[s.top] = num;
68. }
69. return;
70. }
71. /* Function to delete an element from the stack */
72. int pop ()
73. {
74. int num;
75. if (s.top == - 1)
76. {
77. printf ("Stack is Empty\n");
78. return (s.top);
79. }
80. else
81. {
82. num = s.stk[s.top];
83. printf ("poped element is = %dn", s.stk[s.top]);
84. s.top = s.top - 1;
85. }
86. return(num);
87. }
88. /* Function to display the status of the stack */
89. void display ()
90. {
91. int i;
92. if (s.top == -1)
93. {
94. printf ("Stack is empty\n");
95. return;
96. }
97. else
98. {
99. printf ("\n The status of the stack is \n");
100. for (i = s.top; i >= 0; i--)
101. {
102. printf ("%d\n", s.stk[i]);
103. }
104. }
105. STACK OPERATION
106. printf ("\n");
107. }
Output:
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
0
$ a.out
STACK OPERATION
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT

Program for Queue:


#Include<Stdio.H>
#Include<Conio.H>
#Define Max 5
Int Q[10],Front=0,Rear=-1;
Void Main()
{
Int Ch;
Void Insert();
Void Delet();
Void Display();
Clrscr();
Printf("\Nqueue Operations\N");
Printf("1.Insert\N2.Delete\N3.Display\N4.Exit\N");
While(1)
{
Printf("Enter Your Choice:");
Scanf("%D",&Ch);
Switch(Ch)
{
Case 1: Insert();
Break;
Case 2: Delet();
Break;
Case 3:Display();
Break;
Case 4:Exit();
Default:Printf("Invalid Option\N");
}
}
}

Void Insert()
{
Int X;
If(Rear==Max-1)
Printf("Queue Is Overflow\N");
Else
{
Printf("Enter Element To Be Insert:");
Scanf("%D",&X);
Q[++Rear]=X;
}
}
Void Delet()
{
Int A;
If((Front==0)&&(Rear==-1))
{
Printf("Queue Is Underflow\N");
Getch();
Exit();
}
A=Q[Front++];
Printf("Deleted Element Is:%D\N",A);
If(Front>Rear)
{
Front=0;
Rear=-1;
}
}

Void Display()
{
Int I;
If(Front==0&&Rear==-1)
{
Printf("Queue Is Underflow\N");
Getch();
Exit();
}
For(I=Front;I<=Rear;I++)
Printf("\T%D",Q[I]);
Printf("\N");
}
Getch();

Program for Double Linked List


#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node
{
int e;
Position previous;
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->previous = p;
TmpCell->next = p->next;
p->next = TmpCell;
}
}

int isLast(Position p)
{
return (p->next == NULL);
}

Position Find(int x, List l)


{
Position p = l->next;
while(p != NULL && p->e != x)
p = p->next;
return p;
}

void Delete(int x, List l)


{
Position p, p1, p2;
p = Find(x, l);
if(p != NULL)
{
p1 = p -> previous;
p2 = p -> next;
p1 -> next = p -> next;
if(p2 != NULL) // if the node is not the last node
p2 -> previous = p -> previous;
}
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 main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->previous = NULL;
l->next = NULL;
List p = l;
printf("DOUBLY LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5. QUIT\n\nEnter the choi
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:
p = l;
printf("Enter the element to be searched :: ");
scanf("%d",&x);
p = Find(x,p);
if(p == NULL)
printf("Element does not exist!!!\n");
else
printf("Element exist!!!\n");
break;

case 4:
Display(l);
break;
}
}
while(ch<5);
}

OUTPUT:
DOUBLY LINKED LIST IMPLEMENTATION OF LIST ADT

1. INSERT 2. DELETE 3. FIND 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. FIND 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. FIND 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. FIND 4. PRINT 5. QUIT

Enter the choice :: 4


The list element are :: 10 -> 20 -> 30 ->

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

Enter the choice :: 3


Enter the element to be searched :: 20 Element exist!!!
Program for Circular Linked List
#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, List l)


{
return (p->next == l);
}

Position FindPrevious(int x, List l)


{
Position p = l;
while(p->next != l && p->next->e != x)
p = p->next;
return p;
}

Position Find(int x, List l)


{
Position p = l->next;
while(p != l && p->e != x)
p = p->next;
return p;
}

void Delete(int x, List l)


{
Position p, TmpCell;
p = FindPrevious(x, l);
if(!isLast(p, l))
{
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 != l)
{
printf("%d -> ", p->e);
p = p->next;
}
}

void main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->next = l;
List p = l;
printf("CIRCULAR LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5. QUIT\n\nEnter the choi
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:
p = l;
printf("Enter the element to be searched :: ");
scanf("%d",&x);
p = Find(x,p);
if(p == l)
printf("Element does not exist!!!\n");
else
printf("Element exist!!!\n");
break;

case 4:
Display(l);
break;
}
}while(ch<5);
return 0;
}

OUTPUT:
CIRCULAR LINKED LIST IMPLEMENTATION OF LIST ADT

1. INSERT 2. DELETE 3. FIND 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. FIND 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. FIND 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. FIND 4. PRINT 5. QUIT

Enter the choice :: 4


The list element are :: 10 -> 20 -> 30 ->

Program for Quick Sort


#include <stdio.h>
#define MAX 10

void swap(int *m,int *n)


{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
int get_key_position(int x,int y )
{
return((x+y) /2);
}

// Function for Quick Sort


void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = get_key_position(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
}

// Function to read the data


void read_data(int list[],int n)
{
int j;
printf("\n\nEnter the elements:\n");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
}

// Function to print the data


void print_data(int list[],int n)
{
int j;
for(j=0;j<n;j++)
printf("%d\t",list[j]);
}

main()
{
int list[MAX], num;
//clrscr();
printf("\n***** Enter the number of elements Maximum [10] *****\n");
scanf("%d",&num);
read_data(list,num);
printf("\n\nElements in the list before sorting are:\n");
print_data(list,num);
quicksort(list,0,num-1);
printf("\n\nElements in the list after sorting are:\n");
print_data(list,num);
//getch();
}

OUTPUT:

***** Enter the number of elements Maximum [10] *****


6

Enter the elements:


56
26
16
66
06
36

Elements in the list before sorting are:


56 26 16 66 6 36

Elements in the list after sorting are:


6 16 26 36 56 66

Program for Bubble Sort


1. #include <stdio.h>
2.
3. void bubble_sort(long [], long);
4.
5. int main()
6. {
7. long array[100], n, c, d, swap;
8.
9. printf("Enter number of elements\n");
10. scanf("%ld", &n);
11.
12. printf("Enter %ld integers\n", n);
13.
14. for (c = 0; c < n; c++)
15. scanf("%ld", &array[c]);
16.
17. bubble_sort(array, n);
18.
19. printf("Sorted list in ascending order:\n");
20.
21. for (c = 0; c < n; c++)
22. printf("%ld\n", array[c]);
23.
24. return 0;
25. }
26.
27. void bubble_sort(long list[], long n)
28. {
29. long c, d, t;
30.
31. for (c = 0 ; c < n - 1; c++)
32. {
33. for (d = 0 ; d < n - c - 1; d++)
34. {
35. if (list[d] > list[d+1])
36. {
37. /* Swapping */
38.
39. t = list[d];
40. list[d] = list[d+1];
41. list[d+1] = t;
42. }
43. }
44. }
45. }

Program for merge sort


#include<stdio.h>
#include<conio.h>

#define MAX_SIZE 5

void merge_sort(int, int);


void merge_array(int, int, int, int);

int arr_sort[MAX_SIZE];

int main() {
int i;

printf("Simple Merge Sort Example - Functions and Array\n");


printf("\nEnter %d Elements for Sorting\n", MAX_SIZE);
for (i = 0; i < MAX_SIZE; i++)
scanf("%d", &arr_sort[i]);

printf("\nYour Data :");


for (i = 0; i < MAX_SIZE; i++) {
printf("\t%d", arr_sort[i]);
}

merge_sort(0, MAX_SIZE - 1);

printf("\n\nSorted Data :");


for (i = 0; i < MAX_SIZE; i++) {
printf("\t%d", arr_sort[i]);
}
getch();

void merge_sort(int i, int j) {


int m;
if (i < j) {
m = (i + j) / 2;
merge_sort(i, m);
merge_sort(m + 1, j);
// Merging two arrays
merge_array(i, m, m + 1, j);
}
}

void merge_array(int a, int b, int c, int d) {


int t[50];
int i = a, j = c, k = 0;

while (i <= b && j <= d) {


if (arr_sort[i] < arr_sort[j])
t[k++] = arr_sort[i++];
else
t[k++] = arr_sort[j++];
}

//collect remaining elements


while (i <= b)
t[k++] = arr_sort[i++];

while (j <= d)
t[k++] = arr_sort[j++];

for (i = a, j = 0; i <= d; i++, j++)


arr_sort[i] = t[j];
}

Sample Output:
Simple Merge Sort Example - Functions and Array
Enter 5 Elements for Sorting
67
57
45
32
13

Your Data : 67 57 45 32 13
Sorted Data : 13 32 45 57 67

You might also like