[go: up one dir, main page]

0% found this document useful (0 votes)
23 views60 pages

DS Progam

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views60 pages

DS Progam

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

1.

//Array implementation of stack


#include<stdio.h>
#include<conio.h>
#define MAX 10
int s[MAX];
int top=-1;
void push(int);
void pop();
void disp();
void main()
{
int i=1,choice,item;
clrscr();
while(i==1)
{
printf("\n1. Push \n2. Pop \n3. Display");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter item to push:");
scanf("%d",&item);
push(item);
break;
case 2:
pop();
break;
case 3:
disp();
break;
default:
printf("\nWrong choice");
}
printf("\nContinue 1 for Yes:");
scanf("%d",&i);
}
getch();
}
//push function
void push(int item)
{
if(top==MAX-1)
printf("\nstack is full");
else
{
top++;
s[top]=item;
}
}

//pop function
void pop()
{
int item;
if(top==-1)
printf("\nStack is empty:");
else
{
item=s[top];
top--;
printf("\nDeleted Element is %d",item);
}
}

//Display function
void disp()
{
int i;
printf("\n");
for(i=0;i<=top;i++)
printf("%d\t",s[i]);
}
Output:
2. //Multiple stack implementation using a single Array
#include<stdio.h>
#include<conio.h>
#define MAX 10
int s[MAX];
int top1=-1,top2=MAX;
void push1(int); //to push in the first stack
void push2(int); //to push in the second stack
void pop1(); //pop from stack1
void pop2(); //pop from stack2
void disp1(); //display for stack1
void disp2(); //display for stack2
void main()
{
int i=1,choice,item;
clrscr();
while(i==1)
{
printf("\n1.Push s1 2. Push s2 3. Pop1 4. Pop2 5.Display1 6. Display2");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter item to push in stack1:");
scanf("%d",&item);
push1(item);
break;
case 2:
printf("\nEnter item to push in stack2:");
scanf("%d",&item);
push2(item);
break;
case 3:
pop1();
break;
case 4:
pop2();
break;
case 5:
disp1();
break;
case 6:
disp2();
break;
default:
printf("\nWrong choice");
}
printf("\nContinue 1 for Yes:");
scanf("%d",&i);
}
getch();
}

//push function for stack1


void push1(int item)
{
if(top1<top2-1)
{
top1++;
s[top1]=item;
}
else
printf("\nstack is full");
}

//push function for stack2


void push2(int item)
{
if(top2>top1+1)
{
top2--;
s[top2]=item;
}
else
printf("\nstack is full");
}
//pop function for stack1
void pop1()
{
int item;
if(top1==-1)
printf("\nStack1 is empty:");
else
{
item=s[top1];
top1--;
printf("\nDeleted Element from stack1 is %d",item);
}
}

//pop function for stack2


void pop2()
{
int item;
if(top2==MAX)
printf("\nStack2 is empty:");
else
{
item=s[top2];
top2++;
printf("\nDeleted Element from stack2 is %d",item);
}
}

//Display function for stack1


void disp1()
{
int i;
printf("\n");
for(i=0;i<=top1;i++)
printf("%d\t",s[i]);
}
//Display function for stack2
void disp2()
{
int i;
printf("\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",s[i]);
}

Output;
3. //program for Tower of Hanoi
#include<stdio.h>
#include<conio.h>
void tower(int,char,char,char);
void main()
{
int n;
char a='A',b='B',c='C';
clrscr();
printf("\nHow many disc:");
scanf("%d",&n);
tower(n,a,b,c);
getch();
}

