Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Lab Cycle

Uploaded by

Pavan Malladi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab Cycle

Uploaded by

Pavan Malladi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 82

Data Structure using C

Lab Cycle:

1. How to insert element into the Array

2. How to delete the element into the Array

3. How to update the element into the Array

4. Stack Using Array

5. Stack Using Linked List

6. Queue Using Array

7. Queue Using Linked List

8. Bubble Sort

9. Insertion Sort

10. Merge Sort

11. Linear Search

12. Binary Search

13. Single Linked List

14. Double Linked List

15. Binary Tree Traversals Methods

16. Death First Search

17. Breadth First Search

18. Deque

19. Insert node in BST

20. Delete a node in BST

21. Binary Tree Traversing Methods

22.
1) How to insert element into the Array

#include <stdio.h>

#include<conio.h>

void main()

int n,arr[20],i, pos,ele;

clrscr();

printf(“enter the size of the array\n”);

scanf(“%d”,&n);

printf(“enter the array elements \n”);

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

scanf(“%d”,&arr[i]);

pirntf(“enter the inserted element postion\n”);

scanf(“%d”,&pos);

printf(“enter the inserted element:\n”);

scanf(“%d”,&ele);

if(pos > n)

printf(“Invalid Input”);

else

for (i = n – 1; i >= pos – 1; i--)

arr[i+1] = arr[i];

arr[pos-1] = ele;

printf(“Array after insertion is:\n”);

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


printf(“%d\n”, arr[i]);

getch();

Output:

Enter the size of the array

Enter the array elements


1
2
3
4
5

Enter the inserted element position


4

Enter the inserted element


10
Array after insertion is:

1 2 3 10 4 5

2) How to delete the element into the Array

#include <stdio.h>

#include<conio.h>

void main()

int arr[20], pos, i, n;

clrscr();

printf(“Enter the size of the array : \n“);

scanf(“%d”, &n);

printf(“Enter the array elements : “);

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

scanf(“%d”, &arr[i]);
}

printf(“Enter the deleted element position :\n “);

scanf(“%d”, &pos);

if (pos >= n+1)

printf(“Deletion not possible.\n”);

else

for (i = pos – 1; i < n – 1; i++)

arr[i] = arr[i+1];

printf(“After array deletion : \n“);

for (i = 0; i < n – 1; i++)

printf(“%d\n”, arr[i]);

getch();

Output:

Enter the size of the array

Enter the array elements


1
2
3
4
5

Enter the deleted element position


4
Array after insertion is:

1 2 3 5
3) How to update the element into the Array

#include<stdio.h>

#include<conio.h>

void main()

int a[10],i,pos,n,s;

clrscr();

printf("Enter the size of the array:\n");

scanf("%d",&n);

printf("Enter the array elements:\n");

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

scanf("%d",&a[i]);

printf("Enter the position to be update:\n");

scanf("%d",&pos);

printf("Enter the updated element:\n");

scanf("%d",&s);

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

if(i==pos)

a[i]=s;

printf("\nAfter Updated value is:");

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

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

}
getch();

Output
Enter the size of the array

Enter the array elements


1
2
3
4
5

Enter the position to be update:


3
Enter the updated element:
10
After Updated values are:
1 2 10 4 5

4) Stack Using Array

#include<stdio.h>

#include<conio.h>

int stack[10],choice,n,top,x,i;

void push();

void pop();

void display();

void main()

clrscr();

top = -1;

printf("Enter the size of STACK :\n ");

scanf("%d",&n);

printf("STACK IMPLEMENTATION USING ARRAYS\n");


do

printf("1.PUSH \n 2.POP\n 3.DISPLAY\n 4.EXIT\n");

printf("\nEnter the choice : ");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf ("\nInvalid Choice\n");


}

while(choice!=4);

void push()

if(top >= n - 1)

printf("STACK OVERFLOW\n");

else

printf("Enter a value to be pushed : ");

scanf("%d",&x);

top++;

stack[top] = x;

void pop()

if(top <= -1)

printf("STACK UNDERFLOW\n");

else

printf("The popped element is %d", stack[top]);

top--;

}
void display()

if(top >= 0)

printf("ELEMENTS IN THE STACK\n");

for(i = top ; i >= 0 ; i--)

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

else

printf("EMPTY STACK\n");

Output:
5) Stack Using Linked List

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct Node

int data;

struct Node *next;

}*top = NULL;

void push(int);

void pop();

void display();

void main()

int choice, value;

clrscr();

