1.
1 Polynomial Addition
#include<stdio.h>
void main()
{
int d1,d2,l,i;
printf("enter the higest degree of the first polynomial:");
scanf("%d",&d1);
int A[d1+1];
printf("\nenter the coefficient in ascending order of
exponents:\n");
for(i=0;i<=d1;i++)
{
printf("enter the coefficient of X^%d:",i);
scanf("%d",&A[i]);
}
printf("enter the higest degree of the second polynomial:");
scanf("%d",&d2);
int B[d2+1];
printf("\nenter the coefficient in ascending order of
exponents:\n");
for(i=0;i<=d2;i++)
{
printf("enter the coefficient of X^%d:",i);
scanf("%d",&B[i]);
}
if(d1>d2)
l=d1;
else
l=d2;
int sum[l+1];
if(d1>d2)
B[d1]=0;
else
if(d2>d1)
A[d2]=0;
for(i=0;i<=l;i++)
sum[i]=A[i];
for(i=0;i<=l;i++)
sum[i]+=B[i];
printf("\nthe sum is: ");
for(i=0;i<=l;i++)
{
printf("%d",sum[i]);
if(i!=0)
printf("X^%d",i);
if(i!=l)
printf("+");
}
}
output:
enter the higest degree of the first polynomial:3
enter the coefficient in ascending order of exponents:
enter the coefficient of X^0:5
enter the coefficient of X^1:2
enter the coefficient of X^2:10
enter the coefficient of X^3:4
enter the higest degree of the second polynomial:4
enter the coefficient in ascending order of exponents:
enter the coefficient of X^0:1
enter the coefficient of X^1:2
enter the coefficient of X^2:3
enter the coefficient of X^3:4
enter the coefficient of X^4:4 5
the sum is: 6+4X^1+13X^2+8X^3+5X^4
2. Stack Operation Using Array
#include<stdio.h>
#include<stdlib.h>
int a[50],top=-1,n,i;
void push()
{
if(top==n-1)
printf("stack overflow\n\n");
else
{
printf("Enter the item to be insert:");
scanf("%d",&a[++top]);
}
}
void pop()
{
if(top==-1)
printf("stack underflow\n\n");
else
{
printf("%d deleted\n",a[top--]);
}
}
void traverse()
{
if(top==-1)
printf("stack is empty\n\n");
else
{ printf("the elements in stack:\n\n");
for(i=top;i>-1;i--)
{
printf("%d\n",a[i]);
}
}
}
void main()
{
int c;
printf("enter the stack size:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.TRAVERSE\n\t
4.EXIT\n");
do
{
printf("enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:push();
break;
case 2:pop();
break;
case 3:traverse();
break;
case 4:exit(0);
break;
default:printf("invalid input\n");
}
}
while(c!=4);
}
output:
enter the stack size:5
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.TRAVERSE
4.EXIT
enter your choice:1
Enter the item to be insert:1
enter your choice:1
Enter the item to be insert:2
enter your choice:1
Enter the item to be insert:3
enter your choice:1
Enter the item to be insert:4
enter your choice:1
Enter the item to be insert:5
enter your choice:1
stack overflow
enter your choice:2
5 deleted
enter your choice:2
4 deleted
enter your choice:3
the elements in stack:
2
1
enter your choice:2
3 deleted
enter your choice:2
2 deleted
enter your choice:2
1 deleted
enter your choice:2
stack underflow
enter your choice:4
1.3. Evaluation of Postfix Expression
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int top=-1;
float stack[50];
void push(float s)
{
if(top>=49)
printf("stack is full");
else
{
top++;
stack[top]=s;
}
}
float pop()
{
if(top==-1)
printf("stack is empty");
else
return(stack[top--]);
}
float eval(char postfix[50],float value[50])
{
char symbol;
int index=0;
float op1,op2,total,d;
while(postfix[index]!='\0')
{
symbol=postfix[index];
if(symbol!='+'&&symbol!='-
'&&symbol!='*'&&symbol!='/'&&symbol!='^')
{
push(value[index]);
}
else
{
op2=pop();
op1=pop();
switch(symbol)
{
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
}
}
index++;
}
d=pop();
return d;
}
void main()
{
char postfix[50],symbol;
float result,value[50];
int i=0;
printf("enter the postfix expression");
scanf("%s",postfix);
while(postfix[i]!='\0')
{
symbol=postfix[i];
if(symbol!='+'&&symbol!='-
'&&symbol!='*'&&symbol!='/'&&symbol!='^')
{
printf("enter the value for %c",postfix[i]);
scanf("%f",&value[i]);
}
i++;
}
result=eval(postfix,value);
printf("%f",result);
}
output:
enter the postfix expressionab+cd+*
enter the value for a1
enter the value for b2
enter the value for c3
enter the value for d4
21.000000
1.4. Converstion
#include<stdio.h>
#include<string.h>
int top=-1;
char stack[50];
char infix[25];
void push(char s)
{
if(top>=49)
printf("stack is full");
else
{
top++;
stack[top]=s;
}
}
char pop()
{
if(top==-1)
printf("stack is empty");
else
{
return(stack[top--]);
}
}
int pre(char ch)
{
if(ch=='^')
return(5);
else if(ch=='*'||ch=='/')
return(4);
else if(ch=='+'||ch=='-')
return(3);
else
return(2);
}
void in_to_post(char infix[])
{
char postfix[50];
char symbol,index=0,pos=0,temp;
int len;
len=strlen(infix);
push('(');
while(index<len)
{
symbol=infix[index];
switch(symbol)
{
case '(':push(symbol);
break;
case ')':temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(pre(stack[top])>=pre(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default:postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
printf("%s",postfix);
void main()
{
printf("enter the expression\n");
scanf("%s",infix);
in_to_post(infix);
output:
enter the expression
(a+b)*(c+d)
ab+cd+*
1.5 queue using array
Algorithm for Enqueue
Step1: IF REAR = SIZE - 1
Write OVERFLOW
Go to step
[END OF IF]
Step2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step3: Set QUEUE[REAR] = ITEM
Step4: EXIT
Algorithm for Dequeue
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Program:
#include <stdio.h>
#include<stdlib.h>
# define SIZE 100
void enqueue();
void dequeue();
void Display();
int q[SIZE];
int Rear = - 1;
int Front = - 1;
int main()
{
int ch;
while (1)
{
printf("1.Enqueue \n");
printf("2.Dequeue \n");
printf("3.Display \n");
printf("4.Exit\n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
Display();
break;
case 4:
exit(0);
default:
printf("Incorrect choice \n");
}
}
}
void enqueue()
{
int insert_item;
if (Rear == SIZE - 1)
printf("Overflow \n");
else
{
if (Front == - 1)
Front = 0;
printf("Element to be inserted in the Queue:\n ");
scanf("%d", &insert_item);
Rear = Rear + 1;
q[Rear] = insert_item;
}
}
void dequeue()
{
if (Front == - 1 || Front > Rear)
{
printf("Underflow \n");
return ;
}
else
{
printf("Element deleted from the Queue: %d\n",
q[Front]);
Front = Front + 1;
}
}
void Display()
{
if (Front == - 1)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf("%d ", q[i]);
printf("\n");
}
}
Output:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
2
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
Queue:
1 2 3 4
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 2
Element deleted from the Queue: 1
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
Queue:
2 3 4
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 4
1.6 circular queue
Algorithm for enqueue:
Step 1: IF (REAR+1)%MAX = FRONT
Write " OVERFLOW "
Goto step 4
[End OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: EXIT
Algorithm for dequeue:
Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
Step 2: SET VAL = QUEUE[FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
Step 4: EXIT
Program:
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue()
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
void dequeue()
{
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int ch=1,m;
while(ch<4 && ch!=0)
{
printf("\n 1: enqueue");
printf("\n 2: dequeue");
printf("\n 3: Display ");
printf("\nEnter your choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &m);
enqueue(m);
break;
case 2:
dequeue();
break;
case 3:
display();
}
}
return 0;
}
Output:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Element to be inserted in the Queue:
8
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
Queue:
2 4 6 8
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 2
Element deleted from the Queue: 2
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
Queue:
4 6 8
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 4
2.1 singly linked list operations
Algorithm for beginsert()
Step 1: IF PTR = NULL
Write Out of memory ,go to Step 5
[END OF IF]
Step 2: SET PTR →DATA = ITEM
Step 3: SET PTR→ NEXT = HEAD
Step 4: SET HEAD= PTR
Step 5: EXIT
Algorithm for endinsert()
Step 1: IF PTR= NULL
Write Out of Memory,go to Step 8
[END OF IF]
Step 2: SET PTR - > DATA= ITEM
Step 3: SET PTR - > NEXT = NULL
Step 4: SET TEMP = HEAD
Step 5: Repeat Step 6 while TEMP - > NEXT != NULL
Step 6: SET TEMP= TEMP- > NEXT
[END OF LOOP]
Step 7: SET TEMP- > NEXT = PTR
Step 8: EXIT
Algorithm for specinsert()
Step 1: IF PTR= NULL
WRITE Out of Memory,go to STEP 10
[ END OF IF]
Step 2: PTR->DATA = ITEM
Step 3: SET TEMP = HEAD
Step 4: SET I = 0
Step 5: REPEAT STEP 5 AND 6 UNTIL Ioc-1 && TEMP!=NULL
Step 6: TEMP = TEMP→ NEXT
Step 7: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT",go to STEP 10
END OF IF
END OF LOOP
Step 8: PTR→ NEXT = TEMP→ NEXT
Step 9: TEMP → NEXT = PTR
Step 10:EXIT
Algorithm for begin_delete()
Step 1: IF HEAD = NULL
Write UNDERFLOW,go to Step 5
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: SET HEAD = PTR-> NEXT
Step 4: FREE PTR
Step 5: EXIT
Algorithm for end_delete()
Step 1: IF HEAD= NULL
Write UNDERFLOW,go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
Step 4: SET PTR1 = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PTR1 -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Algorithm for spec_delete()
Step 1: IF HEAD= NULL
WRITE UNDERFLOW,go to STEP 11
END OF IF
Step 2: SET PTR = HEAD
Step 3: SET I = 0
Step 4: REPEAT STEP 5 TO 8 UNTIL LOC -1 AND PTR!=NULL
Step 5: PTR1= PTR
Step 6: PTR = PTR → NEXT
Step 7: IF PTR = NULL
WRITE "DESIRED NODE NOT PRESENT",go to STEP 11
[END OF IF]
Step 8: I = I+1
[END OF LOOP]
Step 9: PTR 1→ NEXT = PTR → NEXT
Step 10: FREE PTR
Step 11: EXIT
Algorithm for search()
Step 1 : SET PTR=HEAD
Step 2 : SET I=0
Step 3 : IF PTR= NULL
WRITE “EMPTY LIST” GO TO Step 8
[END OF IF]
Step 4 : REPEAT STEP 5 TO STEP 7 UNTIL PTR!=NULL
Step 5 : IF PTR ->DATA=ITEM
WRITE I + 1
[END OF IF]
Step 6 : I=I+1
Step 7 : PTR=PTR ->NEXT
[END OF LOOP]
Step 8 : EXIT
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void endinsert ();
void specinsert();
void begin_delete();
void end_delete();
void spec_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n1.Insert at the begining\n2.Insert at the
end\n3.Insert at specified position\n4.Delete from
Beginning\n5.Delete from end\n6.Delete from specified
position\n7.Search\n8.display\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
endinsert();
break;
case 3:
specinsert();
break;
case 4:
begin_delete();
break;
case 5:
end_delete();
break;
case 6:
spec_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("enter valid choice:");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
}
void endinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void specinsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to
insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void end_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void spec_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you
want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Output:
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Enter value
Node inserted
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
2
Enter value?
Node inserted
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Enter element value1
Enter the location after which you want to insert 1
Node inserted
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
8
printing values . . . . .
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Enter value?
123
Node inserted
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Enter value
1234
Node inserted
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Node deleted from the begining ...
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Deleted Node from the last ...
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Enter the location of the node after which you want to
perform deletion
Deleted node 2
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
printing values . . . . .
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
Enter item which you want to search?
item found at location 1 item found at location 2
1.Insert at the begining
2.Insert at the end
3.Insert at specified position
4.Delete from Beginning
5.Delete from end
6.Delete from specified position
7.Search
8.display
9.Exit
Enter your choice?
EXIT
2.2 Linked Stack
Algorithm for push()
Step 1: if HEAD= NULL
Step 2: PTR -> next = NULL
ELSE
Step 3: PTR -> next = HEAD
Step 4: Exit
Algorithm for pop()
Step 1: if HEAD == NULL
Step 2: print "EMPTY STACK"
ELSE
Step 3: create a temporary node, PTR= HEAD
Step 4: print HEAD -> VAL
Step 5: HEAD = HEAD -> next
Step 6: FREE PTR
Step 7: EXIT
Program:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void traverse();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int ch=0;
printf("\nMenu\n");
printf("\n-----\n");
while(ch != 4)
{
printf("\n1.Push\n2.Pop\n3.traverse\n4.Exit");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
traverse();
break;
}
case 4:
{
printf("Exiting");
break;
}
default:
{
printf("Enter valid choice: ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct
node));
if(ptr == NULL)
{
printf("Out of memory space\n");
}
else
{
printf("\nEnter the value for the node:");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("\n List is empty\n");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void traverse()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Output:
Menu
-----
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :1
Enter the value for the node:1
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :1
Enter the value for the node:2
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :1
Enter the value for the node:3
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :1
Enter the value for the node:4
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :3
Printing Stack elements
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :2
Item popped
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :3
Printing Stack elements
1.Push
2.Pop
3.traverse
4.Exit
Enter your choice :4
Exiting
2.3 Linked Queue
Algorithm:
Step 1: Allocate the space for the new node PTR
Step 2: SET PTR -> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
Step 4: END
Algorithm:
Step 1: IF FRONT = NULL
Write " Underflow "Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n**************Main Menu**************\n");
printf("\n==============================\n");
printf("\n1.insert\n2.Delete \n3.Diplay \n4.Exit\n");
printf("\nEnter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice:\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value:\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values \n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
Output:
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter your choice :1
Enter value:
12
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter your choice :1
Enter value:
90
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter your choice :3
printing values
12
90
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter your choice :2
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter your choice :3
printing values
90
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter your choice :4