//function
void tower(int n,char a,char b,char c)
{
if(n!=0)
{
tower(n-1,a,c,b);
printf("\nMove disc from %c to %c",a,c);
tower(n-1,b,a,c);
}
}
4. //program in c to convert infix to postfix
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define SIZE 50
char s[SIZE],infix[SIZE],post[SIZE];
int top=-1;
void push(char);
char pop();
int pre(char);
void main()
{
char ch;
int i,k,p=0;
clrscr();
printf("\nEnter an infix expression:");
gets(infix);
for(i=0;infix[i]!='\0';i++)
{
ch=infix[i];
if(isalnum(ch))
{
post[p++]=ch;
}
else
if(ch=='(')
push(ch);
else
if(ch==')')
{
while(s[top]!='(')
{
post[p++]=pop();
}
pop();
}
else
if(ch=='/'||ch=='*'||ch=='+'||ch=='-')
{
if(pre(ch)>pre(s[top]))
push(ch);
else
{
while(pre(ch)<=pre(s[top]))
post[p++]=pop();
push(ch);
}
}
}
while(top>-1)
post[p++]=pop();
post[p]='\0';
printf("\nPostfix expression is :%s",post);
} //end main

//function for the precedence of operators


int pre(char ch)
{
if(ch=='('||ch==')')
return 0;
else
if(ch=='+'||ch=='-')
return 1;
else
if(ch=='/'||ch=='*')
return 2;
}

//push function
void push(char item)
{
if(top>=SIZE-1)
printf("\nStack overflow:");
else
{
top++;
s[top]=item;
}
}
//pop function
char pop()
{
char item;
if(top==-1)
printf("\nStack underflow");
else
{
item=s[top];
top--;
return item;
}
}
5. //program in c to evaluate postfix expression
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define SIZE 50
char s[SIZE],post[SIZE];
int top=-1;
void push(int);
int pop();
void main()
{
char ch;
int i,v,a,b;
clrscr();
printf("\nEnter an postfix expression:");
gets(post);
for(i=0;post[i]!='\0';i++)
{
ch=post[i];
if(isdigit(ch))
{
push(ch-'0');
}
else
if(ch=='+'||ch=='*'||ch=='-'||ch=='/')
{
a=pop();
b=pop();

switch(ch)
{
case '*':
v=b*a;
break;
case '/':
v=b/a;
break;
case '+':
v=b+a;
break;
case '-':
v=b-a;
break;
}
push(v);
}
}
printf("\nResult is:%d",pop());
}

//push function
void push(int item)
{
if(top>=SIZE-1)
printf("\nStack overflow:");
else
{
top++;
s[top]=item;
}
}

//pop function
int pop()
{
int item;
if(top==-1)
printf("\nStack underflow");
else
{
item=s[top];
top--;
return item;
}
}
6. //program for array implementation of queue
#include<stdio.h>
#include<conio.h>
#define MAX 5
int q[MAX],r=-1,f=-1;
void insert();
void del();
void disp();
void main()
{
int i=1,choice;
clrscr();
while(i==1)
{
printf("\n1. Insert \n2. Delete \n3. Display");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break();
case 3:
disp();
break;
default:
printf("\nChoice is wrong:");
} //end switch
printf("\nContinue 1 for yes:");
scanf("%d",&i);
} //end while
getch();
} //end main
//insert function
void insert()
{
int item;
if(r==MAX-1)
printf("\nQueue is full");
else
{
if(f==-1)
f=0;
printf("\nEnter item to insert:");
scanf("%d",&item);
r++;
q[r]=item;
}
}

//delete function
void del()
{
int item;
if(f==-1)
printf("\nqueue empty");
else
{
if(r==f) //last element
{
item=q[f];
printf("\nDeleted element is:%d",item);
r=f=-1;
}
else
{
item=q[f];
printf("\nDeleted element is:%d",item);
f++;
}
}
}
//display function
void disp()
{
int i;
printf("\n");
if(f==-1)
printf("\nQueue empty");
else
{
for(i=f;i<=r;i++)
printf("%d\t",q[i]);
}
}
7. //program for circular queue
#include<stdio.h>
#include<conio.h>
#define MAX 5
int q[MAX],r=-1,f=-1;
void insert();
void del();
void disp();
void main()
{
int i=1,choice;
clrscr();
while(i==1)
{
printf("\n1. Insert \n2. Delete \n3. Display");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
default:
printf("\nChoice is wrong:");
} //end switch
printf("\nContinue 1 for yes:");
scanf("%d",&i);
} //end while
getch();
} //end main
//insert function
void insert()
{
int item;
if((f==0 && r==MAX-1)||(r==f-1))
printf("\nQueue is full");
else
{
if(f==-1)
f=r=0;
else
if(f!=0 && r==MAX-1)
r=0;
else
r++;
printf("\nEnter item to insert:");
scanf("%d",&item);
q[r]=item;
}
}

