0% found this document useful (0 votes)
22 views30 pages

DSA C Programs

The document contains code implementations for various data structures using arrays in C including: 1) A stack implementation using an array with push, pop, peek and other functions. 2) An infix to postfix conversion algorithm using a stack. 3) A linear queue implementation using an array with enqueue, dequeue and other functions. 4) Two implementations of a circular queue - one using a single array and tracking front and rear indices, and another using additional functions. The code snippets demonstrate how basic data structures can be created using arrays in C with functions for insertion, removal and traversal operations. Various examples of usage and outputs are also shown testing the implementations.
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)
22 views30 pages

DSA C Programs

The document contains code implementations for various data structures using arrays in C including: 1) A stack implementation using an array with push, pop, peek and other functions. 2) An infix to postfix conversion algorithm using a stack. 3) A linear queue implementation using an array with enqueue, dequeue and other functions. 4) Two implementations of a circular queue - one using a single array and tracking front and rear indices, and another using additional functions. The code snippets demonstrate how basic data structures can be created using arrays in C with functions for insertion, removal and traversal operations. Various examples of usage and outputs are also shown testing the implementations.
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/ 30

Neepa Shah

STACK using Array


#define MAX 4
#include<stdio.h>
#include<conio.h>

int stack[MAX];
int top;

int isEmpty()
{
if (top==-1)
return 1;
else
return 0;
}

int size()
{
return top+1;
}

void push(int data)


{
if(size() == MAX)
{
printf("Stack full");
return;
}
stack[++top]=data;
}

int pop()
{
int t;
if(isEmpty())
{
printf("Stack empty");
return -1;
}
t=stack[top];
top=top-1;
return t;
}

int peek()
{
int t;
if(isEmpty())
{
printf("Stack empty");
return -1;
}
t=stack[top];
// top=top-1;
return t;

Page 1 of 30
Neepa Shah

void show()
{
int i;
printf("\nThe Stack elements are:");
for(i=top;i>=0;i--)
{
printf("%d\t",stack[i]);
}
}

int main()
{
char ch , a='y';
int choice, data;
top=-1;
printf("1.Insert");
printf("\n2.Delete");
printf("\n3.show or display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the token to be inserted:");
scanf("%d",&data);
push(data);
show();
break;
case 2:
data=pop();
printf("\nThe token deleted is %d",data);
show();
break;
case 3:
data = peek();
printf("\nThe token at top is %d",data);
show();
break;
default:
printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n):");
scanf(" %c", &ch);
}while(ch=='y'||ch=='Y');
}

Infix to postfix conversion and evaluation


#define SIZE 50
#include <ctype.h>
#include <stdio.h>

Page 2 of 30
Neepa Shah

char s[SIZE];
int top = -1;

void push(char elem)


{
s[++top] = elem;
}

char pop()
{
return (s[top--]);
}

int pr(char elem)


{
switch (elem)
{
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}

void main()
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s", infx);
push('#');

while ((ch = infx[i++]) != '\0')


{
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
pop(); /* Remove ( */
}

else
{ /* Operator */
while (pr(s[top]) >= pr(ch))

Page 3 of 30
Neepa Shah

pofx[k++] = pop();

push(ch);
}
}

while (s[top] != '#') /* Pop from stack till empty */


pofx[k++] = pop();
pofx[k] = '\0';

printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);


}
// a / b - c + d * e - a * c

/* Read the Infix Expression ? (a+b-c)*d-(e+f)

Given Infix Expn: (a+b-c)*d-(e+f) Postfix Expn: ab+c-d*ef+-

Read the Infix Expression ? a/b-c+d*e-a*c

Given Infix Expn: a/b-c+d*e-a*c Postfix Expn: ab/c-de*+ac*- */

LINEAR QUEUE
#define MAX 4
#include<stdio.h>
#include<conio.h>

int queue[MAX];
int front, rear;

void enqueue(int data)


{
if (rear == MAX - 1) // size()==MAX || front == 0 and rear == MAX-1
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
rear = rear + 1;
queue[rear] = data;
}
}

