Saturday, 15 February 2014

Queue using linked list cprogram

Aim:Implementation of queue using linked list in C program

to download file   :click here

Program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void enque(int);

void deque();

void display();

struct queue

{

int data;

struct queue *next;

};

typedef struct queue node;

node *rear,*front,*new,*dfront;

void main()

{

int ch,item;

clrscr();

front=rear=NULL;

while(1)

{

printf("1.enque\t2.deque\t3.display\t4.exit\n");

printf("enter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("enter the element to insert");

scanf("%d",&item);

enque(item);

break;

case 2:

deque();

break;

case 3:

printf("elements in queue are: ");

display();

break;

default :

exit(0);

}

}

}

void enque(int item)

{

if(rear==NULL)

{

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

rear->data=item;

rear->next=NULL;

front=rear;

}

else

{

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

new->data=item;

new->next=NULL;

rear->next=new;


rear=new;

}

}

void deque()

{

if(front==NULL)

printf("underflow\n");

else

{

printf("element deleted is %d\n",front->data);

if(front==rear)

front=rear=NULL;

else

front=front->next;

}

}

void display()

{       dfront=front;

while(dfront!=NULL)/*dfront means duplicate front*/

{

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

dfront=dfront->next;

}

printf("\n");

}

Output:






Binary search tree operations(insertion,deletion) cprogram

Aim: to implement binary search operation(insertion,deletion) in c program
program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void insertion(int);

void display();

struct bsearch

{

int data;

struct bsearch *rchild,*lchild;

};

typedef struct  bsearch node;

node *root,*ptr,*new,*parent,*temro;

void main()

{

int ch,item,rt,item2;

clrscr();

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

printf("enter the root for bst tree");

scanf("%d",&rt);

root->data=rt;

root->rchild=NULL;

root->lchild=NULL;

while(1)

{

printf("1.insert  2.delete  3.display  4.eixt");

printf("\tenter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("enter the item to insert");

scanf("%d",&item);

insertion(item);

break;

case 2:

printf("enter the item to be deleted");

scanf("%d",&item);

deletion(root,item);

break;

case 3:

printf("inoder=");

display(root);

printf("\n");

break;

default :

exit(0);

}

}

}

void insertion(int item)

{

ptr=root;

while(ptr!=NULL)

{

if(ptr->data==item)

{

printf("item already exit insertion not possible");

exit(0);

}

else if(item<ptr->data)

{

parent=ptr;

ptr=ptr->lchild;

}

else

{

parent=ptr;

ptr=ptr->rchild;

}

if(ptr==NULL)

{

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

new->data=item;

new->lchild=NULL;

new->rchild=NULL;

if(item<parent->data)

parent->lchild=new;

else

parent->rchild=new;

}

}

}

void display(struct bsearch *root)

{

if(root!=NULL)

{

display(root->lchild);

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

display(root->rchild);

}

}

deletion(struct bsearch *root,int item)

{

node *ptr1,*insuc;

int found=0,cas,temp;

ptr=root;

while(ptr!=NULL)

{

if(ptr->data==item)

{

found=1;

break;

}

else if(item<ptr->data)

{

parent=ptr;

ptr=ptr->lchild;

}

else

{

parent=ptr;

ptr=ptr->rchild;

}

}

if(found==0)

printf("item to be deleted is not found");

else

{

if(ptr->lchild==NULL&&ptr->rchild==NULL)

cas=1;

else if(ptr->lchild!=NULL&&ptr->rchild!=NULL)

cas=3;

else

cas=2;

}

if(cas==1)

{

if(parent->lchild->data==item)

{

parent->lchild=NULL;

free(ptr);

}

else

{

parent->rchild=NULL;

free(ptr);

}

}

if(cas==2)

{

if(parent->lchild->data==item)

{

if(ptr->lchild!=NULL)

{

parent->lchild=ptr->lchild;

free(ptr);

}

else

{

parent->lchild=ptr->rchild;

free(ptr);

}

}

else

{

if(ptr->lchild!=NULL)

{

parent->rchild=ptr->lchild;

free(ptr);

}

else

{

parent->rchild=ptr->rchild;

free(ptr);

}


}

}

if(cas==3)

{

ptr1=ptr->rchild;

while(ptr1!=NULL)

{

insuc=ptr1;

ptr1=ptr1->lchild;

}

temp=insuc->data;

ptr->data=temp;

deletion(insuc,temp);


}

}

output:















Representation of polynomials using linked list

Aim: To Represent polynomials using linked list in cprogram

program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void display();

void insert();

struct poly