//delete function
void del()
{
int item;
if(f==-1)
printf("\nqueue empty");
else
{
if(r==f) //last element
{
item=q[f];
printf("\nDeleted element is:%d",item);
r=f=-1;
}
else
if(f==MAX-1)
{
item=q[f];
printf("\nDeleted element is:%d",item);
f=0;
}
else
{
item=q[f];
printf("\nDeleted element is:%d",item);
f++;
}
}
}

//display function
void disp()
{
int i;
printf("\n");
if(f==-1)
printf("\nQueue empty");
else
if(f>r)
{
for(i=0;i<=r;i++)
printf("%d\t",q[i]);
for(i=f;i<=MAX-1;i++)
printf("%d\t",q[i]);
}
else
{
for(i=f;i<=r;i++)
printf("%d\t",q[i]);
}
}
8. //Program for singly linked list
#include<stdio.h>
#include<conio.h>
#define NULL 0
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *start,*temp,*temp1,*newnode;
void create(); //function for the creation of linked list
void insert_b(); //insert new node at the beginning
void insert_e(); //insert new node at the end
void insert_p(); //insert new node in between two nodes
void del_f(); //delete first node
void del_l(); //delete last node
void del_m(); //delete intermediate node
void disp(); //display linked list
void main()
{
int i=1,choice;
clrscr();
while(i==1)
{
printf("\n1. Create\n2. Insert at Beg\n3. Insert at Pos\n4. insert at end");
printf("\n5. Del first \n6. Del last \n7. Del intermedate \n8. Display");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert_b();
break;
case 3:
insert_p();
break;
case 4:
insert_e();
break;
case 5:
del_f();
break;
case 6:
del_l();
break;
case 7:
del_m();
break;
case 8:
disp();
break;
default:
printf("\nChoice is wrong");
}
printf("\nContinue 1 for yes");
scanf("%d",&i);
}
}

//function for the creation of linked list


void create()
{
int k=1;
start=(node *)malloc(sizeof(node));
newnode=start;
do
{
printf("Enter info new node:");
scanf("%d",&newnode->info);
printf("More node 1 for yes:");
scanf("%d",&k);
if(k==1)
{
newnode->next=(node *)malloc(sizeof(node));
newnode=newnode->next;
}
else
newnode->next=NULL;
}while(k==1);
}
//function to insert node at the beginning
void insert_b()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=start;
start=newnode;
}

//function to insert node at the end


void insert_e()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=NULL;
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}

//function to insert node at particular position


void insert_p()
{
int t;
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
printf("\nEnter info of target node:");
scanf("%d",&t);
temp=start;
while(temp->next!=NULL)
{
if(temp->next->info==t)
break;
else
temp=temp->next;
}
temp1=temp->next;
temp->next=newnode;
newnode->next=temp1;
}

//function to delete first node


void del_f()
{
if(start->next==NULL) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=start;
printf("\nDeleted node:%d",temp->info);
temp=temp->next;
start=temp;
}
}

//function to delete last node


void del_l()
{
if(start->next==NULL) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=start;
while(temp->next->next!=NULL)
temp=temp->next;
printf("\nDeleted node:%d",temp->next->info);
temp->next=NULL;
}
}
//function to delete intermediate node
void del_m()
{
int item;
temp=start;
printf("\nWhich node to be deleted:");
scanf("%d",&item);
while(temp->next->next!=NULL)
{
if(temp->next->info==item)
{
printf("\nDeleted element is:%d",temp->next->info);
temp->next=temp->next->next;
break;
}
temp=temp->next;
}
}