printf("\nIMPLEMENTING STACKS USING LINKED LISTS\n");

while(1)

printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");

printf("\nEnter your choice : ");

scanf("%d",&choice);

switch(choice)

case 1:

printf("\nEnter the value to insert: ");

scanf("%d", &value);

push(value);

break;

case 2:
pop();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nInvalid Choice\n");

void push(int value)

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

if(top == NULL)

newNode->next = NULL;

else

newNode->next = top;

top = newNode;

printf("Node is Inserted\n\n");

void pop()

if(top == NULL)

printf("\nEMPTY STACK\n");

else
{

struct Node *temp = top;

printf("\nPopped Element : %d", temp->data);

printf("\n");

top = temp->next;

free(temp);

void display()

if(top == NULL)

printf("\nEMPTY STACK\n");

else

printf("The stack is \n");

struct Node *temp = top;

while(temp->next != NULL)

printf("%d--->",temp->data);

temp = temp -> next;

printf("%d--->NULL\n\n",temp->data);

}
Output:
6) Queue Using Array

#include <stdio.h>

#include<conio.h>

#define MAX 50

void insert();

void delete();

void display();

int queue_array[MAX];

int rear = - 1;

int front = - 1;

void main()

int choice;

clrscr();

while(1)

printf("1.Insert\n");

printf("2.Delete\n");

printf("3.Display\n");

printf("4.Quit \n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch (choice)

case 1:

insert();

break;

case 2:

delete();
break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice \n");

void insert()

int item;

if (rear == MAX - 1)

printf("Queue Overflow \n");

else

if (front == - 1)

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &item);

rear = rear + 1;

queue_array[rear] = item;

void delete()

if (front == - 1 || front > rear)


{

printf("Queue Underflow \n");

return ;

else

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

void display()

int i;

if (front == - 1)

printf("Queue is empty \n");

else

printf("Queue is : \n");

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

printf("%d ", queue_array[i]);

printf("\n");

}
Output:

7) Queue Using Linked List

#include <stdio.h>

#include<conio.h>

#include <stdlib.h>

struct node

int data;

struct node *next;

} *rear,*front,*temp,*newNode;

void create()

front = rear = NULL;

void insertqueue(int data)

newNode = (struct node*) malloc(sizeof(struct node));

newNode -> data = data;

newNode -> next = NULL;


if(rear != NULL)

rear -> next = newNode;

rear = newNode;

else

front = rear = newNode;

int deletequeue()

int data;

data = front -> data;

temp = front;

front = front -> next;

free(temp);

if(front == NULL)

rear = NULL;

return data;

int empty()

if(front == NULL)

return 1;

else

return 0;

void display()

{
if(empty())

printf("\nEMPTY QUEUE\n");

else

temp = front;

printf("\nQUEUE ELEMENTS : ");

while(temp != NULL)

printf("%d ",temp -> data);

temp = temp -> next;

void main()

int num,choice;

clrscr();

while(1)

printf("\n\nQUEUE OPERATIONS\n\n1.INSERT\n2.DELETE\n3.DISPLAY\n\n");

scanf("%d",&choice);

switch (choice)

case 1:

printf("\nEnter item : ");

scanf("%d",&num);

enqueue(num);

break;

case 2:

if(!(empty()))
printf("\nDeleted queue element : %d",deletequeue());

break;

case 3:

display();

break;

default:

exit(0);

getch();

Output:
8) Bubble Sort

#include<stdio.h>

#include<conio.h>

void main()

int n, i, j, temp;

int arr[20];

clrscr();

printf(“enter the size of Bubble sort\n”);

scanf("%d", &n);

printf(enter the Bubble sort elements\n”);

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

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

for(i = 0; i < n - 1; i++)

for(j = 0; j < n - i - 1; j++)

if(arr[j] > arr[j + 1])

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

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

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


}

Output:

Enter the size of the Bubble sort

10

Enter the Bubble sort elements

66 33 11 88 99 77 55 22 00 44

00 11 22 33 44 55 66 77 88 99

9) Selection Sort

#include<stdio.h>

#include<conio.h>

void main()

int n, i, j, temp, min,arr[30];

clrscr();

printf(“enter the size of Selection sort\n”);

scanf("%d", &n);

printf(enter the Selection sort elements\n”);

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

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

for (i = 0; i < n - 1; i++)

min = i;

for (j = i + 1; j < n; j++)

if (arr[j] < arr[min])

min = j;

temp = arr[min];
arr[min] = arr[i];

