Saturday, 15 February 2014
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");
}
Infix to Postfix cprogram
aim: to implement a infix to post fix c-program:
program:
#include<ctype.h>
#include<stdio.h>
char stack[80];
int top=-1;
char pop();
void main()
{
char instring[80],outstring[80];
int l,p=0,i;
clrscr();
printf("Enter the infix expression\n");
scanf("%s",instring);
l=strlen(instring);
for(i=0;i<l;i++)
{
if((toupper(instring[i])>='A')&&(toupper(instring[i])<='Z'))
outstring[p++]=toupper(instring[i]);
else if(instring[i]=='(')
push(instring[i]);
else if(instring[i]==')')
{
while(stack[top]!='(')
outstring[p++]=pop();
pop();
}
else
{
while(stack_preced(stack[top])>=in_preced(instring[i]))
outstring[p++]=pop();
push(instring[i]);
}
}
while(top>=0)
outstring[p++]=pop();
outstring[p--]='\0';
printf("postfix expresion=%s",outstring);
getch();
}
stack_preced(char x)
{
switch(x)
{
case '+':
case '-':return(1);
case '*':
case '/':return(2);
case '$':return(3);
case '(':return(0);
}
}
in_preced(char x)
{
switch(x)
{
case '+':
case '-':return(1);
case '*':
case '/':return(2);
case '$':return(3);
case '(':return(4);
}
}
push(char c)
{
stack[++top]=c;
}
char pop()
{
char c;
c=stack[top];
top--;
return(c);
}
Subscribe to:
Posts (Atom)