//function to display linked list


void disp()
{
temp=start;
printf("\n");
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}
10. //Program for circular linked list
#include<stdio.h>
#include<conio.h>
#define NULL 0
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *start,*temp,*temp1,*newnode;
void create(); //function for the creation of linked list
void insert_b(); //insert new node at the beginning
void insert_e(); //insert new node at the end
void insert_p(); //insert new node at the position
void del_f(); //delete first node
void del_l(); //delete last node
void del_m(); //delete intermediate node
void disp(); //display linked list
void main()
{
int i=1,choice;
clrscr();
while(i==1)
{
printf("\n1. Create\n2. Insert at Beg\n3. Insert at Pos\n4. insert at end");
printf("\n5. Del first \n6. Del last \n7. Del intermediate \n8. Display");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert_b();
break;
case 3:
insert_p();
break;
case 4:
insert_e();
break;
case 5:
del_f();
break;
case 6:
del_l();
break;
case 7:
del_m();
break;
case 8:
disp();
break;
default:
printf("\nChoice is wrong");
}
printf("\nContinue 1 for yes");
scanf("%d",&i);
}
}

//function for the creation of linked list


void create()
{
int k=1;
start=(node *)malloc(sizeof(node));
newnode=start;
do
{
printf("Enter info new node:");
scanf("%d",&newnode->info);
printf("More node 1 for yes:");
scanf("%d",&k);
if(k==1)
{
newnode->next=(node *)malloc(sizeof(node));
newnode=newnode->next;
}
else
newnode->next=start;
}while(k==1);
//function to insert node at the beginning
void insert_b()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=start;
temp=start;
while(temp->next!=start)
temp=temp->next;
start=newnode;
temp->next=start;
}

//function to insert node at the end


void insert_e()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=start;
temp=start;
while(temp->next!=start)
temp=temp->next;
temp->next=newnode;
}

//function to insert node at particular position


void insert_p()
{
int t;
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
printf("\nEnter info of target node:");
scanf("%d",&t);
temp=start;
while(temp->next!=start)
{
if(temp->next->info==t)
break;
else
temp=temp->next;
}
temp1=temp->next;
temp->next=newnode;
newnode->next=temp1;
}

//function to delete first node


void del_f()
{
if(start->next==start) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=start;
printf("\nDeleted node:%d",start->info);
while(temp->next!=start)
temp=temp->next;
start=start->next;
temp->next=start;
}
}

//function to delete last node


void del_l()
{
if(start->next==start) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=start;
while(temp->next->next!=start)
temp=temp->next;
printf("\nDeleted node:%d",temp->next->info);
temp->next=start;
}
}

//function to delete intermediate node


void del_m()
{
int item;
temp=start;
printf("\nWhich node to be deleted:");
scanf("%d",&item);
while(temp->next->next!=start)
{
if(temp->next->info==item)
{
printf("\nDeleted element is:%d",temp->next->info);
temp->next=temp->next->next;
break;
}
temp=temp->next;
}
}

//function to display linked list


void disp()
{
temp=start;
printf("\n");
while(temp->next!=start)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}
12. //Program for doubly linked list
#include<stdio.h>
#include<conio.h>
#define NULL 0
struct list
{
struct list *prev;
int info;
struct list *next;
};
typedef struct list node;
node *start,*last,*temp,*temp1,*newnode;
void create(); //function for the creation of linked list
void insert_b(); //insert new node at the beginning
void insert_e(); //insert new node at the end
void insert_p(); //insert new node at the position
void del_f(); //delete first node
void del_l(); //delete last node
void del_m(); //delete intermediate node
void disp_b(); //display from first node
void disp_l(); //display from last node
void main()
{
int i=1,choice;
clrscr();
while(i==1)
{
printf("\n1. Create\n2. Insert at Beg\n3. Insert at Pos\n4. insert at end");
printf("\n5. Del first \n6. Del last \n7. Del intermedate \n8. Display forward");
printf("\n9. Display Backward");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert_b();
break;
case 3:
insert_p();
break;
case 4:
insert_e();
break;

case 5:
del_f();
break;
case 6:
del_l();
break;
case 7:
del_m();
break;
case 8:
disp_b();
break;
case 9:
disp_l();
break;
default:
printf("\nChoice is wrong");
}
printf("\nContinue 1 for yes");
scanf("%d",&i);
}
}