{

int coef,exp; 

struct poly *next;

};

typedef struct poly node;

node *header,*ptr,*temp;

void main()

{

int ch;

clrscr(); 

        header->coef=NULL;

header->exp=NULL;

header->next=NULL;

while(1)

{ 

printf("1.insert poly\t 2.display\t 3.exit\n"); 

printf("enter your choice");  

                scanf("%d",&ch);

switch(ch)

{  

case 1: insert();  

                                        break;  

case 2:  

display(); 

break;  

default:  

exit(0); 

}

}

void insert()

{ 

       int coef,exp; 

       printf("enter coefficients,exponen for term ");

scanf("%d%d",&coef,&exp);

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

ptr->coef=coef; ptr->exp=exp; 

       ptr->next=NULL; 

       if(header->next==NULL) '

header->next=ptr;

else

{  

               temp=header;  

               while(temp->next!=NULL) 

temp=temp->next; 

temp->next=ptr;

}

}

void display()

{

printf("header->"); 

      ptr=header->next;

while(ptr!=NULL)

{ 

printf("[%d][%d]->",ptr->coef,ptr->exp); 

ptr=ptr->next; 

       }

printf("NULL\n");
}

out put:





Wednesday, 12 February 2014

Operations On linked lists(Insertion,Deletion,Reverse list) Cprogram

Aim: To implement linked list operations(Insertion,Deletion,Reverse list) in Cprogram

Program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void create();

void deletion();

void display();

void insert();

void reverse();

struct list

{

int data;

struct list *next;

};

typedef struct list node;

node *header;

void main()

{

int ch,item;

clrscr();

while(1)

{

printf("1.create  2.insert  3.deletion  4.display  5.reverese  6.exit\n");

printf("enter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

create();

break;

case 3:

deletion();

break;

case 4:

display();

break;

case 2:

insert();

break;

case 5:

reverse();

break;

default :

exit(0);

}

}

}

void create()

{

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

header->data=NULL;

header->next=NULL;


}

void insert()

{

node *new,*ptr,*prev;

int item,pos,element;

printf("enter the item you want 2 insert");

scanf("%d",&item);

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

new->data=item;

printf("enter position of insertio 1.begining\t 2.end\t 3.any position");

scanf("%d",&pos);

switch(pos)

{

case 1:

new->next=header->next;

header->next=new;

break;

case 2:

ptr=header;

while(ptr->next!=NULL)

ptr=ptr->next;

new->next=NULL;

ptr->next=new;

break;

case 3:

printf("enter the element after which you want 2 insert:");

scanf("%d",&element);

ptr=header->next;

while(ptr->next!=NULL&&ptr->data!=element);

ptr=ptr->next;

if(ptr->data==element)

{

new->next=ptr->next;

ptr->next=new;

}

else

{

printf("element not found\n");

break;

}

}

}

void deletion()

{

node *ptr,*prev;

int pos,item;

printf("1.delete front \t2. delete last\t 3.delete any");

scanf("%d",&pos);

switch(pos)

{

case 1:

{

ptr=header->next;

if(ptr==NULL)

printf("list is empty\n");

else

{

header->next=ptr->next;

free(ptr);

}

break;

}

case 2:

{

ptr=header->next;

prev=header;

if(ptr==NULL)

printf("list is empty\n");

else

{

while(ptr->next!=NULL)

{

prev=ptr;

ptr=ptr->next;

}

prev->next=NULL;

free(ptr);

}

break;

}

case 3:

{

ptr=header->next;

prev=header;

printf("enter the item you want to delete");

scanf("%d",&item);

while(ptr->data!=item&&ptr->next!=NULL)

{

prev=ptr;

ptr=ptr->next;

}

if(ptr->data==item)

{

prev->next=ptr->next;

free(ptr);

}

else

printf("item not found\n");

break;

}

}

}

void display()

{

node *ptr;

ptr=header->next;

printf("header->");

while(ptr!=NULL)

{

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

ptr=ptr->next;

}

printf("NULL\n");

}

void reverse()

{

node *p1,*p2,*p3,*last;

p1=header->next;

if(p1==NULL)

printf("list is empty");

else

{

p2=p1->next;

p3=p2->next;

last=header->next;

while(last->next!=NULL)

last=last->next;

if(p3==NULL)

{

p1->next=NULL;

p2->next=p1;

header->next=p2;

}

else

{

p1->next=NULL;

while(p3!=last)

{

p2->next=p1;

p1=p2;

p2=p3;

p3=p3->next;

}

p2->next=p1;

p3->next=p2;

header->next=p3;

}

}

display();

}

output:















Stack using linked list cprogram

Aim: Stack Using Linked List In Cprogram

Program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void push(int);

void pop();

void display();

struct list

{

int data;

struct list *next;

};

typedef struct list node;

node *header,*top;



void main()

{

int ch,item;

clrscr();

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

header->data=NULL;

header->next=NULL;

top=header->next;

while(1)

{

printf("1.push\t 2.pop\t 3.display\t 4.exit\n");

printf("enter your choice\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("enter the item you want to push\n");

scanf("%d",&item);

push(item);

break;

case 2:

pop();

break;

case 3:

display();

break;

default:

exit(0);

}

}

}

void push(int item)

{

node *new;

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

new->data=item;

new->next=top;

top=new;

header->next=new;

}

void pop()

{

top=header->next;

if(top==NULL)

printf("underflow\n");

else

{

header->next=top->next;

free(top);

top=header->next;

}

}

void display()

{

node *ptr;

ptr=header->next;

printf("header->");

while(ptr!=NULL)

{

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

ptr=ptr->next;

}

printf("NULL\n");


}

Output:


Circular Queue using array cprogram

Aim: To Implement Circular Queue using array in C program

program:

#include<stdio.h>

#include<conio.h>

#define maxsize 5

void insertion();

int deletion();

void display();

int rear=0,front=0;

int cqueue[maxsize];

void main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.insertion\t 2.deletion 3.display\t 4.exit\n");

printf("enter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("enter the item to insert:");

scanf("%d",&item);

insertion(item);

break;

case 2:

item=deletion();

printf("item  deletion from the cqueue is %d\n",item);

break;

case 3:

display();

break;

default :

exit(0);

}

}

}