arr[i] = temp;

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

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

Output:

Enter the size of the Selection sort

10

Enter the Selection sort elements

66 33 11 88 99 77 55 22 00 44

00 11 22 33 44 55 66 77 88 99

10) Insertion Sort

#include<stdio.h>

#include<conio.h>

void main()

int i, n, j, temp, min, key, arr[30];

clrscr();

printf(“enter the size of Insertion sort\n”);

scanf("%d", &n);

printf(enter the Insertion sort elements\n”);

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

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

for (i = 1; i < n; i++)


{

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key)

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

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

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

Output:

Enter the size of the Insertion sort

10

Enter the Insertion sort elements

66 33 11 88 99 77 55 22 00 44

00 11 22 33 44 55 66 77 88 99

11) Merge Sort

#include <stdio.h>

#include<conio.h>

int divide(int arr[], int start, int end)

if(start < end)

int mid;
mid = (start + end) / 2;

divide(arr, start, mid);

divide(arr, mid + 1, end);

merge(arr, start, mid, end);

int merge(int arr[], int start, int mid, int end)

int i, j, k;

int num1 = mid - start + 1;

int num2 = end - mid;

int arr1[num1], arr2[num2];

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

arr1[i] = arr[start + i];

for (j = 0; j < num2; j++)

arr2[j] = arr[mid + 1 + j];

i = 0;

j = 0;

k = start;

while (i < num1 && j < num2)

if (arr1[i] <= arr2[j])

arr[k] = arr1[i];

i++;

else

arr[k] = arr2[j];

j++;
}

k++;

while (i < num1)

arr[k] = arr1[i];

i++;

k++;

while (j < num2)

arr[k] = arr2[j];

j++;

k++;

int display(int arr[], int n)

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

printf("%d ", arr[i]);

void main()

int n, i, arr[30];

clrscr();

printf(“enter the size of Merge sort :\n”);

scanf("%d", &n);

printf(“enter the Merge sort elements:\n”);


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

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

divide(arr, 0, n - 1);

display(arr, n);

Output:

Enter the size of the Merge sort

10

Enter the Merge sort elements

66 33 11 88 99 77 55 22 00 44

00 11 22 33 44 55 66 77 88 99

12) Quick Sort

#include<stdio.h>

#include<conio.h>

int partition(int arr[], int low, int high)

int temp;

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high - 1; j++)

if (arr[j] <= pivot)

i++;

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;
}

temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

return (i + 1);

void quicksort(int arr[], int low, int high)

if (low < high)

int pi = partition(arr, low, high);

quicksort(arr, low, pi - 1);

quicksort(arr, pi + 1, high);

int display(int arr[], int n)

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

printf("%d ", arr[i]);

void main()

int n, i, arr[30];

clrscr();

printf(“enter the size of Quick sort:\n”);

scanf("%d", &n);

printf(“enter the Quick sort elements :\n”);


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

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

quicksort(arr, 0, n - 1);

display(arr, n);

Output:

Enter the size of the Quick sort

10

Enter the Quick sort elements

66 33 11 88 99 77 55 22 00 44

00 11 22 33 44 55 66 77 88 99

13) Heap Sort

#include<stdio.h>

#include<conio.h>

int temp;

void swaplargest(int arr[], int n, int i)

int largest = i;

int left = 2*i + 1;

int right = 2*i + 2;

if (left < n && arr[left] > arr[largest])

largest = left;

if (right < n && arr[right] > arr[largest])

largest = right;

if (largest != i)

temp = arr[i];
arr[i] = arr[largest];

arr[largest] = temp;

swaplargest(arr, n, largest);

void heap(int arr[], int n)

for (int i = n / 2 - 1; i >= 0; i--)

swaplargest(arr, n, i);

for (int i = n - 1; i >= 0; i--)

temp = arr[0];

arr[0] = arr[i];

arr[i] = temp;

swaplargest(arr, i, 0);

int display(int arr[], int n)

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

printf("%d ", arr[i]);

void main()

int n, i, arr[30];

clrscr();

printf(“enter the size of Heap sort:\n”);

scanf("%d", &n);
printf(“enter the Heap sort elements:\n”);

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

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

heap(arr, n);

display(arr, n);

Output:

Enter the size of the Heap sort

10

Enter the Heap sort elements

66 33 11 88 99 77 55 22 00 44

00 11 22 33 44 55 66 77 88 99

14) Linear Search

#include <stdio.h>

#include <conio.h>