int dequeue()
{
int t;
if (front == - 1) // if (isEmpty())
{
printf("Queue Underflow \n");
return -999;
}
else
{
if (front == rear)
{

Page 4 of 30
Neepa Shah

t = queue[front];
front = rear = -1;
}
else
{
t = queue[front];
front = front + 1;
}
return t;
}
}

int front1()
{
int t;
if (front == - 1) // if (isEmpty())
{
printf("Queue Underflow \n");
return -999;
}
else
{
t = queue[front];
return t;
}
}

void show()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("\nThe Queue elements are:");
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}

int main()
{
char ch , a='y';
int choice, data;
front = rear = -1;
printf("\n1.Insert");
printf("\n2.Delete");

printf("\n3.show or display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)

Page 5 of 30
Neepa Shah

{
case 1:
printf("\nEnter the token to be inserted:");
scanf("%d",&data);
enqueue(data);
show();
printf("Element at front %d", front1());
show();
break;
case 2:
data=dequeue();
if(data != -999)
printf("\nThe token deleted is %d",data);
show();
break;
case 3:
show();
break;
default:
printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n):");
scanf(" %c", &ch);
}while(ch=='y'||ch=='Y');
}

CIRCULAR QUEUE
WAY 1:
#include <stdio.h>
#include<stdlib.h>
#define SIZE 5

int a[SIZE];
int rear,front;
int temp;

int isEmpty()
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}

void enqueue(int v)
{
if((front == 0 && rear == SIZE-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (isEmpty()) /*If queue is empty */
{
front = 0;

Page 6 of 30
Neepa Shah

rear = 0;
}
else
{
if(rear == SIZE-1) /*rear is at last position of queue */
rear = 0;
else
rear = rear + 1;
}
a[rear] = v ;
printf("\n\nRear = %d Front = %d ",rear,front);
}

int dequeue()
{
int temp;
if(isEmpty())
printf("queue is empty");

temp = a[front];
if(front == rear) /* queue has only one element */
{
front = -1;
rear = -1;
}
else
{
if(front == SIZE-1)
front = 0;
else
front = front + 1;
}
printf("\n\nRear = %d Front = %d ",rear,front);
return temp;
}

void display()
{
int i;
if(isEmpty())
printf("Circular Queue is Empty");
else
{
if(rear < front)
{
for(i = front;i <= SIZE-1;i++)
printf("%d ", a[i]);
for(i = 0;i <= rear;i++)
printf("%d ", a[i]);
}
else
{
for(i = front;i <= rear;i++)
printf("%d ", a[i]);
printf("\n");

Page 7 of 30
Neepa Shah

}
}
}

void main()
{
int t, ch, num;
rear = -1, front = -1;

printf("\nProgram for Circular Queue demonstration through array");

while(1)
{
printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
enqueue(num);
display();
break;
case 2:
t = dequeue();
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",t);
display();
break;
case 3:
exit(0);
default: printf("\n\nInvalid Choice . ");
}
}
}

WAY 2
#include <stdio.h>
#include<stdlib.h>
#define SIZE 5

int a[SIZE];
int rear,front;
int temp;

int isEmpty()
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}

int size()
{

Page 8 of 30
Neepa Shah

return (SIZE - front + rear) % SIZE + 1;


}

void enqueue(int v)
{
if(SIZE == size())
{
printf("queue is full");
return;
}
else
{
if(isEmpty())
{
front=0;
rear=0;
}
else
rear=(rear+1) % SIZE;
}
a[rear]=v ;
printf("\n\nRear = %d Front = %d ",rear,front);
}

int dequeue()
{
int temp;
if(isEmpty())
printf("queue is empty");
else
{
temp=a[front];
if(front==rear)
front = rear = -1;
else
front=(front+1) % SIZE;
}
printf("\n\nRear = %d Front = %d ",rear,front);
return temp;
}

void display()
{
int i;
if(isEmpty())
printf("Circular Queue is Empty");
else
{
if(rear < front)
{
for(i = front;i <= SIZE-1;i++)
printf("%d ", a[i]);
for(i = 0;i <= rear;i++)
printf("%d ", a[i]);
}

Page 9 of 30
Neepa Shah

else
{
for(i = front;i <= rear;i++)
printf("%d ", a[i]);
printf("\n");
}
}
}

