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

Document

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 views

Document

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/ 52

1) Write a C program that uses functions to perform the following on Singly Linked List: i)

Creation ii) Insertion iii) Deletion iv) Traversal

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int count=0;

struct node

int data;

struct node *next;

}*head,*newn,*trav;

void create_list()

int value;

struct node *temp;

temp=head;

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

printf("\nenter the value to be inserted");

scanf("%d",&value);

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;
}

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

void insert_at_begning(int value)

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

newn->next=head;

head=newn;
count++;

void insert_at_end(int value)

struct node *temp;

temp=head;

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

}
int insert_at_middle()

if(count>=2)

struct node *var1,*temp;

int loc,value;

printf("\n after which value you want to insert : ");

scanf("%d",&loc);

printf("\nenter the value to be inserted");

scanf("%d",&value);

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

newn->data=value;

temp=head;

/* if(head==NULL)

head=newn;

head->next=NULL;

count++;

return 0;

else

{*/

while(temp->data!=loc)

temp=temp->next;
if(temp==NULL)

printf("\nSORRY...there is no %d element",loc);

return 0;

//var1=temp->next;

newn->next=temp->next;//var1;

temp->next=newn;

count++;

//}

else

printf("\nthe no of nodes must be >=2");

int delete_from_middle()

if(count==0)

printf("\n List is Empty!!!! you can't delete elements\n");

else if(count>2)

struct node *temp,*var;

int value;
temp=head;

printf("\nenter the data that you want to delete from the list shown above");

scanf("%d",&value);

while(temp->data!=value)

var=temp;

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",value);

return 0;

if(temp==head)

head=temp->next;

else{

var->next=temp->next;

temp->next=NULL;

count--;

if(temp==NULL)

printf("Element is not avilable in the list \n**enter only middle elements..**");

else

printf("\ndata deleted from list is %d",value);


free(temp);

else

printf("\nthere no middle elemts..only %d elemts is avilable\n",count);

int delete_from_front()

struct node *temp;

temp=head;

if(head==NULL)

printf("\nno elements for deletion in the list\n");

return 0;

else

printf("\ndeleted element is :%d",head->data);

if(temp->next==NULL)

head=NULL;

else{

head=temp->next;

temp->next=NULL;
}

count--;

free(temp);

}}

int delete_from_end()

struct node *temp,*var;

temp=head;

if(head==NULL)

printf("\nno elemts in the list");

return 0;

else{

if(temp->next==NULL )

head=NULL;//temp->next;

else{

while(temp->next != NULL)

var=temp;

temp=temp->next;

var->next=NULL;

}
printf("\ndata deleted from list is %d",temp->data);

free(temp);

count--;

return 0;

int display()

trav=head;

if(trav==NULL)

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

return 0;

else

printf("\n\nElements in the Single Linked List is %d:\n",count);

while(trav!=NULL)

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

trav=trav->next;

printf("\n");

int main()
{

int ch=0;

char ch1;

head=NULL;

while(1)

printf("\n1.create linked list");

printf("\n2.insertion at begning of linked list");

printf("\n3.insertion at the end of linked list");

printf("\n4.insertion at the middle where you want");

printf("\n5.deletion from the front of linked list");

printf("\n6.deletion from the end of linked list ");

printf("\n7.deletion of the middle data that you want");

printf("\n8.display the linked list");

printf("\n9.exit\n");

printf("\nenter the choice of operation to perform on linked list");

scanf("%d",&ch);

switch(ch)

case 1:

do{

create_list();

display();

printf("do you want to create list ,y / n");

getchar();
scanf("%c",&ch1);

}while(ch1=='y');

break;

case 2:

int value;

printf("\nenter the value to be inserted");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 3:

int value;

printf("\nenter value to be inserted");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 4:

insert_at_middle();

display();
break;

case 5:

delete_from_front();

display();

}break;

case 6:

delete_from_end();

display();

break;

case 7:

display();

delete_from_middle();

display();

break;

case 8:

display();

break;

case 9:
{

exit(1);

default:printf("\n****Please enter correct choice****\n");

getch();

4.1) Write a program that implement Stack (its operations) using Arrays
#include<stdio.h>

#include<conio.h>

#define max 5

int st[max],top=-1;

void push()

int x;

if(top==max-1)

printf("stack is full");

return;

printf("enter element");

scanf("%d",&x);

top++;
st[top]=x;

void pop()

int x;

if(top==-1)

printf("stack is empty");

return;

printf("enter element");

scanf("%d",&x);

top--;

printf("enter deleted element=%d",x);

void display()

int i;

if(top==-1)

printf("stack is empty");

return;}

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

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

}}
int main()