void main()
{
int array[100], search, i, n;

clrscr();

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


scanf("%d", &n);

printf("Enter the array elements \n");

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

{
scanf("%d", &array[i]);

printf("Enter a the search element\n");


scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (array[i] == search)

{
printf("%d is present at location %d\n", search, (i+1));
break;
}
}
if (i == n)
{

printf("%d isn't present in the array.\n", search);

getch();
}

Output:

Enter number of elements in to array

Enter the array elements

4 9 3 8 2 0 6

Enter the searching element

3 is present at location 3

15) Binary Search

#include <stdio.h>

#include <conio.h>

void main()

int i, low, high, mid, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter the array elements are:\n");

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


scanf("%d",&array[i]);

printf("Enter the searching value\n");

scanf("%d", &search);

low = 0;

high = n - 1;

mid = (low+high)/2;

while (low <= high)

if(array[mid] <search)

low = mid + 1;

else

if (array[mid] == key)

printf("%d found at location %d", search, mid+1);

break;

else

high = mid - 1;

mid = (low + high)/2;

if(low > high)

printf("%d Not found in the list", search);

getch();

}
Output:

Enter number of elements in to array

Enter the array elements

2 3 4 5 7 8 9

Enter the searching element

5 is found at location 4

16) To convert an Infix Expression to Postfix Expression

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<math.h>

#include <string.h>

char infix_string[20], postfix_string[20];

int top;

int stack[20];

int pop();

int precedence(char symbol);

int isEmpty();

void infix_to_postfix();

int check_space(char symbol);

void push(long int symbol);

void main()

int count, length;

char temp;

clrscr();
top = -1;

printf("\nINPUT THE INFIX EXPRESSION : ");

scanf("%s", infix_string);

infix_to_postfix();

printf("\nEQUIVALENT POSTFIX EXPRESSION : %s\n", postfix_string);

return 0;

void infix_to_postfix()

unsigned int count, temp = 0;

char next;

char symbol;

for(count = 0; count < strlen(infix_string); count++)

symbol = infix_string[count];

if(!check_space(symbol))

switch(symbol)

case '(':

push(symbol);

break;

case ')':

while( (next = pop()) != '(' )

postfix_string[temp++] = next;

break;

case '+':

case '-':
case '*':

case '/':

case '%':

case '^':

while(!isEmpty() && precedence(stack[top]) >= precedence(symbol))

postfix_string[temp++] = pop();

push(symbol);

break;

default:

postfix_string[temp++] = symbol;

while(!isEmpty())

postfix_string[temp++] = pop();

postfix_string[temp] = '\0';

int precedence(char symbol)

switch(symbol)

case '(':

return 0;

case '+':

case '-':

return 1;

case '*':

case '/':
case '%':

return 2;

case '^':

return 3;

default:

return 0;

int check_space(char symbol)

if(symbol == '\t' || symbol == ' ' )

return 1;

else

return 0;

void push(long int symbol)

if(top > 20)

printf("Stack Overflow\n");

exit(1);

top = top + 1;

stack[top] = symbol;

int isEmpty()
{

if(top == -1)

return 1;

else

return 0;

int pop()

if(isEmpty())

printf("Stack is Empty\n");

exit(1);

return(stack[top--]);

Output:

INPUT THE INFIX EXPRESSION: a/b-(c+d)-e

EQUIVALENT POSTFIX EXPRESSION: ab/cd+-e-


17) To convert an expression from Infix to Prefix

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<string.h>

#include <stdlib.h>

#define MAX 20

void push(int);

char pop();

void infix_to_prefix();

int precedence (char);

char stack[20],infix[20],prefix[20];

int top = -1;

void main()

clrscr();

printf("\nINPUT THE INFIX EXPRESSION : ");

scanf("%s",infix);

infix_to_prefix();

return 0;

void push(int pos)

if(top == MAX-1)

printf("\nSTACK OVERFLOW\n");

else {

top++;

stack[top] = infix[pos];
}

char pop()

char ch;

if(top < 0)

printf("\nSTACK UNDERFLOW\n");

exit(0);

else

ch = stack[top];

stack[top] = '\0';

top--;

return(ch);

return 0;

void infix_to_prefix()

int i = 0,j = 0;

strrev(infix);

while(infix[i] != '\0')

if(infix[i] >= 'a' && infix[i] <= 'z')

prefix[j] = infix[i];

j++;

i++;
}

else

if(infix[i] == ')' || infix[i] == '}' || infix[i] == ']')

push(i);

