Вы находитесь на странице: 1из 22

Complete link list

#include<stdio.h>

#include<alloc.h>

#include<conio.h>

int i=0;

int a;

struct node

int data;

struct node *link;

}*start,*temp,*x,*temp1;

void main()

int ch;

char ans;

void insertbeg();

void insertmid();

void insertend();

void delbeg();

void delmid();

void delend();

void delall();

void view();

clrscr();
do

printf("1:Insert");

printf("\n2:Delete ");

printf("\n3:View");

printf("\nEnter your choice =>");

scanf("%d",&ch);

switch(ch)

case 1:{

printf("\n1:Insert at Beginning");

printf("\n2:Insert in Middle");

printf("\n3:Insert at End");

printf("\nEnter your choice =>");

fflush(stdin);

scanf("%d",&ch);

switch(ch)

case 1: { insertbeg();

break;

case 2: { insertmid();

break;

case 3: { insertend();

break;
}

break;

case 2:{

printf("\n1:Delete First Element");

printf("\n2:Delete from Middle");

printf("\n3:Delete last Element");

printf("\n4:Delete Entire List");

printf("\nEnter your choice =>");

fflush(stdin);

scanf("%d",&ch);

switch(ch)

case 1: { delbeg();

break;

case 2: { delmid();

break;

case 3: { delend();

break;

case 4: { delall();

break;

break;

}
case 3:{

view();

break;

default:{

printf("\nYou have entered a wrong choice ");

printf("\n\nDo you want to continue =>(y/n)");

fflush(stdin);

scanf("%c",&ans);

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

getch();

void insertbeg()

if(i==0)

temp=start;

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

printf("Enter the data =>");

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

x->link=NULL;

start=x;

temp=start;
i=i+1;

else

temp=start;

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

printf("Enter the data =>");

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

x->link=start;

start=x;

temp=start;

i=i+1;

printf("\n Node Created ");

void insertmid()

temp=start;

printf("Enter the data after which u want the node =>");

scanf("%d",&a);

while(temp->data!=a)

temp=temp->link;

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

printf("Enter the data =>");

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

temp->link=x;

temp=start;

i=i+1;

printf("\nNode Created ");

void insertend()

temp=start;

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

printf("Enter the data =>");

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

while(temp->link!=NULL)

temp=temp->link;

temp->link=x;

x->link=NULL;

printf("\n Node Created ");

i=i+1;

void delbeg()

temp=start;

start=start->link;

free(temp);

temp=start;
i=i-1;

printf("\nFirst Node Deleted....!!");

void delmid()

temp=start;

printf("Enter the data to be deleted =>");

scanf("%d",&a);

while(temp->data!=a)

temp1=temp;

temp=temp->link;

temp1->link=temp->link;

free(temp);

i=i-1;

printf("\nElement Deleted...!!");

void delend()

temp=start;

while(temp->link!=NULL)

temp1=temp;
temp=temp->link;

temp1->link=NULL;

free(temp);

i=i-1;

printf("\nLast Element Deleted...!!");

void delall()

temp=start;

while(start!=NULL)

temp1=temp;

temp=temp->link;

free(temp1);

i=i-1;

start=temp;

printf("\nAll Elements Deleted....List Empty..");

void view()

if(i==0)

printf("Linked list Empty...!! ");

}
else

temp=start;

while(temp->link!=NULL)

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

temp=temp->link;

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

}
Bubble Sort

#include<stdio.h>
#include<conio.h>

void main()
{
int a[20],temp,m,i,j;
clrscr();

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


scanf("%d",&m);
printf("\n Enter %d elements: ",m);
for(i=0;i<m;i++)
scanf("%d",&a[i]);

// BUBBLE SORT
for(i=0;i<m-1;i++)
{
for(j=0;j<m-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("the sorted array is\n");


for(i=0;i<m;i++)
printf("%d\t",a[i]);

getch();
}
Merge Sort

#include<stdio.h>
#include<conio.h>

void main()
{
int a[20],b[20],c[40],m,n,i,j,k,temp;
clrscr();

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


scanf("%d",&m);
printf("\n Enter %d elements: ",m);
for(i=0;i<m;i++)
scanf("%d",&a[i]);

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


scanf("%d",&n);
printf("\n Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);

// BUBBLE SORT
for(i=0;i<m-1;i++)
{
for(j=0;j<m-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

//SELECTION SORT
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}

/* printf("\n Sorted Array (a): "); //to display sorted array a


for(i=0;i<m;i++)
printf("%d ",a[i]);

printf("\n Sorted Array (b): "); //to display sorted array b


for(i=0;i<n;i++)
printf("%d ",b[i]);
*/

i=0;j=0;
for(k=0;k<(m+n);k++)
{
if(i==m)
{
c[k]=b[j];
j++;
}
else if(j==n)
{
c[k]=a[i];
i++;
}
else if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
}

printf("\n Merged Array: ");


for(i=0;i<(m+n);i++)
printf("%d ",c[i]);
getch();
}
Selection Sort

#include<stdio.h>
#include<conio.h>

void main()
{
int b[20],temp,n,i,j;
clrscr();

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


scanf("%d",&n);
printf("\n Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);

//SELECTION SORT
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}

printf("the sorted array is\n");


for(i=0;i<n;i++)
printf("%d\t",b[i]);

getch();
}
Binary search

#include<stdio.h>
#include<conio.h>

void main()
{
int arr[10]={1,2,4,7,8,9,12,34,57,97};
int mid,lower=0,upper=9,num,flag;

clrscr();
printf("Enter the no to be searched");
scanf("%d",&num);

for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(arr[mid]==num)
{
printf("the no is at pos %d in the array",mid);
flag=0;
break;
}
if(arr[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
{
printf("Element found in the array");

}
getch();

}
Binary Search Tree

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

#define TRUE 1
#define FALSE 0

struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};

void insert(struct btreenode**,int);


void delete(struct btreenode**,int);
void search(struct btreenode**,int,struct btreenode**,struct btreenode**,int *);
void inorder(struct btreenode*);

void main()
{
struct btreenode *bt;
int req,i=0,num,a[]={11,9,13,8,10,12,14,15,7};
bt=NULL;

clrscr();

while(i<=8)
{
insert(&bt,a[i]);
i++;
}

clrscr();

printf("\nBinary tree before deletion\n");


inorder(bt);

delete(&bt,10);
printf("\nBinary tree before deletion\n");
inorder(bt);

}
void insert(struct btreenode **sr,int num)
{

if(*sr==NULL)
{
*sr=malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /*search the node to where it is to be attached*/
{
//if new data is less traverse to left
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else //traverse to right
insert(&((*sr)->rightchild),num);
}

void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{

struct btreenode *q;


q=*root;
*found=FALSE;
*par=NULL;

while(q!=NULL)
{

if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}

*par=q;
if(q->data>num)
q=q->leftchild;
else
q=q->rightchild;
}
}
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);

printf("\t%d",sr->data);

inorder(sr->rightchild);
}

void delete(struct btreenode **root,int num)


{
int found;
struct btreenode *parent,*x,*xsucc;

if(*root==NULL)
{
printf("Tree is empty");
return;
}
parent=x=NULL;

search(root,num,&parent,&x,&found);

if(found==FALSE)
{
printf("\ndata to be deleted is not found");
return;
}

if(x->leftchild!=NULL&&x->rightchild!=NULL)
{
parent=x;
xsucc=xsucc->leftchild;

x->data=xsucc->data;
x=xsucc;

}
if(x->leftchild==NULL&&x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->leftchild=NULL;
free(x);
return;
}
if(x->leftchild==NULL&&x->rightchild!=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
if(x->leftchild!=NULL&&x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->leftchild;
free(x);
return;
}
}
Queues

#include<stdio.h> //header files


#include<conio.h>

void insertion(); //forward declaration


void deletion();
void display();

char q[20]; //global variables


char ele;
char ans;
int front=-1,rear=-1,ch,i;

void main()
{

clrscr();
do
{
clrscr();
printf("\n\n What do you want to do? ");
printf("\n\n 1 : INSERT");
printf("\n 2 : DELETE");
printf("\n 3 : DISPLAY");
printf("\n 4 : EXIT");
printf("\n\n Enter your choice here => ");
scanf("%d",&ch);

switch(ch)
{
case 1:{
insertion();
break;
}
case 2:{
deletion();
break;
}
case 3:{
display();
break;
}
case 4: exit();
default: printf("\n\n You have entered a wrong choice.");
}
printf("\n\n Do you want to continue? (y/n): ");
flushall();
scanf("%c",&ans);
}while(ans=='y');

getch();
}

void insertion()
{
if(front==rear)
printf("\n\n Queue is empty.");
printf("\n\n Enter the character to be inserted: ");
flushall();
scanf("%c",&ele);
rear=rear+1;
q[rear]=ele;
}

void deletion()
{
front=front+1;
q[front]=NULL;
printf("\n\n Character %c has been deleted. ",q[front]);
if(front==rear)
printf("\n\n Queue is empty.");

void display()
{
i=front;

if(front==rear)
printf("\n\n Queue is empty.");
front=front+1;
while(front<=rear)
{
printf("%c->",q[front]);
front=front+1;
}

front=i;
}
Stacks

#include<stdio.h> //header files


#include<conio.h>

void push(); //forward declaration


void pop();
void display();

char ans; //global variables declaration


int top=-1,ch,i,ele;
int stack[30];

void main()
{
clrscr();
do
{
clrscr();
printf("\n\n What do you want to do? ");
printf("\n\n 1 : INSERT");
printf("\n 2 : DELETE");
printf("\n 3 : DISPLAY");
printf("\n 4 : EXIT");
printf("\n\n Enter your choice here => ");
scanf("%d",&ch);

switch(ch)
{
case 1:{
push();
break;
}
case 2:{
pop();
break;
}
case 3:{
display();
break;
}
case 4: exit();
default: printf("\n\n You have entered a wrong choice.");
}
printf("\n\n Do you want to continue? (y/n): ");
flushall();
scanf("%c",&ans);
}while(ans=='y');

getch();
}

void push()
{
if(top==-1)
printf("\n\n Stack is empty.");

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


scanf("%d",&ele);
top=top+1;
stack[top]=ele;
printf("\n\n Element %d has been inserted.",ele);
}

void pop()
{
printf("\n\n Element %d has been deleted.",stack[top]);
top=top-1;
if(top==-1)
printf("\n\n Stack is empty.");
}

void display()
{
if(top==-1)
printf("\n\n Stack is empty.");
else
{
printf("\n\n The Resultant Stack: ");
for(i=0;i<=top;i++)
printf("%d",stack[i]);
}
}

Вам также может понравиться