//function for the creation of linked list


void create()
{
int k=1;
newnode=(node *)malloc(sizeof(node));
start=newnode;
newnode->prev=NULL;
do
{
printf("Enter info new node:");
scanf("%d",&newnode->info);
printf("More node 1 for yes:");
scanf("%d",&k);
if(k==1)
{
newnode->next=(node *)malloc(sizeof(node));
newnode->next->prev=newnode;
newnode=newnode->next;
}
else
{
newnode->next=NULL;
last=newnode;
}
}while(k==1);
}

//function to insert node at the beginning


void insert_b()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->prev=NULL;
newnode->next=start;
start->prev=newnode;
start=newnode;
}

//function to insert node at the end


void insert_e()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=NULL;
newnode->prev=last;
last->next=newnode;
last=newnode;

}
//function to insert node at particular position
void insert_p()
{
int t;
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
printf("\nEnter info of target node:");
scanf("%d",&t);
temp=start;
while(temp->next!=NULL)
{
if(temp->next->info==t)
break;
else
temp=temp->next;
}
if(temp->next==NULL)
printf("\nNode not fund");
else
{
newnode->next=temp->next;
newnode->prev=temp;
temp->next->prev=newnode;
temp->next=newnode;
}
}

//function to delete first node


void del_f()
{
if(start->next==NULL) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=start->next;
printf("\nDeleted node:%d",start->info);
temp->prev=NULL;
start=temp;
}
}

//function to delete last node


void del_l()
{
if(start->next==NULL) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=last->prev;
printf("\nDeleted node:%d",last->info);
temp->next=NULL;
last=temp;
}
}

//function to delete intermediate node


void del_m()
{
int item;
temp=start;
printf("\nWhich node to be deleted:");
scanf("%d",&item);
while(temp->next->next!=NULL)
{
if(temp->next->info==item)
{
printf("\nDeleted element is:%d",temp->next->info);
temp->next=temp->next->next;
temp->next->prev=temp;
break;
}
temp=temp->next;
}
}
//function to display linked list forward
void disp_b()
{
temp=start;
printf("\n");
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}

//function to display linked list backward


void disp_l()
{
temp=last;
printf("\n");
while(temp->prev!=NULL)
{
printf("%d\t",temp->info);
temp=temp->prev;
}
printf("%d",temp->info);
}
//Program for singly linked list
#include<stdio.h>
#include<conio.h>
#define NULL 0
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *start,*temp,*temp1,*newnode;
void create(); //function for the creation of linked list
void insert_b(); //insert new node at the beginning
void insert_e(); //insert new node at the end
void insert_p(); //insert new node at the position
void del_f(); //delete first node
void del_l(); //delete last node
void del_m(); //delete intermediate node
void disp(); //display linked list
void rev(); //function to reverse linked list
void main()
{
int i=1,choice;
clrscr();
while(i==1)
{
printf("\n1. Create\n2. Insert at Beg\n3. Insert at Pos\n4. insert at end");
printf("\n5. Del first \n6. Del last \n7. Del intermedate \n8. Display");
printf("\n9. Reverse");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert_b();
break;
case 3:
insert_p();
break;
case 4:
insert_e();
break;
case 5:
del_f();
break;
case 6:
del_l();
break;
case 7:
del_m();
break;
case 8:
disp();
break;
case 9:
rev();
break;
default:
printf("\nChoice is wrong");
}
printf("\nContinue 1 for yes");
scanf("%d",&i);
}
}