i++;

else

if(infix[i] == '(' || infix[i] == '{' || infix[i] == '[')

if(infix[i] == '(')

while( stack[top] != ')' )

prefix[j] = pop();

j++;

pop();

i++;

else

if(infix[i] == '[')

while(stack[top] != ']')

prefix[j] = pop();

j++;

pop();

i++;
}

else

if(infix[i] == '{')

while( stack[top] != '}' )

prefix[j] = pop();

j++;

pop();

i++;

else

if(top == -1)

push(i);

i++;

else

if( precedence(infix[i]) < precedence(stack[top]))

prefix[j] = pop();

j++;

while(precedence(stack[top]) > precedence(infix[i]))

prefix[j] = pop();

j++;

if(top < 0)
{

break;

push(i);

i++;

else

if(precedence(infix[i]) >= precedence(stack[top]))

push(i);

i++;

while(top != -1)

prefix[j] = pop();

j++;

strrev(prefix);

prefix[j] = '\0';

printf("EQUIVALENT PREFIX NOTATION : %s ",prefix);

int precedence(char alpha)

if(alpha == '+' || alpha =='-')

return(1);

}
if(alpha == '*' || alpha =='/')

return(2);

getch();

Output:

INPUT THE INFIX EXPRESSION: a/b-(c+d)-e

EQUIVALENT POSTFIX EXPRESSION: --/ab+cde

18) Single Linked List

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head;

void beginsert ();

void lastinsert ();

void begin_delete();

void last_delete();

void display();

void main ()

int choice =0;

clrscr();
while(choice != 6)

printf("\n\n*********Main Menu*********\n");

printf("\nChoose one option from the following list ...\n");

printf("\n===============================================\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n

4.Delete from last\n 5.Display\n6.Exit\n");

printf("\nEnter your choice\n");

scanf("\n%d",&choice);

switch(choice)

case 1:

beginsert();

break;

case 2:

lastinsert();

break;

case 3:

begin_delete();

break;

case 4:

last_delete();

break;

case 5:

display();

break;

case 6:

exit(0);

break;

default:
printf("Please enter valid choice..");

void beginsert()

struct node *ptr;

int item;

ptr = (struct node *) malloc(sizeof(struct node *));

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter value\n");

scanf("%d",&item);

ptr->data = item;

ptr->next = head;

head = ptr;

printf("\nNode inserted");

void lastinsert()

struct node *ptr,*temp;

int item;

ptr = (struct node*)malloc(sizeof(struct node));

if(ptr == NULL)

{
printf("\nOVERFLOW");

else

printf("\nEnter value?\n");

scanf("%d",&item);

ptr->data = item;

if(head == NULL)

ptr -> next = NULL;

head = ptr;

printf("\nNode inserted");

else

temp = head;

while (temp -> next != NULL)

temp = temp -> next;

temp->next = ptr;

ptr->next = NULL;

printf("\nNode inserted");

void begin_delete()

struct node *ptr;

if(head == NULL)
{

printf("\nList is empty\n");

else

ptr = head;

head = ptr->next;

free(ptr);

printf("\nNode deleted from the begining ...\n");

void last_delete()

struct node *ptr,*ptr1;

if(head == NULL)

printf("\nlist is empty");

else

if(head -> next == NULL)

head = NULL;

free(head);

printf("\nOnly node of the list deleted ...\n");

else

ptr = head;

while(ptr->next != NULL)

{
ptr1 = ptr;

ptr = ptr ->next;

ptr1->next = NULL;

free(ptr);

printf("\nDeleted Node from the last ...\n");

void display()

struct node *ptr;

ptr = head;

if(ptr == NULL)

printf("Nothing to print");

else

printf("\nprinting values . . . . .\n");

while (ptr!=NULL)

printf("\n%d",ptr->data);

ptr = ptr -> next;

}
Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice?

Enter value

Node inserted

*********Main Menu*********

Choose one option from the following list...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice

Enter value

Node inserted
*********Main Menu*********

Choose one option from the following list...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice

Enter element value

Enter the location after which you want to insert

Node inserted

*********Main Menu*********

Choose one option from the following list...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice

printing values . . . . .

2
1

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice

Enter value

123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice

Enter value

1234

Node inserted
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice?

Node deleted from the beginning...

*********Main Menu*********

Choose one option from the following list...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice

Deleted Node from the last...

*********Main Menu*********

Choose one option from the following list ...

==============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning


4.Delete from last

5.Show

6.Exit

Enter your choice

Enter the location of the node after which you want to perform deletion

Deleted node 2

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice

Printing values . . . . .

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show
6.Exit

Enter your choice

Enter item which you want to search?

item found at location 1

item found at location 2

*********Main Menu*********

Choose one option from the following list...

===============================================

1.Insert in beginning

2.Insert at last

3.Delete from Beginning

4.Delete from last

5.Show

6.Exit

Enter your choice?

9
19) Double Linked List

#include <stdio.h>

#include<conio.h>

#include <string.h>

#include <stdlib.h>

#include <stdbool.h>

struct node

int data;

int key;

struct node *next;

struct node *prev;

};

struct node *head = NULL;

struct node *last = NULL;

struct node *current = NULL;

bool isEmpty()

return head == NULL;

int length()

int length = 0;

struct node *current;

for(current = head; current != NULL; current = current->next)

length++;

return length;
}