void insertion(int item)

{

if(front==0)

{

front=1;

rear=1;

cqueue[rear]=item;

}

else

{

rear=(rear%maxsize)+1;

if(rear==front)

printf("overflow");

else

cqueue[rear]=item;

}

}

int deletion(int item)

{

if(rear==0)

printf("under flow");

else

{

item=cqueue[front];

if(front==rear)

{

front=0;

rear=0;

}

else

front=(front%maxsize)+1;

return item;

}

}

void display()

{

int i;

printf("elements of cqueue are\n");

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

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

printf("\n");

}

output:


Queue using array cprogram

Aim: To Implement queue using arrays in cprogram

program:

#include<stdio.h>

#define MAXSIZE 3

void Insert(int);

void delete();

void Display();

int queue[MAXSIZE],rear=0,front=0;

main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.Insert\t");

printf("2.Delete\t");

printf("3.Dispiay\t");

printf("4.Exit\n");

printf("Enter your chioce : ");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("Enter the item you want to Insert : ");

      scanf("%d",&item);

      Insert(item);

      break;

case 2:item=Delete();

      if(item>0)

      printf("Item deleted from queue is %d\n",item);

      break;

case 3:Display();

      break;

      default:exit(0);

}

}

}

void Insert(int item)

{

if(rear==MAXSIZE)

printf("overflow\n");

else

{

rear=rear+1;

queue[rear]=item;

if(front==0)

front=1;

}

}

int Delete()

{

int item;

if(front==0)

{

printf("underflow\n");

return -1;

}

else

{

item=queue[front];

if(front==rear)

{

front=0;

rear=0;

}

else

front=front+1;

return item;

}

}

void Display()

{

int i;

printf("Elements of queue are :  ");

if(front==rear)

printf("NULL\n");

else

{

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

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

printf("\n");

}

}

output:


Stack using array c program

Aim: To implement stack using array in c program
program:

#include<stdio.h>

#define MAXSIZE 3

void push(int);

int pop();

void display();

int top=0;stack[MAXSIZE];

void main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.push\n");

printf("2.pop\n");

printf("3.display\n");

printf("4.exit\n");

printf("enter your choice:");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("enter the item you want to push:");

scanf("%d",&item);

push(item);

break;

case 2:item=pop();

printf("item deleted from stack is %d\n",item);

break;

case 3:display();

break;

default:exit(0);

}

}

}

void push(int item)

{

if(top==MAXSIZE)

printf("overflow");

else

{

top=top+1;

stack[top]=item;

}

}

int pop()

{

int item;

if(top==0)

{

printf("underflow");

exit(0);

}

else

{

item=stack[top];

top=top-1;

return item;

}

}

void display()

{

int i;

printf("elements of stack are \n");

for(i=1;i<=top;i++)

printf("%3d",stack[i]);

printf("\n");

}