Saturday, 4 March 2017

C program to implement singly queue using array.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
# define MAX 10
int que_arr[MAX];
int rear=-1;
int front=-1;
void insert(int );
int del();
int peek();
void display();
int full();
int empty();
main()
{
int item,choice;
while(1)
{
printf("1.insert\n2.delete\n3.peek\n4.display\n5.exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
 {
  case 1:
    printf("enter item to be insert:");
    scanf("%d",&item);
    insert(item);
    break;
   case 2:
                   item=del();    
      printf("deleted item is : %d\n",item);
  break;
   case 3:
      item=peek();
  printf("item at the front : %d\n",item);    
          break;
   case 4:
      display();
  break;
   case 5:
      exit(1);
      break;
default:
   printf("sorry!this choice not present\n");
               
 }
}
getch();
}
int full()
{
if(rear==MAX-1)
 return 1;
else
 return 0;
}
int empty()
{
if((front==-1)||(front==rear+1))
 return 1;
else
 return 0;
}
void insert(int item)
{
if(full())
{
   printf("queue is overflow");
   exit(1);
    }
    else
    {
    if(front==-1)
    front=0;
    rear=rear+1;
    que_arr[rear]=item;
    }
}
int del()
{
    int item;
if(empty())
{
printf("queue is underflow");
exit(1);
}
else
{
item=que_arr[front];
front=front+1;
return(item);
}
}
int peek()
{  
     if(empty())
{
printf("queue is underflow");
exit(1);
}
else
{
return(que_arr[front]);
}
}
void display()
{  
    int i;
    printf("items are : ");
if(empty())
{
printf("queue is underflow");
exit(1);
}

for(i=front;i<=rear;i++)
printf("%d\t",que_arr[i]);
printf("\n");
}

C program to implement queue using linklist.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct stack
{
int info;
struct stack *next;
}s;
s *front=NULL;
s *rear=NULL;
void push(s *,int);
int pop(s *);
void display(s *);
main()
{
int item,choice;
while(1)
{
printf("\n    ***********\n    1.)push\n    2.)pop\n    3.)display\n    4.)exit\n    Enter your choice : ");
   scanf("%d",&choice);
switch(choice)
{
case 1:
  printf("enter item to be push :");
  scanf("%d",&item);
  push(rear,item);
  break;
case 2:
      item=pop(front);
  printf("popped item is : %d",item);
  break;
case 3:
      display(front);
  break;
case 4:
      exit(1);
  break;
default:
      printf("please enter your choice again");        
}
}
getch();
}
void push(s *p,int item)
{
s*tmp;
tmp=(s *)malloc(sizeof(s));
if(p==NULL)
{
tmp->info=item;
rear=tmp;
rear->next=NULL;
front=tmp;
}
else
{
tmp->info=item;
rear->next=tmp;
rear=tmp;
rear->next=NULL;

}
}
void display(s *p)
{
s *ptr;
ptr=front;
if(front==NULL)
{
printf("queue is empty");
exit(1);
}
else
    {
printf("queue is\n");
printf("       | ");
while(ptr->next!=NULL)
{
printf("%d ",ptr->info);
ptr=ptr->next;
}
printf("%d ",ptr->info);
printf(" | ");
    }
}
int pop(s *p)
{int item;
if(front==NULL)
{
printf("queue is empty");
exit(1);
}
s*tmp;
tmp=front;
item=tmp->info;
front=front->next;
free(tmp);
return item;
}

C program to implement stack using linklist.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct stack
{
int info;
struct stack *next;
}s;
s *top=NULL;
void push(s *,int);
int pop(s *);
void display(s *);
main()
{
int item,choice;
while(1)
{
printf("\n    ***********\n    1.push\n    2.pop\n    3.display\n    4.exit\n    Enter your choice : ");
   scanf("%d",&choice);
switch(choice)
{
case 1:
  printf("enter item to be push :");
  scanf("%d",&item);
  push(top,item);
  break;
case 2:
      item=pop(top);
  printf("popped item is : %d",item);
  break;
case 3:
      display(top);
  break;
case 4:
      exit(1);
  break;
default:
      printf("please enter your choice again");        
}
}
getch();
}
void push(s *p,int item)
{
  s *tmp;
  tmp=(s *)malloc(sizeof(s));
  tmp->info=item;
  tmp->next=top;
  top=tmp;
}
int pop(s *p)
{

int y;
s *tmp;
if(p==NULL)
{
printf("stack is empty");
exit(1);
}
else
{
tmp=top;
top=tmp->next;
y=tmp->info;
free(tmp);
return y;}
}
void display(s *p)
{
s *ptr;
ptr=top;
if(ptr==NULL)
{
printf("stack is empty");
exit(1);
}
else
{

printf("stack is \n");
while(ptr->next!=NULL)
{
printf("|  %d  |\n",ptr->info);
ptr=ptr->next;
}
printf("|  %d  |\n",ptr->info);
}}

C program to implement stack using array.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
int stack_arr[MAX];
int top=-1;
void push(int item);
int pop();
int peek();
void display();
int full();
int empty();
main()
{
int item,choice;
while(1)
{
   printf("1.Push\n2.Pop\n3.peek\n4.Display\n5.Exit\nEnter your choice:");
   scanf("%d",&choice);
   switch(choice)
     {
     case 1:
       printf("enter elemnt to be pushed:");
               scanf("%d",&item);
               push(item);
               break;
         case 2:
             item=pop();
       printf("popped element is : %d\n",item);
       break;
 case 3:
       item=peek();
printf("top element is : %d\n",item);
break;
 case 4:
       display();
break;
 case 5:
       exit(1);
 default:
       printf("This choice is not present SORRY!!");
     }
}
getch();
}
int full()
{
if(top==MAX-1)
return 1;
else
return  0;
}
int empty()
{
if(top==-1)
return 1;
else
return 0;
}
void push(int item)
{
if(full())
{
printf("its overflow");
exit(1);
}
else
{
top++;
stack_arr[top]=item;
}
}
int pop()
{
int item;
if(empty())
{
printf("its overflow");
exit (1);
}
else
{
item=stack_arr[top];
top--;
return(item);
}
}
int peek()
{
if(empty())
{
printf("its underflow");
exit(1);
}
else
{
return(stack_arr[top]);
}
}
void display()
{
int i;
if(empty())
{
  printf("stack is empty");
  exit(1);
    }
else
{
printf("items are : ");
for(i=top;i>=0;i--)
{
printf("%d  ",stack_arr[i]);
}
printf("\n");
}
}