void displayForward()

struct node *ptr = head;

printf("\n[ ");

while(ptr != NULL)

printf("(%d,%d) ",ptr->key,ptr->data);

ptr = ptr->next;

printf(" ]");

void displayBackward()

struct node *ptr = last;

printf("\n[ ");

while(ptr != NULL)

printf("(%d,%d) ",ptr->key,ptr->data);

ptr = ptr ->prev;

void insertFirst(int key, int data)

struct node *link = (struct node*) malloc(sizeof(struct node));

link->key = key;

link->data = data;

if(isEmpty())

last = link;
}

else

head->prev = link;

link->next = head;

head = link;

void insertLast(int key, int data)

struct node *link = (struct node*) malloc(sizeof(struct node));

link->key = key;

link->data = data;

if(isEmpty())

last = link;

else

last->next = link;

link->prev = last;

last = link;

struct node* deleteFirst()

struct node *tempLink = head;

if(head->next == NULL)

last = NULL;
}

else

head->next->prev = NULL;

head = head->next;

return tempLink;

struct node* deleteLast()

struct node *tempLink = last;

if(head->next == NULL)

head = NULL;

else

last->prev->next = NULL;

last = last->prev;

return tempLink;

struct node* delete(int key)

struct node* current = head;

struct node* previous = NULL;

if(head == NULL)

return NULL;

}
while(current->key != key)

if(current->next == NULL)

return NULL;

else

previous = current;

current = current->next;

if(current == head)

head = head->next;

else

current->prev->next = current->next;

if(current == last)

last = current->prev;

else

current->next->prev = current->prev;

return current;

}
bool insertAfter(int key, int newKey, int data)

Struct node *current = head;

if(head == NULL)

return false;

while(current->key != key)

if(current->next == NULL)

return false;

else

current = current->next;

struct node *newLink = (struct node*) malloc(sizeof(struct node));

newLink->key = newKey;

newLink->data = data;

if(current == last)

newLink->next = NULL;

last = newLink;

else

newLink->next = current->next;

current->next->prev = newLink;
}

newLink->prev = current;

current->next = newLink;

return true;

void main()

clrscr();

insertFirst(1,10);

insertFirst(2,20);

insertFirst(3,30);

insertFirst(4,40);

insertFirst(5,50);

insertFirst(6,60);

printf("\nList (First to Last): \n\n");

displayForward();

printf("\n");

printf("\nList (Last to first):\n\n ");

displayBackward();

printf("\n After deleting first node: ");

deleteFirst();

displayForward();

printf("\n After deleting last node: ");

deleteLast();

displayForward();

printf("\n Insert after key(4) : ");

insertAfter(4,7, 13);

displayForward();

printf("\n After delete key(4) : ");

delete(4);
displayForward();

getch();

Output:

List (First to Last):


[ (6,60) (5,50) (4,40) (3,30) (2,20) (1,10) ]

List (Last to first):


[ (1,10) (2,20) (3,30) (4,40) (5,50) (6,60) ]

After deleting first node:


[ (5,50) (4,40) (3,30) (2,20) (1,10) ]

After deleting last node:


[ (5,50) (4,40) (3,30) (2,20) ]

Insert after key(4) :


[ (5,50) (4,40) (7,70) (3,30) (2,20) ]

After delete key(4) :


[ (5,50) (4,40) (3,30) (2,20) ]

20) Binary Tree Traversals

#include <stdio.h>

#include<conio.h>

#include <stdlib.h>

struct node

int data;

struct node* left;

struct node* right;

};

struct node* newNode(int data)


