Datastructure PRGM
Datastructure PRGM
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:
#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
1.PUSH
2.POP
3.TRAVERSE
4.EXIT
stack overflow
5 deleted
4 deleted
2
1
3 deleted
2 deleted
1 deleted
stack underflow
#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:
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:
(a+b)*(c+d)
ab+cd+*
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
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
Queue:
1 2 3 4
1.Enqueue
2.Dequeue
3.Display
4.Exit
2.Dequeue
3.Display
4.Exit
Queue:
2 3 4
1.Enqueue
2.Dequeue
3.Display
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:
case 2:
dequeue();
break;
case 3:
display();
}
}
return 0;
}
Output:
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
8
1.Enqueue
2.Dequeue
3.Display
4.Exit
Queue:
2 4 6 8
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
Queue:
4 6 8
1.Enqueue
2.Dequeue
3.Display
4.Exit
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
}
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:
7.Search
8.display
9.Exit
Enter value
Node inserted
7.Search
8.display
9.Exit
2
Enter value?
Node inserted
7.Search
8.display
9.Exit
Node inserted
7.Search
8.display
9.Exit
8
printing values . . . . .
7.Search
8.display
9.Exit
Enter value?
123
Node inserted
8.display
9.Exit
Enter value
1234
Node inserted
7.Search
8.display
9.Exit
8.display
9.Exit
7.Search
8.display
9.Exit
Deleted node 2
7.Search
8.display
9.Exit
printing values . . . . .
7.Search
8.display
9.Exit
7.Search
8.display
9.Exit
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
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
Item pushed
1.Push
2.Pop
3.traverse
4.Exit
1.Push
2.Pop
3.traverse
4.Exit
Item popped
1.Push
2.Pop
3.traverse
4.Exit
1.Push
2.Pop
3.traverse
4.Exit
Exiting
Algorithm:
Step 1: Allocate the space for the new node PTR
Step 4: END
Algorithm:
Step 1: IF FRONT = NULL
Write " Underflow "Go to Step 5
[END OF IF]
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;
Output:
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter value:
12
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
Enter value:
90
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
12
90
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit
printing values
90
**********Main Menu**********
==============================
1.insert
2.Delete
3.Display
4.Exit