void main()
{
int t, ch, num;
rear = -1, front = -1;

printf("\nProgram for Circular Queue demonstration through array");

while(1)
{
printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
enqueue(num);
display();
break;
case 2:
t = dequeue();
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",t);
display();
break;
case 3:
exit(0);
default: printf("\n\nInvalid Choice . ");
}
}
}

SLL
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}*head,*var,*trav;

void insert_at_begin(int value)


{
var = (struct node *)malloc(sizeof (struct node));
var->data = value;

Page 10 of 30
Neepa Shah

if(head == NULL)
{
head = var;
var->next = NULL;
}
else
{
var->next = head;
head = var;
}
}

void insert_at_end(int value)


{
trav = head;
var = (struct node *)malloc(sizeof (struct node));
var->data = value;
if(head == NULL)
{
head = var;
head->next = NULL;
}
else
{
while(trav->next != NULL)
trav = trav->next;
var->next = NULL;
trav->next = var;
}
}

void insert_at_middle(int value, int d)


{
var=(struct node *)malloc(sizeof (struct node));
var->data = value;
trav = head;

if(head==NULL)
{
head = var;
head->next = NULL;
}
else
{
while(trav->data != d)
trav = trav->next;
var->next = trav->next;
trav->next = var;
}
}

int delete_from_middle(int value)


{
struct node *temp,*var;
temp=head;

Page 11 of 30
Neepa Shah

while(temp != NULL)
{
if(temp->data == value)
{
if(temp == head)
{
head = temp->next;
free(temp);
return 0;
}
else
{
var->next = temp->next;
free(temp);
return 0;
}
}
else
{
var = temp;
temp = temp->next;
}
}
printf("data deleted from list is %d",value);
}

int delete_from_end()
{
struct node *temp;
temp = head;
while(temp->next != NULL)
{
var = temp;
temp = temp->next;
}
if(temp == head)
{
head = temp->next;
free(temp);
return 0;
}
printf("data deleted from list is %d",temp->data);
var->next=NULL;
free(temp);
return 0;
}

void display()
{
trav = head;
if(trav == NULL)
printf("\nList is Empty");
else
{
printf("\nElements in the List: ");

Page 12 of 30
Neepa Shah

while(trav!=NULL)
{
printf(" -> %d ",trav->data);
trav=trav->next;
}
printf("\n");
}
}

int main()
{
int ch = 0, value, data;
head = NULL;
printf("1:insertion at begning of linked list");
printf("\n2:insertion at the end of linked list");
printf("\n3:insertion at the middle where you want");
printf("\n4:deletion from the end of linked list");
printf("\n5:deletion of the data that you want");
printf("\n6:exit\n");
while(1)
{
printf("\nenter the choice of operation to perform on linked list");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_begin(value);
display();
break;
case 2:
printf("\nenter value to be inserted");
scanf("%d",&value);
insert_at_end(value);
display();
break;
case 3:
printf("\nafter which data you want to insert the data");
scanf("%d",&data);
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_middle(value, data);
display();
break;
case 4:
delete_from_end();
display();
break;
case 5:
display();
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
delete_from_middle(value);
display();

Page 13 of 30
Neepa Shah

break;
case 6:
exit(0);
}
}
}

Stack using LL
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int Data;
struct Node *next;
}*top;

void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}

void push(int value)


{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}

void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);

Page 14 of 30
Neepa Shah

var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}

int main()
{
int i=0, value;
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("\nEnter a value to push into Stack: ");
scanf("%d",&value);
push(value);
display();
break;
case 2:
popStack();
display();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}

Queue using LL

#include<stdio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *front,*rear;

void insert(void)
{
struct node *nn;

Page 15 of 30
Neepa Shah

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


printf("enter a data:\n");
scanf("%d",&nn->data);
nn->next=NULL;
if(rear==NULL)
{
front=rear=nn;
}
else
{
rear->next=nn;
rear=nn;
}
printf("%d is successfully inserted\n",nn->data);
}

void deletion(void)
{
struct node *temp;
int x;
if(front==NULL)
{
printf("queue is empty\n");
return;
}
temp=front;
x=temp->data;
if(front==rear)
{
front=rear=NULL;
}
else
{
front=front->next;
}
free(temp);
printf("%d is succ. deleted\n",x);
return;
}