{

struct node* node = (struct node*)malloc(sizeof(struct node));

node->data = data;

node->left = NULL;

node->right = NULL;

return (node);

void Postorder(struct node* node)

if (node == NULL)

return;

Postorder(node->left);

Postorder(node->right);

printf("%d ", node->data);

void Inorder(struct node* node)

if (node == NULL)

return;

Inorder(node->left);

printf("%d ", node->data);

printInorder(node->right);

void Preorder(struct node* node)

if (node == NULL)

return;

printf("%d ", node->data);

Preorder(node->left);

Preorder(node->right);
}

void main()

clrscr();

struct node* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("\nPreorder traversal of binary tree is \n");

Preorder(root);

printf("\nInorder traversal of binary tree is \n");

Inorder(root);

printf("\nPostorder traversal of binary tree is \n");

Postorder(root);

getch();

Output:

Preorder traversal of binary tree is


1 2 4 5 3
Inorder traversal of binary tree is
4 2 5 1 3
Postorder traversal of binary tree is
4 5 2 3 1
21) Death First Search

#include<stdio.h>

#include<conio.h>

int a[20][20],reach[20],n;

void dfs(int v)

int i;

reach[v]=1;

for (i=1;i<=n;i++)

if(a[v][i] && !reach[i])

printf("\n %d->%d",v,i);

dfs(i);

void main()

int i,j,count=0;

clrscr();

printf("\n Enter number of vertices:");

scanf("%d",&n);

for (i=1;i<=n;i++)

reach[i]=0;

for (j=1;j<=n;j++)

a[i][j]=0;

printf("\n Enter the adjacency matrix:\n");

for (i=1;i<=n;i++)
for (j=1;j<=n;j++)

scanf("%d",&a[i][j]);

dfs(1);

printf("\n");

for (i=1;i<=n;i++)

if(reach[i])

count++;

if(count==n)

printf("\n Graph is connected");

else

printf("\n Graph is not connected");DFS

getch();

Output:
22) Breadth First Search

#include<stdio.h>

#include<conio.h>

int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;

void bfs(int v)

for (i=1;i<=n;i++)

if(a[v][i] && !visited[i])

q[++r]=i;

if(f<=r)

visited[q[f]]=1;

bfs(q[f++]);

void main()

int v;

clrscr();

printf("\n Enter the number of vertices:");

scanf("%d",&n);

for (i=1;i<=n;i++)

q[i]=0;

visited[i]=0;

printf("\n Enter graph data in matrix form:\n");

for (i=1;i<=n;i++)

for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);

printf("\n Enter the starting vertex:");

scanf("%d",&v);

bfs(v);

printf("\n The node which are reachable are:\n");

for (i=1;i<=n;i++)

if(visited[i])

printf("%d\t",i);

else

printf("\n Bfs is not possible");

getch();

Output:
DEQUE

#include <stdio.h>
#define size 5
int deque[size];
int front = -1, rear = -1;

void insert_front(int x)
{
if((front==0 && rear==size-1) || (front==rear+1))
{
printf("Overflow");
}
else if((front==-1) && (rear==-1))
{
front=r=0;
deque[front]=x;
}
else if(front==0)
{
front=size-1;
deque[front]=x;
}
else
{
front=front-1;
deque[front]=x;
}
}

void insert_rear(int x)
{
if((front==0 && rear==size-1) || (front==rear+1))
{
printf("Overflow");
}
else if((front==-1) && (rear==-1))
{
rear=0;
deque[rear]=x;
}
else if(rear==size-1)
{
rear=0;
deque[rear]=x;
}
else
{
rear++;
deque[rear]=x;
}

void display()
{
int i=front;
printf("\nElements in a deque are: ");

while(i!=rear)
{
printf("%d ",deque[i]);
i=(i+1)%size;
}
printf("%d",deque[rear]);
}

void getfront()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else
{
printf("\nThe value of the element at front is: %d", deque[front]);
}

}
void getrear()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else
{
printf("\nThe value of the element at rear is %d", deque[rear]);
}

void delete_front()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else if(front==rear)
{
printf("\nThe deleted element is %d", deque[front]);
front=-1;
rear=-1;

}
else if(front==(size-1))
{
printf("\nThe deleted element is %d", deque[front]);
f=0;
}
else
{
printf("\nThe deleted element is %d", deque[front]);
front=front+1;
}
}
void delete_rear()
{
if((front==-1) && (rear==-1))
{
printf("Deque is empty");
}
else if(front==rear)
{
printf("\nThe deleted element is %d", deque[rear]);
front=-1;
rear=-1;

}
else if(rear==0)
{
printf("\nThe deleted element is %d", deque[rear]);
rear=size-1;
}
else
{
printf("\nThe deleted element is %d", deque[rear]);
rear=rear-1;
}
}