int ch;

while(1)

printf("enter \n1.push\n2.pop\n3.display\n4.exit");

scanf("%d",&ch);

switch(ch)

case 1:push();break;

case 2:pop();break;

case 3:display();break;

case 4:exit(1);break;

}}

return 1;

4.2) Write a program that implement Stack (its operations) using Pointers
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 50

int size;

// Defining the stack structure


struct stack {

int arr[MAX];

int top;

};

// Initializing the stack(i.e., top=-1)

void init_stk(struct stack *st) {

st->top = -1;

// Entering the elements into stack

void push(struct stack *st, int num) {

if (st->top == size - 1) {

printf("\nStack overflow(i.e., stack full).");

return;

st->top++;

st->arr[st->top] = num;

//Deleting an element from the stack.

int pop(struct stack *st) {

int num;

if (st->top == -1) {

printf("\nStack underflow(i.e., stack empty).");

return NULL;

num = st->arr[st->top];

st->top--;
return num;

void display(struct stack *st) {

int i;

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

printf("\n%d", st->arr[i]);

int main() {

int element, opt, val;

struct stack ptr;

init_stk(&ptr);

printf("\nEnter Stack Size :");

scanf("%d", &size);

while (1) {

printf("\n\nSTACK PRIMITIVE OPERATIONS");

printf("\n1.PUSH");

printf("\n2.POP");

printf("\n3.DISPLAY");

printf("\n4.QUIT");

printf("\n");

printf("\nEnter your option : ");

scanf("%d", &opt);

switch (opt) {

case 1:

printf("\nEnter the element into stack:");

scanf("%d", &val);
push(&ptr, val);

break;

case 2: element = pop(&ptr);

printf("\nThe element popped from stack is : %d", element);

break;

case 3:

printf("\nThe current stack elements are:");

display(&ptr);

break;

case 4:

exit(0);

default:

printf("\nEnter correct option!Try again.");

return (0);

5.1) Write a program that implement Queue (its operations) using Arrays
#include<stdio.h>

#include<conio.h>

#define SIZE 5

int queue[SIZE], front = -1, rear = -1;

void enQueue(int value){


if(rear == SIZE-1)

printf("\nQueue is Full!!! Insertion is not possible!!!");

else{

if(front == -1)

front = 0;

rear++;

queue[rear] = value;

printf("\nInsertion success!!!");

void deQueue(){

if(front == rear)

printf("\nQueue is Empty!!! Deletion is not possible!!!");

else{

printf("\nDeleted : %d", queue[front]);

front++;

if(front == rear)

front = rear = -1;

void display(){

if(rear == -1)

printf("\nQueue is Empty!!!");

else{

int i;
printf("\nQueue elements are:\n");

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

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

void main()

int value, choice;

while(1){

printf("\n\n***** MENU *****\n");

printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");

printf("\nEnter your choice: ");

scanf("%d",&choice);

switch(choice){

case 1: printf("Enter the value to be insert: ");

scanf("%d",&value);

enQueue(value);

break;

case 2: deQueue();

break;

case 3: display();

break;

case 4: exit(0);

default: printf("\nWrong selection!!! Try again!!!");

}
}

5.2) Write a program that implement Queue (its operations) using Pointers
#include<stdlib.h>

struct q

int no;

struct q *next;

*start=NULL;

void add();

int del();

void display();

void main()

int ch;

char choice;

while(1)

printf(" \n MENU \n");

printf("\n1.Insert an element in Queue\n");

printf("\n2.Delete an element from Queue\n");

printf("\n3.Display the Queue\n");

printf("\n4.Exit!\n");
printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:add();

break;

case 2:

printf("\nThe deleted element is=%d",del());

break;

case 3:display();

getch();

break;

case 4:exit(0);

break;

default:printf("\nYou entered wrong choice");

getch();

break;

getch();

void add()

struct q *p,*temp;

temp=start;

p=(struct q*)malloc(sizeof(struct q));


printf("\nEnter the element:");

scanf("%d",&p->no);

p->next=NULL;

if(start==NULL)

start=p;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=p;

int del()

struct q *temp;

int value;

if(start==NULL)

printf("\nQueue is Empty");

getch();

return(0);

}
else

temp=start;

value=temp->no;

start=start->next;

free(temp);

return(value);

void display()

struct q *temp;

temp=start;

if(temp==NULL)

printf("queue is empty");

else

while(temp->next!=NULL)

printf("\nno=%d",temp->no);

temp=temp->next;

printf("\nno=%d",temp->no);

getch();

}
6.1 Quick sort
#include<stdio.h>

void quicksort(int number[25],int first,intlast){

int i, j, pivot, temp;

if(first<last){

pivot=first;

i=first;

j=last;

while(i<j){

while(number[i]<=number[pivot]&&i<last)

i++;

while(number[j]>number[pivot])

j--;

if(i<j){

temp=number[i];

number[i]=number[j];

number[j]=temp;

temp=number[pivot];

number[pivot]=number[j];

number[j]=temp;

quicksort(number,first,j-1);

quicksort(number,j+1,last);

}
}

int main(){

int i, count, number[25];

printf("How many elements are u going to enter?: ");

scanf("%d",&count);

printf("Enter %d elements: ", count);

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

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

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");

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

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

return0;

6.2 Heap sort program


#include <stdio.h>

int array[100], n;

main()

int choice, num;

n =0;/*Represents number of nodes in the heap*/

while(1)
{

printf("1.Insert the element \n");

printf("2.Delete the element \n");

printf("3.Display all elements \n");

printf("4.Quit \n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

case1:

printf("Enter the element to be inserted to the list : ");

scanf("%d",&num);

insert(num, n);

n = n +1;

break;

case2:

printf("Enter the elements to be deleted from the list: ");

scanf("%d",&num);

delete(num);

break;

case3:

display();

break;

case4:

exit(0);

default:
printf("Invalid choice \n");

}/*End of switch */

}/*End of while */

}/*End of main()*/

display()

int i;

if(n ==0)

printf("Heap is empty \n");

return;

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

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

printf("\n");

}/*End of display()*/

insert(int num,int location)

int parentnode;

while(location >0)

parentnode =(location -1)/2;

if(num <= array[parentnode])

{
array[location]= num;

return;

array[location]= array[parentnode];

location = parentnode;

}/*End of while*/

array[0]= num;/*assign number to the root node */

}/*End of insert()*/

delete(int num)

int left, right, i, temp, parentnode;

for(i =0; i < num; i++){

if(num == array[i])

break;

if(num != array[i])

printf("%d not found in heap list\n", num);

return;

array[i]= array[n -1];

n = n -1;

parentnode =(i -1)/2;/*find parentnode of node i */

if(array[i]> array[parentnode])
{

insert(array[i], i);

return;

left =2* i +1;/*left child of i*/

right =2* i +2;/* right child of i*/

while(right < n)

if(array[i]>= array[left]&& array[i]>= array[right])

return;

if(array[right]<= array[left])

temp = array[i];

array[i]= array[left];

array[left]= temp;

i = left;

else

temp = array[i];

array[i]= array[right];

array[right]= temp;

i = right;

left =2* i +1;

right =2* i +2;


}/*End of while*/

if(left == n -1&& array[i]){

temp = array[i];

array[i]= array[left];

array[left]= temp;

6.3 Merge sort


#include <stdio.h>

#define max 10

int a[11] = { 20, 24, 33, 26, 37, 31, 33, 65, 42, 34, 02 };

int b[10];

void merging(int low, int mid, int high) {

int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {

if(a[l1] <= a[l2])

b[i] = a[l1++];

else

b[i] = a[l2++];

}
while(l1 <= mid)

b[i++] = a[l1++];

while(l2 <= high)

b[i++] = a[l2++];

for(i = low; i <= high; i++)

a[i] = b[i];

void sort(int low, int high) {

int mid;

if(low < high) {

mid = (low + high) / 2;

sort(low, mid);

sort(mid+1, high);

merging(low, mid, high);

} else {

return;

int main() {

int i;
printf("List before sorting\n");

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

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

sort(0, max);

printf("\nList after sorting\n");

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

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

7.TREE TRAVARSAL METHODS


#include<stdio.h>

#include<stdlib.h>

typedef struct BST

int data;

struct BST *left;

struct BST *right;

}node;
node *create();

void insert(node *,node *);

void preorder(node *);

void inorder(node *);

void postorder(node *);

int main()

char ch;

node *root=NULL,*temp;

do

temp=create();

if(root==NULL)

root=temp;

else

insert(root,temp);

printf("nDo you want to enter more(y/n)?");

getchar();

scanf("%c",&ch);

}while(ch=='y'|ch=='Y');

printf("\nPreorder Traversal: ");

preorder(root);
printf("\nInorder Traversal: ");

inorder(root);

printf("\nPostorder Traversal: ");

postorder(root);

return 0;

node *create()

node *temp;

printf("nEnter data:");

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

scanf("%d",&temp->data);

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

return temp;

void insert(node *root,node *temp)

if(temp->data<root->data)

if(root->left!=NULL)

insert(root->left,temp);

else
root->left=temp;

if(temp->data>root->data)

if(root->right!=NULL)

insert(root->right,temp);

else

root->right=temp;

void preorder(node *root)

if(root!=NULL)

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

preorder(root->left);

preorder(root->right);

void inorder(node *root)

if(root!=NULL)

{
inorder(root->left);

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

inorder(root->right);

void postorder(node *root)

if(root!=NULL)

postorder(root->left);

postorder(root->right);

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

8.1 Binary search tree

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;
struct node *right_child;

struct node *left_child;

};

struct node* new_node(int x){

struct node *temp;

temp = malloc(sizeof(struct node));

temp->data = x;

temp->left_child = NULL;

temp->right_child = NULL;

return temp;

struct node* search(struct node * root, int x){

if (root == NULL || root->data == x)

return root;

else if (x > root->data)

return search(root->right_child, x);

else

return search(root->left_child, x);

struct node* insert(struct node * root, int x){

if (root == NULL)

return new_node(x);
else if (x > root->data)

root->right_child = insert(root->right_child, x);

else

root -> left_child = insert(root->left_child, x);

return root;

struct node* find_minimum(struct node * root) {

if (root == NULL)

return NULL;

else if (root->left_child != NULL)

return find_minimum(root->left_child);

return root;

struct node* delete(struct node * root, int x) {

if (root == NULL)

return NULL;

if (x > root->data)

root->right_child = delete(root->right_child, x);

else if (x < root->data)

root->left_child = delete(root->left_child, x);

else {

if (root->left_child == NULL && root->right_child == NULL){

free(root);

return NULL;
}

else if (root->left_child == NULL || root->right_child == NULL){

struct node *temp;

if (root->left_child == NULL)

temp = root->right_child;

else

temp = root->left_child;

free(root);

return temp;

else {

struct node *temp = find_minimum(root->right_child);

root->data = temp->data;

root->right_child = delete(root->right_child, temp->data);

return root;

void inorder(struct node *root){

if (root != NULL)

inorder(root->left_child);

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

inorder(root->right_child);

}
}

int main() {

struct node *root;

root = new_node(20);

insert(root, 5);

insert(root, 1);

insert(root, 15);

insert(root, 9);

insert(root, 7);

insert(root, 12);

insert(root, 30);

insert(root, 25);

insert(root, 40);

insert(root, 45);

insert(root, 42);

inorder(root);

printf("\n");

root = delete(root, 1);

root = delete(root, 40);

root = delete(root, 45);


root = delete(root, 9);

inorder(root);

printf("\n");

return 0;

9. Graph travarsal method

#include <stdio.h>

#include <stdlib.h>

// A structure to represent a node in the adjacency list

struct node {

int vertex;

struct node* next;

};

// A structure to represent the adjacency list

struct adj_list {

struct node* head;

};

// A structure to represent the graph

struct graph {
int num_vertices;

struct adj_list* adj_lists;

int* visited;

};

// Create a new node in the adjacency list

struct node* new_node(int vertex) {

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

new_node->vertex = vertex;

new_node->next = NULL;

return new_node;

// Create a graph with n vertices

struct graph* create_graph(int n) {

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

graph->num_vertices = n;

graph->adj_lists = (struct adj_list*)malloc(n * sizeof(struct adj_list));

graph->visited = (int*)malloc(n * sizeof(int));

int i;

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

graph->adj_lists[i].head = NULL;

graph->visited[i] = 0;

}
return graph;

// Add an edge to the graph

void add_edge(struct graph* graph, int src, int dest) {

// Add an edge from src to dest

struct node* new_node1 = new_node(dest);

new_node1->next = graph->adj_lists[src].head;

graph->adj_lists[src].head = new_node1;

// Add an edge from dest to src

struct node* new_node2 = new_node(src);

new_node2->next = graph->adj_lists[dest].head;

graph->adj_lists[dest].head = new_node2;

// BFS traversal of the graph starting from vertex v

void bfs(struct graph* graph, int v) {

// Create a queue for BFS

int queue[1000];

int front = -1;

int rear = -1;

// Mark the current node as visited and enqueue it

graph->visited[v] = 1;

queue[++rear] = v;
// Loop to visit all the vertices in the graph

while (front != rear) {

// Dequeue a vertex from the queue and print it

int current_vertex = queue[++front];

printf("%d ", current_vertex);

// Get all the neighbors of the dequeued vertex

struct node* temp = graph->adj_lists[current_vertex].head;

while (temp != NULL) {

int adj_vertex = temp->vertex;

// If the neighbor has not been visited, mark it as visited and enqueue it

if (graph->visited[adj_vertex] == 0) {

graph->visited[adj_vertex] = 1;

queue[++rear] = adj_vertex;

temp = temp->next;

int main() {

// Create a graph with 6 vertices

struct graph* graph = create_graph(6);


// Add edges to the graph

add_edge(graph, 0, 1);

add_edge(graph, 0, 2);

add_edge(graph, 1, 3);

add_edge(graph, 1, 4);

add_edge(graph, 2, 4);

add_edge(graph, 3, 4);

add_edge(graph, 3, 5);

add_edge(graph, 4,5);

// Perform BFS traversal starting from vertex 0

printf("BFS traversal starting from vertex 0: ");

bfs(graph, 0);

return 0;

10.1 pma USING bOYER-MOORE

# include <limits.h>

# include <string.h>

# include <stdio.h>

# define NO_OF_CHARS 256

// A utility function to get maximum of two integers


Int max(int a, int b) {

Return (a > b) ? a : b;

// The preprocessing function for Boyer Moore’s bad character heuristic

Void badCharHeuristic(char *str, int size, int badchar[NO_OF_CHARS]) {

Int I;

// Initialize all occurrences as -1

For (I = 0; I < NO_OF_CHARS; i++)

Badchar[i] = -1;

// Fill the actual value of last occurrence of a character

For (I = 0; I < size; i++)

Badchar[(int) str[i]] = I;

Void search(char *txt, char *pat) {

Int m = strlen(pat);

Int n = strlen(txt);

Int badchar[NO_OF_CHARS];

badCharHeuristic(pat, m, badchar);

int s = 0; // s is shift of the pattern with respect to text


while (s <= (n – m)) {

int j = m – 1;

while (j >= 0 && pat[j] == txt[s + j])

j--;

if (j < 0) {

printf(“\n pattern occurs at shift = %d”, s);

s += (s + m < n) ? m – badchar[txt[s + m]] : 1;

Else

S += max(1, j – badchar[txt[s + j]]);

Int main() {

Char txt[] = “ABAAABCD”;

Char pat[] = “AAA”;

Search(txt, pat);

Return 0;

}
10.2 PMA USING KNUTH-MORRIS-PRATT

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

Void computeLPSArray(char *pat, int M, int *lps);

Void KMPSearch(char *pat, char *txt) {

Int M = strlen(pat);

Int N = strlen(txt);

Int *lps = (int *) malloc(sizeof(int) * M);

Int j = 0;

computeLPSArray(pat, M, lps);

int I = 0;

while (I < N) {

if (pat[j] == txt[i]) {

j++;

i++;

}
If (j == M) {

Printf(“Found pattern at index %d \n”, I – j);

J = lps[j – 1];

Else if (I < N && pat[j] != txt[i]) {

If (j != 0)

J = lps[j – 1];

Else

I = I + 1;

Free(lps);

Void computeLPSArray(char *pat, int M, int *lps) {

Int len = 0;

Int I;

Lps[0] = 0;

I = 1;

While (I < M) {
If (pat[i] == pat[len]) {

Len++;

Lps[i] = len;

I++;

} else

If (len != 0) {

Len = lps[len – 1];

} else

Lps[i] = 0;

I++;

Int main() {

Char *txt = “ABABDABACDABABCABAB”;

Char *pat = “ABAB”;

KMPSearch(pat, txt);

Return 0;
}

You might also like