Document
Document
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int count=0;
struct node
int data;
}*head,*newn,*trav;
void create_list()
int value;
temp=head;
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++;
newn->data=value;
if(head==NULL)
head=newn;
head->next=NULL;
count++;
else
newn->next=head;
head=newn;
count++;
temp=head;
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)
int loc,value;
scanf("%d",&loc);
scanf("%d",&value);
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
int delete_from_middle()
if(count==0)
else if(count>2)
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)
else
else
int delete_from_front()
temp=head;
if(head==NULL)
return 0;
else
if(temp->next==NULL)
head=NULL;
else{
head=temp->next;
temp->next=NULL;
}
count--;
free(temp);
}}
int delete_from_end()
temp=head;
if(head==NULL)
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
while(trav!=NULL)
trav=trav->next;
printf("\n");
int main()
{
int ch=0;
char ch1;
head=NULL;
while(1)
printf("\n9.exit\n");
scanf("%d",&ch);
switch(ch)
case 1:
do{
create_list();
display();
getchar();
scanf("%c",&ch1);
}while(ch1=='y');
break;
case 2:
int value;
scanf("%d",&value);
insert_at_begning(value);
display();
break;
case 3:
int value;
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);
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--;
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;
int arr[MAX];
int top;
};
st->top = -1;
if (st->top == size - 1) {
return;
st->top++;
st->arr[st->top] = num;
int num;
if (st->top == -1) {
return NULL;
num = st->arr[st->top];
st->top--;
return num;
int i;
printf("\n%d", st->arr[i]);
int main() {
init_stk(&ptr);
scanf("%d", &size);
while (1) {
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.QUIT");
printf("\n");
scanf("%d", &opt);
switch (opt) {
case 1:
scanf("%d", &val);
push(&ptr, val);
break;
break;
case 3:
display(&ptr);
break;
case 4:
exit(0);
default:
return (0);
5.1) Write a program that implement Queue (its operations) using Arrays
#include<stdio.h>
#include<conio.h>
#define SIZE 5
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
void deQueue(){
if(front == rear)
else{
front++;
if(front == rear)
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
printf("%d\t",queue[i]);
void main()
while(1){
scanf("%d",&choice);
switch(choice){
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
}
}
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("\n4.Exit!\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
case 1:add();
break;
case 2:
break;
case 3:display();
getch();
break;
case 4:exit(0);
break;
getch();
break;
getch();
void add()
struct q *p,*temp;
temp=start;
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>
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(){
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return0;
int array[100], n;
main()
while(1)
{
printf("4.Quit \n");
scanf("%d",&choice);
switch(choice)
case1:
scanf("%d",&num);
insert(num, n);
n = n +1;
break;
case2:
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)
return;
printf("\n");
}/*End of display()*/
int parentnode;
while(location >0)
{
array[location]= num;
return;
array[location]= array[parentnode];
location = parentnode;
}/*End of while*/
}/*End of insert()*/
delete(int num)
if(num == array[i])
break;
if(num != array[i])
return;
n = n -1;
if(array[i]> array[parentnode])
{
insert(array[i], i);
return;
while(right < n)
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;
temp = array[i];
array[i]= array[left];
array[left]= temp;
#define max 10
int a[11] = { 20, 24, 33, 26, 37, 31, 33, 65, 42, 34, 02 };
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
b[i++] = a[l2++];
a[i] = b[i];
int mid;
sort(low, mid);
sort(mid+1, high);
} else {
return;
int main() {
int i;
printf("List before sorting\n");
sort(0, max);
#include<stdlib.h>
int data;
}node;
node *create();
int main()
char ch;
node *root=NULL,*temp;
do
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);
getchar();
scanf("%c",&ch);
}while(ch=='y'|ch=='Y');
preorder(root);
printf("\nInorder Traversal: ");
inorder(root);
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;
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;
if(root!=NULL)
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
if(root!=NULL)
{
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
if(root!=NULL)
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *right_child;
};
temp->data = x;
temp->left_child = NULL;
temp->right_child = NULL;
return temp;
return root;
else
if (root == NULL)
return new_node(x);
else if (x > root->data)
else
return root;
if (root == NULL)
return NULL;
return find_minimum(root->left_child);
return root;
if (root == NULL)
return NULL;
if (x > root->data)
else {
free(root);
return NULL;
}
if (root->left_child == NULL)
temp = root->right_child;
else
temp = root->left_child;
free(root);
return temp;
else {
root->data = temp->data;
return root;
if (root != NULL)
inorder(root->left_child);
inorder(root->right_child);
}
}
int main() {
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");
inorder(root);
printf("\n");
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node {
int vertex;
};
struct adj_list {
};
struct graph {
int num_vertices;
int* visited;
};
new_node->vertex = vertex;
new_node->next = NULL;
return new_node;
graph->num_vertices = n;
int i;
graph->adj_lists[i].head = NULL;
graph->visited[i] = 0;
}
return graph;
new_node1->next = graph->adj_lists[src].head;
graph->adj_lists[src].head = new_node1;
new_node2->next = graph->adj_lists[dest].head;
graph->adj_lists[dest].head = new_node2;
int queue[1000];
graph->visited[v] = 1;
queue[++rear] = v;
// Loop to visit all the vertices in the graph
// 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() {
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);
bfs(graph, 0);
return 0;
# include <limits.h>
# include <string.h>
# include <stdio.h>
Return (a > b) ? a : b;
Int I;
Badchar[i] = -1;
Badchar[(int) str[i]] = I;
Int m = strlen(pat);
Int n = strlen(txt);
Int badchar[NO_OF_CHARS];
badCharHeuristic(pat, m, badchar);
int j = m – 1;
j--;
if (j < 0) {
Else
Int main() {
Search(txt, pat);
Return 0;
}
10.2 PMA USING KNUTH-MORRIS-PRATT
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
Int M = strlen(pat);
Int N = strlen(txt);
Int j = 0;
computeLPSArray(pat, M, lps);
int I = 0;
while (I < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
If (j == M) {
J = lps[j – 1];
If (j != 0)
J = lps[j – 1];
Else
I = I + 1;
Free(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) {
} else
Lps[i] = 0;
I++;
Int main() {
KMPSearch(pat, txt);
Return 0;
}