void main()
{
insert_front(20);
insert_front(10);
insert_rear(30);
insert_rear(50);
insert_rear(80);
display();
getfront();
getrear();
delete_front();
delete_rear();
display();
getch();
}
How to insert a node in BST

#include <stdio.h>

#include <stdlib.h>

struct node {

int key;

struct node *left, *right;

};

struct node* newNode(int item)

struct node* temp

= (struct node*)malloc(sizeof(struct node));

temp->key = item;

temp->left = temp->right = NULL;

return temp;

void inorder(struct node* root)

if (root != NULL) {

inorder(root->left);

printf("%d ", root->key);

inorder(root->right);

struct node* insert(struct node* node, int key)

if (node == NULL)

return newNode(key);
if (key < node->key)

node->left = insert(node->left, key);

else if (key > node->key)

node->right = insert(node->right, key);

return node;

void main()

struct node* root = NULL;

root = insert(root, 50);

insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

insert(root, 60);

insert(root, 80);

inorder(root);

getch();

Output:
How to delete a Node in BST

#include <stdio.h>

#include <stdlib.h>

struct node {

int key;

struct node *left, *right;

};

struct node* newNode(int item)

struct node* temp

= (struct node*)malloc(sizeof(struct node));

temp->key = item;

temp->left = temp->right = NULL;

return temp;

void inorder(struct node* root)

if (root != NULL) {

inorder(root->left);

printf("%d ", root->key);

inorder(root->right);

}
struct node* insert(struct node* node, int key)

if (node == NULL)

return newNode(key);

if (key < node->key)

node->left = insert(node->left, key);

else

node->right = insert(node->right, key);

return node;

struct node* minValueNode(struct node* node)

struct node* current = node;

while (current && current->left != NULL)

current = current->left;

return current;

struct node* deleteNode(struct node* root, int key)

if (root == NULL)

return root;

if (key < root->key)

root->left = deleteNode(root->left, key);

else if (key > root->key)


root->right = deleteNode(root->right, key);

else {

if (root->left == NULL)

struct node* temp = root->right;

free(root);

return temp;

else if (root->right == NULL) {

struct node* temp = root->left;

free(root);

return temp;

struct node* temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);

return root;

void main()

struct node* root = NULL;

root = insert(root, 50);

root = insert(root, 30);

root = insert(root, 20);

root = insert(root, 40);

root = insert(root, 70);


root = insert(root, 60);

root = insert(root, 80);

printf("Inorder traversal of the given tree \n");

inorder(root);

printf("\nDelete 20\n");

root = deleteNode(root, 20);

printf("Inorder traversal of the modified tree \n");

inorder(root);

printf("\nDelete 30\n");

root = deleteNode(root, 30);

printf("Inorder traversal of the modified tree \n");

inorder(root);

printf("\nDelete 50\n");

root = deleteNode(root, 50);

printf("Inorder traversal of the modified tree \n");

inorder(root);

getch();

Output:

Binary Tree Traversals Techniques

#include <stdio.h>

#include<conio.h>

#include <stdlib.h>
struct node

int data;

struct node* left;

struct node* right;

};

struct node* newNode(int data)

struct node* node = (struct node*)malloc(sizeof(struct node));

node->data = data;

node->left = NULL;

node->right = NULL;

return (node);

void Postorder(struct node* node)

if (node == NULL)

return;

Postorder(node->left);

Postorder(node->right);

printf("%d ", node->data);

void Inorder(struct node* node)

if (node == NULL)

return;

Inorder(node->left);

printf("%d ", node->data);

printInorder(node->right);
}

void Preorder(struct node* node)

if (node == NULL)

return;

printf("%d ", node->data);

Preorder(node->left);

Preorder(node->right);

void main()

clrscr();

struct node* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("\nPreorder traversal of binary tree is \n");

Preorder(root);

printf("\nInorder traversal of binary tree is \n");

Inorder(root);

printf("\nPostorder traversal of binary tree is \n");

Postorder(root);

getch();

Output:

Preorder traversal of binary tree is


1 2 4 5 3
Inorder traversal of binary tree is
4 2 5 1 3
Postorder traversal of binary tree is
4 5 2 3 1

You might also like