//function for the creation of linked list


void create()
{
int k=1;
start=(node *)malloc(sizeof(node));
newnode=start;
do
{
printf("Enter info new node:");
scanf("%d",&newnode->info);
printf("More node 1 for yes:");
scanf("%d",&k);
if(k==1)
{
newnode->next=(node *)malloc(sizeof(node));
newnode=newnode->next;
}
else
newnode->next=NULL;
}while(k==1);
}

//function to insert node at the beginning


void insert_b()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=start;
start=newnode;
}

//function to insert node at the end


void insert_e()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=NULL;
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}

//function to insert node at particular position


void insert_p()
{
int t;
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
printf("\nEnter info of target node:");
scanf("%d",&t);
temp=start;
while(temp->next!=NULL)
{
if(temp->next->info==t)
break;
else
temp=temp->next;
}
temp1=temp->next;
temp->next=newnode;
newnode->next=temp1;
}

//function to delete first node


void del_f()
{
if(start->next==NULL) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=start;
printf("\nDeleted node:%d",temp->info);
temp=temp->next;
start=temp;
}
}

//function to delete last node


void del_l()
{
if(start->next==NULL) //last node
{
printf("\nDeleted node:%d",start->info);
start=NULL;
}
else
{
temp=start;
while(temp->next->next!=NULL)
temp=temp->next;
printf("\nDeleted node:%d",temp->next->info);
temp->next=NULL;
}
}

//function to delete intermediate node


void del_m()
{
int item;
temp=start;
printf("\nWhich node to be deleted:");
scanf("%d",&item);
while(temp->next->next!=NULL)
{
if(temp->next->info==item)
{
printf("\nDeleted element is:%d",temp->next->info);
temp->next=temp->next->next;
break;
}
temp=temp->next;
}
}

//function for reversing linked list


void rev()
{
node *pnode,*cnode;
pnode=start;
cnode=start->next;
start=pnode->next;
if(start==NULL)
printf("\nOnly one node in list");
else
{
pnode->next=NULL;
while(start!=NULL)
{
start=start->next;
cnode->next=pnode;
pnode=cnode;
cnode=start;
}
start=pnode;
}
}

//function to display linked list


void disp()
{
temp=start;
printf("\n");
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}
14. //Program for Header linked list
#include<stdio.h>
#include<conio.h>
#define NULL 0
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *start,*temp,*temp1,*newnode;
void create(); //function for the creation of linked list
void insert_b(); //insert new node at the beginning
void insert_e(); //insert new node at the end
void insert_p(); //insert new node at the position
void del_f(); //delete first node
void del_l(); //delete last node
void del_m(); //delete intermediate node
void disp(); //display linked list
int count=0;
void main()
{
int i=1,choice;
clrscr();
while(i==1)
{
printf("\n1. Create\n2. Insert at Beg\n3. Insert at Pos\n4. insert at end");
printf("\n5. Del first \n6. Del last \n7. Del intermedate \n8. Display");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert_b();
break;
case 3:
insert_p();
break;
case 4:
insert_e();
break;
case 5:
del_f();
break;

case 6:
del_l();
break;
case 7:
del_m();
break;
case 8:
disp();
break;
default:
printf("\nChoice is wrong");
}
printf("\nContinue 1 for yes");
scanf("%d",&i);
}
}

//function for the creation of linked list


void create()
{
int k=1;
start=(node *)malloc(sizeof(node));
newnode=start;
while(k==1)
{
newnode->next=(node *)malloc(sizeof(node));
newnode=newnode->next;
count++;
printf("Enter info new node:");
scanf("%d",&newnode->info);
printf("More node 1 for yes:");
scanf("%d",&k);
if(k!=1)
{
newnode->next=NULL;
start->info=count;
break;
}
}
}