void display(void)
{
struct node *temp;
if(front==NULL)
{
printf("queue is empty\n");
return;
}
temp=front;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
return;

Page 16 of 30
Neepa Shah

void main()
{
int c;
front=rear=NULL;
do
{
printf("1:insert\n2:delete\n3:display\n4:exit\nenter choice:");
scanf("%d",&c);
switch(c)
{
case 1:insert();break;
case 2:deletion();break;
case 3:display();break;
case 4:printf("program ends\n");break;
default:printf("wrong choice\n");
}
}while(c!=4);
}

Develop Code to Implement different operations on linked list –copy, concatenate, split, reverse,
count no. of nodes etc.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}*head,*var,*trav;

void insert_at_begning(int value)


{
var=(struct node *)malloc(sizeof (struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
var->next=head;
head=var;
}
}

struct node *reverse(struct node *t1)


{
struct node *p = t1, *q = NULL, *r;
while (p != NULL)
{
r = q;

Page 17 of 30
Neepa Shah

q = p;
p = p->next;
q->next = r;
}
return q;
}

struct node *concat(struct node *p, struct node *q)


{
struct node *x,*r;
if(p==NULL)
r=q;
if(q==NULL)
r=p;
else
{
x=p;
r=x;
while(x->next!=NULL)
x=x->next;
x->next=q;
}
return(r);
}

struct node *copy(struct node *start1)


{
struct node *start2=NULL,*previous=NULL;
while(start1!=NULL)
{
struct node * temp = (struct node *) malloc (sizeof(struct node));
temp->data=start1->data;
temp->next=NULL;
if(start2==NULL)
{
start2=temp;
previous=temp;
}
else
{
previous->next=temp;
previous=temp;
}
start1=start1->next;
}
return start2;
}

int count(struct node *q)


{
int c=0;
if(q==NULL)
{
printf("Empty Link List.\n");
exit(0);

Page 18 of 30
Neepa Shah

}
while(q!=NULL)
{
c++;
q=q->next;
}
return c;
}

void display(struct node *t)


{
trav=t;
if(trav==NULL)
{
printf("\nList is Empty");
}
else
{
printf("\nElements in the List: ");
while(trav!=NULL)
{
printf(" %d ->",trav->data);
trav=trav->next;
}
printf("\n");
}
}

struct node* split(struct node *t)


{
int i=0;
struct node *l2=t;
// printf("\n%d %d", l2->data, t->data);
int c = count(t);
while(i!=(c/2)-1)
{
l2=l2->next;
t=t->next;
i++;
}
// printf("\n%d", i);
l2=l2->next;
t->next=NULL;
return l2;
}

int main()
{
int i=0;
struct node *rev, *dest, *cat, *splt;
head=NULL;
for(i=0; i<4; i++)
insert_at_begning(i);
printf("\nOriginal: 3 to 0");
display(head);

Page 19 of 30
Neepa Shah

rev = reverse(head);
printf("\nReverse: 0 to 3");
display(rev);

dest = copy(rev);
printf("\nCopy: 0 to 3");
display(dest);

cat = concat(rev, dest);


printf("\n Concate: 0 to 3 and 0 to 3");
display(cat);

i=count(cat);
printf("\nConcated list count: %d", i);

printf("\nSpliting list in two :");


splt=split(cat);
display(cat);
display(splt);

DLL
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

struct node {
int data;
struct node *next;
struct node *prev;
};

struct node *head = NULL;


struct node *tail = NULL;
struct node *current = NULL;
struct node *new_node = 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;

Page 20 of 30
Neepa Shah

while(ptr != NULL) {
printf("%d ",ptr->data);
ptr = ptr->next;
}
}

void displayBackward() {
struct node *ptr = tail;
while(ptr != NULL) {
printf("%d ",ptr->data);
ptr = ptr ->prev;
}
}

void insertFirst(int new_data) {


new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = new_data;
new_node->prev = NULL;

if (head != NULL)
head->prev = new_node;
else
tail = new_node;

new_node->next = head;
head = new_node;
}

void insertLast(int new_data) {


new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = NULL;

if (head == NULL) {
new_node->prev = NULL;
head = new_node;
}
else
{
tail->next = new_node;
new_node->prev = tail;
}
tail = new_node;
}

void insertBefore(int key, int new_data)


{
struct node *next_node = head;
if(head == NULL) {
printf("List empty");
}
else
{
while(next_node->data != key) {
if(next_node->next == NULL)

Page 21 of 30
Neepa Shah

insertFirst(key);
else
next_node = next_node->next;

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


new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
else
head = new_node;
}
}

void insertAfter(int key, int new_data) {


struct node *prev_node = head;
if(head == NULL) {
printf("List Empty");
}
else
{
while(prev_node->data != key) {
if(prev_node->next == NULL)
insertLast(key);
else
prev_node = prev_node->next;

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


new_node->data = new_data;

new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
}

void delete1(int key)


{
struct node *del = head;
if(head == NULL) {
printf("List Empty");
}
else if(head->next == NULL || tail->prev == NULL)
{
head = tail = NULL;
free (del);
printf("Deleted the last node");

Page 22 of 30
Neepa Shah

}
else
{
while(del->data != key)
del = del->next;
if (del != NULL)
{
if (head == del)
head = del->next;
if(tail == del)
tail = tail->prev;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->prev != NULL)
del->prev->next = del->next;
free(del);
}
else
printf("Node not found");
}
}

int main() {
insertFirst(10);
insertFirst(20);
insertFirst(30);
insertFirst(40);
insertFirst(50);

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


displayForward();

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

printf("\n\nInsert at the end : ");


printf("\nList (First to Last): ");
insertLast(100);
displayForward();

printf("\n\nInsert Before : ");


printf("\nList (First to Last): ");
insertBefore(40, -40);
displayForward();

printf("\n\nInsert Before BUT BEFORE FIRST NODE: ");


printf("\nList (First to Last): ");
insertBefore(50, -50);
displayForward();

printf("\n\nInsert After : ");


printf("\nList (First to Last): ");
insertAfter(50, 500);
displayForward();

Page 23 of 30
Neepa Shah

printf("\n\nInsert After BUT AFTER LAST NODE: ");


printf("\nList (First to Last): ");
insertAfter(100, 1000);
displayForward();

printf("\n\nList after deleting first record: ");


delete1(-50);
displayForward();

printf("\n\nList after deleting last record: ");


delete1(1000);
displayForward();

printf("\n\nList after delete key(40) : ");


delete1(40);
displayForward();

printf("\n\nList after certain deletes : ");


delete1(500);
delete1(100);
delete1(50);
delete1(10);
delete1(-40);
delete1(20);
displayForward();

delete1(30);
displayForward();

insertLast(100);
displayForward();
return 0;
}

Binary Search Tree


#include<stdio.h>
#include<stdlib.h>
typedef struct treeNode
{
int data;
struct treeNode *left;
struct treeNode *right;
}treeNode;

treeNode* Insert(treeNode *node,int data)


{
if(node==NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp -> data = data;
temp -> left = temp -> right = NULL;
return temp;
}

Page 24 of 30
Neepa Shah

if(data >(node->data))
node->right = Insert(node->right,data);
else if(data < (node->data))
node->left = Insert(node->left,data);
return node;
}

treeNode* Delete(treeNode *node, int data)


{
treeNode *temp;
if(node==NULL)
printf("Element Not Found");
else if(data < node->data)
node->left = Delete(node->left, data);

else if(data > node->data)


node->right = Delete(node->right, data);
else
{
/* Now We can delete this node and replace with either minimum element in the right
sub tree or maximum element in the left subtree */
if(node->right && node->left)
{
/* Here we will replace with minimum element in the right sub tree */
temp = FindMin(node->right);
node->data = temp->data;
/* As we replaced it with some other node, we have to delete that node */
node->right = Delete(node->right,temp->data);
}
else
{
/* If there is only one or zero children then we can directly remove it from
the tree and connect its parent to its child */
temp = node;
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp); /* temp is longer required */
}
}
return node;
}

void PrintInorder(treeNode *node)


{
if(node==NULL)
return;
PrintInorder(node->left);
printf("%d ",node->data);
PrintInorder(node->right);
}

void PrintPreorder(treeNode *node)


{

Page 25 of 30
Neepa Shah

if(node==NULL)
return;
printf("%d ",node->data);
PrintPreorder(node->left);
PrintPreorder(node->right);
}

void PrintPostorder(treeNode *node)


{
if(node==NULL)
return;
PrintPostorder(node->left);
PrintPostorder(node->right);
printf("%d ",node->data);
}

int main()
{
treeNode *root = NULL;
treeNode * temp;
root = Insert(root, 5);
root = Insert(root, -1);
root = Insert(root, 3);
root = Insert(root, -14);
root = Insert(root, 8);
root = Insert(root, 10);
root = Insert(root, 9);
root = Insert(root, 6);
PrintInorder(root);
printf("\n");

root = Delete(root,5);
printf("\n");
PrintInorder(root);

root = Delete(root,-1);
printf("\n");
PrintInorder(root);
printf("\n");

Merge Sort
#include<stdio.h>
#define MAX 50

void merge(int arr[],int low,int mid,int high);


void sort(int arr[],int low,int high);

void main()
{
int a[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);

Page 26 of 30
Neepa Shah

printf("Enter the elements which to be sort: ");


for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

sort(a,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

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


{
int mid;
if(low<high)
{
mid=(low+high)/2;
sort(arr,low,mid);
sort(arr,mid+1,high);
merge(arr,low,mid,high);
}
}

void merge(int arr[],int low,int mid,int high)


{
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high))
{
if(arr[l]<=arr[m])
{
temp[i]=arr[l];
l++;
}
else
{
temp[i]=arr[m];
m++;
}
i++;
}

if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=arr[k];
i++;
}
}

Page 27 of 30
Neepa Shah

else
{
for(k=l;k<=mid;k++)
{
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=temp[k];
}
}

Quick Sort
#include<stdio.h>
#define MAX 50

int partition(int a[], int p, int q);


void sort(int a[],int p, int q);

void main()
{
int i, n=8;
int a[] = {121,9,4,99,-120,1,3,10};
/* int a[MAX];
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
} */

printf("\nBefore sorting elements are: ");


for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
sort(a,0,n);
printf("\nAfter sorting elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

void sort(int a[],int p, int q){

if (q-p < 2)
{
return;
}

int m = partition(a, p, q) ;

Page 28 of 30
Neepa Shah

sort(a, p, m);
sort(a, m+1, q);
}

int partition(int a[], int p, int q)


{
int pivot = a[p], i = p, j = q;
while (i < j)
{
while (i < j && a[--j] >= pivot) ;
if(i < j)
a[i] = a[j];

while (i < j && a[++i] <= pivot) ;


if(i < j)
a[j] = a[i];
}
a[j] = pivot;
return j;
}

Linear Search
#include <stdio.h>

int search(int arr[], int n, int x)


{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int result = search(arr, 5, 5);
if (result == -1)
printf("Element is not present in array");
else
printf("Element is present at index %d", result);
return 0;
}

SEARCH ALL ELEMENTS


#include <stdio.h>
int search(int arr[], int n, int x)
{
int i, count = 0;
for (i = 0; i < n; i++)
{
if (arr[i] == x)
{
printf("%d is present at location %d.\n", x, i+1);

Page 29 of 30
Neepa Shah

count++;
}
}
return count;
}

int main(void)
{
int arr[] = { 2, 10, 4, 10, 40 };
int result = search(arr, 5, 10);
if (result == 0)
printf("Element is not present in array");
else
printf("Element is present %d times", result);

return 0;
}
Binary Search
#include <stdio.h>

int binarySearch(int a[], int x, int N)


{
int low = 0, high = N-1;
while (low <= high)
{
int mid = (low + high) / 2; // middle subscript

if (a[mid] < x)
low = mid + 1;
else if (x < a[mid])
high = mid - 1;
else
return mid; // found
}
return -1; // not found
}

int main(void)
{
int arr[] = { 2, 4, 5, 10, 40, 50 };
int result = binarySearch(arr, 5, 6);
if (result == -1)
printf("Element is not present in array");
else
printf("Element is present at %d", result);
}

Page 30 of 30

You might also like