//function to insert node at the beginning


void insert_b()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=start->next;
start->next=newnode;
start->info=++count;
}
//function to insert node at the end
void insert_e()
{
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
newnode->next=NULL;
temp=start->next;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
start->info=++count;
}
//function to insert node at particular position
void insert_p()
{
int t;
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:");
scanf("%d",&newnode->info);
printf("\nEnter info of target node:");
scanf("%d",&t);
temp=start;
while(temp->next!=NULL)
{
if(temp->next->info==t)
break;
else
temp=temp->next;
}
temp1=temp->next;
temp->next=newnode;
newnode->next=temp1;
start->info=++count;
}

//function to delete first node


void del_f()
{
temp=start->next;
if(temp->next==NULL) //last node
{
printf("\nDeleted node:%d",temp->info);
start->next=NULL;
}
else
{
printf("\nDeleted node:%d",temp->info);
temp=temp->next;
start->next=temp;
}
start->info=--count;
}

//function to delete last node


void del_l()
{
temp=start->next;
if(temp->next==NULL) //last node
{
printf("\nDeleted node:%d",temp->info);
start->next=NULL;
}
else
{
while(temp->next->next!=NULL)
temp=temp->next;
printf("\nDeleted node:%d",temp->next->info);
temp->next=NULL;
}
start->info=--count;
}

//function to delete intermediate node


void del_m()
{
int item;
temp=start->next;
printf("\nWhich node to be deleted:");
scanf("%d",&item);
while(temp->next->next!=NULL)
{
if(temp->next->info==item)
{
printf("\nDeleted element is:%d",temp->next->info);
temp->next=temp->next->next;
break;
}
temp=temp->next;
}
start->info=--count;
}
//function to display linked list
void disp()
{
temp=start;
printf("\n");
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}
//program for binary search
#include<stdio.h>
#include<conio.h>
void binary(int a[],int n,int t);
void main()
{
int a[20],n,t,i;
clrscr();
printf("\nHow many numbers in the list:");
scanf("%d",&n);
printf("Enter nos:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter number to search:");
scanf("%d",&t);
binary(a,n,t);
getch();
}

//function for binary search


void binary(int a[],int n,int t)
{
int f,m,l;
f=0;
l=n-1;
while(f<=l)
{
m=(f+l)/2;
if(a[m]==t)
{
printf("\nSearch successful and number found at %d position",m+1);
break;
}
else
if(a[m]>t)
l=m-1;
else
f=m+1;
}
if(f>l)
printf("\nSearch unsuccessful");
}

//program for linear search


#include<stdio.h>
#include<conio.h>
void linear(int a[],int n,int t);
void main()
{
int a[20],n,t,i;
clrscr();
printf("\nHow many numbers in the list:");
scanf("%d",&n);
printf("Ente nos:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter number to search:");
scanf("%d",&t);
linear(a,n,t);
getch();
}

//function for linear search


void linear(int a[],int n,int t)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==t)
{
printf("\nSearch successful and number found at %d position",i+1);
break;
}
}
if(i==n)
printf("\nSearch unsuccessful");
}
//program for bubble sort
#include<stdio.h>
#include<conio.h>
void bubble(int a[],int);
void main()
{
int a[20],n,i;
clrscr();
printf("\nHow many numbers in the list:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a,n);
getch();
}

//function for bubble sort


void bubble(int a[],int n)
{
int i,j,k,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\nResult after %d pass:\n",i+1);
for(k=0;k<n;k++)
printf("%d ",a[k]);
}
printf("\nResult after sorting:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

//program for insertion sort


#include<stdio.h>
#include<conio.h>
void insert(int a[],int);
void main()
{
int a[20],n,i;
clrscr();
printf("\nHow many numbers in the list:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insert(a,n);
getch();
}

//function for insertion sort


void insert(int a[],int n)
{
int i,j,k,m;
for(i=0;i<n;i++)
{
k=a[i];
j=i-1;
while(j>=0 && a[j]>k)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=k;
printf("\nResult after %d pass:\n",i+1);
for(m=0;m<n;m++)
printf("%d ",a[m]);
}
printf("\nResult after sorting:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
//program for selection sort
#include<stdio.h>
#include<conio.h>
void selection(int a[],int);
void main()
{
int a[20],n,i;
clrscr();
printf("\nHow many numbers in the list:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a,n);
getch();
}

//function for selection sort


void selection(int a[],int n)
{
int i,j,k,min,temp;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
printf("\nResult after %d pass:\n",i+1);
for(k=0;k<n;k++)
printf("%d ",a[k]);
}
printf("\nResult after sorting:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
//program for quick sort
#include<stdio.h>
#include<conio.h>
void quick(int a[],int,int);
int partition(int a[],int,int);
void main()
{
int a[20],n,i;
clrscr();
printf("\nHow many numbers in the list:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick(a,0,n-1);
getch();
}

//function for quick sort


void quick(int a[],int p,int r)
{
int i,q;
if(p<r)
{
q=partition(a,p,r);
quick(a,p,q-1);
quick(a,q+1,r);
}
printf("\nResult after sorting:\n");
for(i=0;i<=r;i++)
printf("%d ",a[i]);
}
//partition function
int partition(int a[],int p,int r)
{
int i,j,x,temp;
x=a[p];
i=p;
j=r+1;
do
{
do
{
i++;
}while(a[i]<x&&i<=r);
do
{
j--;
}while(x<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[p]=a[j];
a[j]=x;
return j;
}
//program for merge sort
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int,int);
void merge(int a[],int,int,int);
void main()
{
int i,n,a[20];
clrscr();
printf("\nHow many numbers:");
scanf("%d",&n);
printf("\nEnter numbers:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
}

//mergesort function
void mergesort(int a[],int f,int l)
{
int m,i;
if(f<l)
{
m=(f+l)/2;
mergesort(a,f,m);
mergesort(a,m+1,l);
merge(a,f,m,l);
}

//function to merge subarrays


void merge(int a[],int f,int m,int l)
{
int i,j,k;
int n1=m-f+1;
int n2=l-m;
int t1[10],t2[10];
for(i=0;i<n1;i++)
t1[i]=a[f+i];
for(j=0;j<n2;j++)
t2[j]=a[m+1+j];
k=f;
i=0;
j=0;
while(i<n1 && j<n2)
{
if(t1[i]<=t2[j])
{
a[k]=t1[i++];
}
else
a[k]=t2[j++];
k++;
}
while(i<n1)
a[k++]=t1[i++];
while(j<n2)
a[k++]=t2[j++];
printf("\nResult after sorting:\n");
for(i=0;i<=l;i++)
printf("%d\t",a[i]);
}
//program for Radix sort
#include<stdio.h>
#include<conio.h>
void radix(int a[],int);
int largest(int a[],int);
void main()
{
int i,n,a[10];
clrscr();
printf("\nHow many numbers:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
radix(a,n);
printf("\nSorted array is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

//function to perform sorting


void radix(int a[],int n)
{
int b[10][10],bk[10];
int i,j,k,r,nop=0,div=1,large,p;
large=largest(a,n);
printf("\nLargest element is %d",large);
while(large>0)
{
nop++;
large/=10;
}
for(p=0;p<nop;p++)
{
for(i=0;i<10;i++)
bk[i]=0;
for(i=0;i<n;i++)
{
r=(a[i]/div)%10;
b[r][bk[r]]=a[i];
bk[r]+=1;
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bk[k];j++)
{
a[i]=b[k][j];
i++;
}
}
div*=10;
printf("\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
}

//function to find largest


int largest(int a[],int n)
{
int large=a[0],i;
for(i=1;i<n;i++)
{
if(large<a[i])
large=a[i];
}
return large;
}

You might also like