Home >> Sem 2 >> Programming Skills II(DS)
GUJARAT TECHNOLOGICAL UNIVERSITY
Subject Name : Programming Skills II(DS)
Subject Code : 620002
GUJARAT TECHNOLOGICAL UNIVERSITY
Subject Name : Programming Skills II(DS)
Subject Code : 620002
Last Update:03 / June / 2010
1. Write a program to perform the following operations on a stack...Shared By :Mohsin Baloch(SVICS kadi) Show/Hide Program
/*Write a program to perform the following operations on a stack.
(Implement the stack using array)*/
/*
i) PUSH
ii) POP
iii) ISEMPTY
iv) ISFULL
v) PEEP
*/
#include <conio.h>
#define N 5
int TOP=0;
void isempty()
{
if(TOP == 0)
{
printf("\nStack is empty");
return;
}
printf("\nStack is not empty");
}
void isfull()
{
if(TOP == N)
{
printf("\nStack is full");
return;
}
printf("\nStack is not full");
}
void push(int a[])
{
int x;
if(TOP == N)
{
isfull();
return;
}
else
{
printf("Enter an element u want to insert=>");
scanf("%d",&x);
TOP++;
a[TOP]=x;
printf("\nElement inserted");
}
}
void pop(int a[])
{
if(TOP == 0)
{
isempty();
return;
}
else
{
int x=a[TOP];
TOP--;
printf("\nElement Deleted=> %d",x);
}
}
void display(int b[])
{
int i;
if(TOP == 0)
{
isempty();
}
else
{
printf("\nElement of stack is underbelow\n\n");
for(i=TOP;i>0;i--)
{
printf("%5d",b[i]);
}
printf("\n");
}
}
void peep(int a[])
{
int i;
if(TOP==0)
{
isempty();
return;
}
else
{
display(a);
}
printf("\nEnter position u want to search=");
scanf("%d",&i);
if((TOP-i+1) <= 0)
{
printf("\nElement not found.");
return;
}
else
{
printf("\nIth element is= %d",a[TOP-i+1]);
}
}
void main()
{
int a[N];
int choice;
clrscr();
do
{
clrscr();
printf("\n===========");
printf("\n1..PUSH.");
printf("\n2..POP.");
printf("\n3..ISEMPTY.");
printf("\n4..ISFULL.");
printf("\n5..PEEP.");
printf("\n6..DISPLAY.");
printf("\n7..EXIT.");
printf("\n===========");
printf("\nEnter ur choice to do operation=>");
scanf("%d",&choice);
switch(choice)
{
case 1: push(a);
break;
case 2: pop(a);
break;
case 3: isempty();
break;
case 4: isfull();
break;
case 5: peep(a);
break;
case 6: display(a);
break;
case 7: return;
default: printf("\nInvalid choice");
}
getch();
}while(choice);
}
//1.2
/*Write a program to perform the following operations on a stack.
(Implement the stack using link list)*/
/*
i) PUSH
ii) POP
iii) ISEMPTY
iv) ISFULL
v) PEEP
*/
#include <stdio.h>
int max;
static int count=0;
struct node
{
int data;
struct node *link;
}*start;
void infront()
{
if(count >= max)
{
printf("\nStack is overflow.");
return;
}
else
{
struct node *q;
int n;
printf("Enter element= ");
scanf("%d",&n);
q=(struct node *)malloc(sizeof(struct node));
q->data=n;
q->link=start;
start=q;
count++;
}
}
void isfull()
{
if(count >=max)
{
printf("\nStack is overflow.");
return;
}
else
printf("\nStack is not full.");
}
void delfront()
{
if(start==NULL)
{
printf("\nStack is empty.");
return;
}
else
{
struct node *d=start;
start=start->link;
free(d);
}
}
void isempty()
{
if(start == NULL)
{
printf("\nStack is Empty.");
}
else
printf("\nStack is not empty.");
}
void peep()
{
if(start==NULL)
{
printf("\nStack is empty.");
return;
}
else
{
int i,j;
int flag=0;
struct node *temp;
temp=start;
printf("Enter position u want to peep element= ");
scanf("%d",&i);
for(j=1;j<=count;j++)
{
if(i==j)
{
printf("\nElement is= %d.",temp->data);
flag=1;
return;
}
temp=temp->link;
}
if(flag==0)
printf("\nElement not found.");
}
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("\nStack is empty.");
return;
}
q=start;
printf("\nYour Stack Is As Follow:\n");
while(q!=NULL)
{
printf("%d",q->data);
printf("\n");
q=q->link;
}
}
void main()
{
int a;
struct node s;
clrscr();
printf("\nEnter Howmany elements u want to store in stack= ");
scanf("%d",&max);
start=NULL;
while(1)
{
clrscr();
printf("\nPress 1 For PUSH");
printf("\nPress 2 For POP");
printf("\nPress 3 For PEEP");
printf("\nPress 4 For ISEMPTY");
printf("\nPress 5 For ISFULL");
printf("\nPress 6 For Display");
printf("\nPress 7 For Exit");
printf("\nEnter The Choice:");
scanf("%d",&a);
switch(a)
{
case 1:
infront();
getch();
break;
case 2:
delfront();
getch();
break;
case 3:
peep();
getch();
break;
case 4:
isempty();
getch();
break;
case 5:
isfull();
getch();
break;
case 6:
display();
getch();
break;
case 7:
return;
default:
printf("\nWrong Choice.");
}
}
}
/* w.a.p. to perform the following operations on a stack
(implement the stack using array)
PUSH
POP
iSEMPTY
ISFULL
PEEP
*/
void push();
void pop();
int isempty();
int isfull();
void peep();
void display();
int top=0,st[5];
void main()
{
int c,n,p,chk=0;
clrscr();
do
{
clrscr();
printf("\n1. For PUSH\n2. For POP\n3. For IsEmpty\n4. For IsFull\n5. For PeeP\n6. For Display\n7. For Exit");
printf("\nEnter Your Choice:");
scanf("%d",&c);
switch(c)
{
case 1:
chk=isfull();
if(chk!=1)
{
printf("Enter The Element:");
scanf("%d",&n);
push(n);
}
break;
case 2:
chk=isempty();
if(chk!=1)
{
pop();
}
break;
case 3:
chk=isempty();
if(chk!=1)
printf("\nStack is not Empty");
getch();
break;
case 4:
chk=isfull();
if(chk!=1)
printf("\nStack is not Full");
getch();
break;
case 5:
chk=isempty();
if(chk!=1)
{
peep();
}
break;
case 6:
chk=isempty();
if(chk!=1)
{
display();
}
break;
}
getch();
}while(c!=7);
}
int isempty()
{
if(top==0)
{
printf("\nStack is Empty");
return 1;
}
else
return 0;
}
int isfull()
{
if(top>=5)
{
printf("\nStack is Full");
return 1;
}
else
return 0;
}
void push(int n)
{
top++;
st[top]=n;
}
void pop()
{
printf("\n'%d' is Deleted From Stack\n",st[top]);
top--;
}
void peep()
{
int item,p;
printf("\n\nEnter position:");
scanf("%d",&p);
if(top-p+1<=0)
{
printf("\nPosition Not Found");
return;
}
printf("\nPosition Element is: %d ",st[top-p+1]);
}
void display()
{
int i;
printf("\n");
for(i=top; i>=1; i--)
printf("\n\t%d",st[i]);
printf("\n");
}
//1.2
//Menu-Driven : PUSH,POP,ISEMPTY,ISFULL,PEEP
#include<stdio.h>
int i=0;
struct stack
{
int n;
struct stack *next;
}*start=NULL,*link=NULL,*temp=NULL;
void main()
{
int ch,j;
clrscr();
do
{
do
{
clrscr();
printf("\n1. PUSH\n2. POP\n3. ISEMPTY\n4. ISFULL\n5. PEEP\n6. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>6);
switch(ch)
{ case 1:
if(i>4)
printf("Stack is Full");
else
{
temp=(struct stack *) malloc(sizeof(struct stack));
if(temp==NULL)
printf("\nNo Sufficient Memory");
else
{
printf("Enter no.: ");
scanf("%d",&temp->n);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else if(start->next==NULL)
{
start->next=temp;
}
else
{
link=start->next;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
printf("\nNo. is Inserted");
i++;
}
}
getch();
break;
case 2:
if(start==NULL)
printf("Stack is Empty");
else
{
link=start;
while(link->next!=NULL)
{
link=link->next;
}
link=NULL;
i--;
printf("No. is Deleted");
}
getch();
break;
case 3:
if(start==NULL)
printf("\nTRUE");
else
printf("\nFALSE");
getch();
break;
case 4:
if(i==5)
printf("\nTRUE");
else
printf("\nFALSE");
getch();
break;
case 5:
link=start;
while(link!=NULL)
{
printf(" %d",link->n);
link=link->next;
}
getch();
}
}while(ch!=6);
}
//Menu-Driven : PUSH,POP,ISEMPTY,ISFULL,PEEP
#include<stdio.h>
void main()
{
int a[5],i=0,ch,j;
clrscr();
do
{
do
{
clrscr();
printf("\n1. PUSH\n2. POP\n3. ISEMPTY\n4. ISFULL\n5. PEEP\n6. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>6);
switch(ch)
{ case 1:
if(i>4)
printf("Stack is Full");
else
{
printf("Enter no.: ");
scanf("%d",&a[i]);
printf("\nNo. is Inserted");
i++;
}
getch();
break;
case 2:
if(i<1)
printf("Stack is Empty");
else
{
i--;
printf("No. is Deleted");
}
getch();
break;
case 3:
if(i==0)
printf("\nTRUE");
else
printf("\nFALSE");
getch();
break;
case 4:
if(i==5)
printf("\nTRUE");
else
printf("\nFALSE");
getch();
break;
case 5:
for(j=0;j<i;j++)
printf(" %d",a[j]);
getch();
}
}while(ch!=6);
}
//---------------OR-----------------
//Menu-Driven : PUSH,POP,ISEMPTY,ISFULL,PEEP
#include<stdio.h>
struct stack
{
int n;
struct stack *next;
}*start=NULL,*link=NULL,*temp=NULL;
void main()
{
int i=0,ch,j;
clrscr();
do
{
do
{
clrscr();
printf("\n1. PUSH\n2. POP\n3. ISEMPTY\n4. ISFULL\n5. PEEP\n6. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>6);
switch(ch)
{ case 1:
if(i>4)
printf("Stack is Full");
else
{
temp=(struct stack *) malloc(sizeof(struct stack));
if(temp==NULL)
printf("\nNo Sufficient Memory");
else
{
printf("Enter no.: ");
scanf("%d",&temp->n);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else if(start->next==NULL)
{
start->next=temp;
}
else
{
link=start->next;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
printf("\nNo. is Inserted");
i++;
}
}
getch();
break;
case 2:
if(start==NULL)
printf("Stack is Empty");
else
{
link=start;
while(link->next!=NULL)
{
link=link->next;
}
link=NULL;
i--;
printf("No. is Deleted");
}
getch();
break;
case 3:
if(start==NULL)
printf("\nTRUE");
else
printf("\nFALSE");
getch();
break;
case 4:
if(i==5)
printf("\nTRUE");
else
printf("\nFALSE");
getch();
break;
case 5:
link=start;
while(link!=NULL)
{
printf(" %d",link->n);
link=link->next;
}
getch();
}
}while(ch!=6);
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
2. Write a program to convert an infix arithmetic expression (parenthesize...
/*Write a program to convert an infix arithmetic expression (parenthesize /
unparenthesized) into postfix notation.*/
void push();
int top,p,q,j,i;
char stack[50],postfix[50],infix[50];
void main()
{
clrscr();
printf("Enter an infix Expression : ");
scanf("%s",infix);
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]=='(')
{
stack[top] = infix[i];
top++;
}
else if( infix[i]==')')
{
while(stack[top-1]!='(')
{
postfix[j] = stack[top-1];
j++;
top--;
}
top--;
}
else if(infix[i]=='+' || infix[i]=='-')
{
p = 2;
push();
stack[top] = infix[i];
top++;
}
else if(infix[i]=='*' || infix[i]=='/')
{
p = 3;
push();
stack[top] = infix[i];
top++;
}
else if(infix[i]=='^' || infix[i]=='$')
{
p = 4;
push();
stack[top] = infix[i];
top++;
}
else
{
postfix[j]=infix[i];
j++;
}
}
while(stack[top-1]!='\0')
{
postfix[j] = stack[top-1];
j++;
top--;
}
postfix[j] = '\0';
printf("\n\n\tPostfix : %s",postfix);
getch();
}
void push()
{
sadik:
if(stack[top-1]=='+' || stack[top-1]=='-')
q = 2;
else if(stack[top-1]=='*' || stack[top-1]=='/')
q = 3;
else if(stack[top-1]=='^' || stack[top-1]=='$')
q = 4;
else
q=0;
if(q>=p)
{
postfix[j] = stack[top-1];
j++;
top--;
goto sadik;
}
}
/*
Convert given infix expression to postfix expression */
void push();
int top,p,q,j,i;
char stack[50],postfix[50],infix[50];
void main()
{
clrscr();
printf("Enter an infix Expression : ");
scanf("%s",infix);
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]=='(')
{
stack[top] = infix[i];
top++;
}
else if( infix[i]==')')
{
while(stack[top-1]!='(')
{
postfix[j] = stack[top-1];
j++;
top--;
}
top--;
}
else if(infix[i]=='+' || infix[i]=='-')
{
p = 2;
push();
stack[top] = infix[i];
top++;
}
else if(infix[i]=='*' || infix[i]=='/')
{
p = 3;
push();
stack[top] = infix[i];
top++;
}
else if(infix[i]=='^' || infix[i]=='$')
{
p = 4;
push();
stack[top] = infix[i];
top++;
}
else
{
postfix[j]=infix[i];
j++;
}
}
while(stack[top-1]!='\0')
{
postfix[j] = stack[top-1];
j++;
top--;
}
postfix[j] = '\0';
printf("\n\n\tPostfix : %s",postfix);
getch();
}
void push()
{
a:
if(stack[top-1]=='+' || stack[top-1]=='-')
q = 2;
else if(stack[top-1]=='*' || stack[top-1]=='/')
q = 3;
else if(stack[top-1]=='^' || stack[top-1]=='$')
q = 4;
else
q=0;
if(q>=p)
{
postfix[j] = stack[top-1];
j++;
top--;
goto a;
}
}
//Infix To Postfix
#include<iostream.h>
#include<conio.h>
int n,top=0;
char a[20];
class abc
{
int i,j;
public :
int precedence(char c)
{
if(c=='^')
{
return 3;
}
else if(c=='*'||c=='/')
{
return 2;
}
else if(c=='+'||c=='-')
{
return 1;
}
else
{
return 0;
}
}
void push(char d)
{
if(top>=20)
{
cout<<"stack overflow";
}
else
{
top++;
a[top]=d;
}
}
int pop()
{
if(top==0)
{
cout<<"stack is overflow";
}
else
{
int g;
g=top;
top--;
return g;
}
}
};
void main()
{
char b[50];
int k,l,m,p;
abc f;
clrscr();
cout<<"enter the equivation:";
cin>>b;
for(k=0;b[k]!='\0';k++)
{
if(b[k]=='('||b[k]=='+'||b[k]=='-'||b[k]=='/'||b[k]=='*'||b[k]=='^')
{
if(b[k]=='(')
{
f.push(b[k]);
}
else
{
if(a[top]=='(')
{
f.push(b[k]);
}
else
{
l=f.precedence(b[k]);
m=f.precedence(a[top]);
if(l>m)
{
f.push(b[k]);
}
else if(l<=m)
{
p=f.pop();
cout<<a[p];
f.push(b[k]);
}
}
}
}
else if(b[k]==')')
{
for(;a[top]!='(';)
{
p=f.pop();
cout<<a[p];
}
p=f.pop();
}
else
{
cout<<b[k];
}
}
for(;top!=0;)
{
p=f.pop();
cout<<a[p];
}
getch();
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Program to Infix to Postfix
#include<stdio.h>
#include<conio.h>
int top=-1;
int len=0;
void push(char stack[],char s)
{
stack[top]=s;
}
void pop(char stack[],char post[])
{
post[len]=stack[top];
}
void main()
{
int i,j,k,b;
char exp[100]="",stack[100]="",post[100]="",op[5]={'+','-','*','/','^'},br[6]={'(','[','{',')',']','}'};
clrscr();
printf("Enter Infix Expression : ");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
for(j=0;j<5;j+=2)
{
if(exp[i]==op[j] || exp[i]==op[j+1])
{
if(top>=0)
{
while(top>=0)
{
for(k=j+1;k<5;k++)
{
if(stack[top]==op[k])
{
pop(stack,post);
len++;
top--;
break;
}
}
if(k==5)
break;
}
}
if(k==5 && (stack[top]==op[j] || stack[top]==op[j+1]))
{
pop(stack,post);
len++;
push(stack,exp[i]);
break;
}
else if(k==5 || top==-1)
{
++top;
push(stack,exp[i]);
break;
}
}
else if((exp[i]>=65 && exp[i]<=90)||(exp[i]>=97&&exp[i]<=122))
{
post[len++]=exp[i];
break;
}
else if(exp[i]==40 || exp[i]==41 || exp[i]==91 || exp[i]==93 || exp[i]==123 || exp[i]==125)
{
for(b=0;b<6;b++)
{
if(exp[i]==br[b] && b<3)
{
++top;
push(stack,exp[i]);
break;
}
else if(exp[i]==br[b] && b>=3)
{
while(stack[top]!=br[b-3])
{
pop(stack,post);
len++;
push(stack,'\0');
top--;
}
if(stack[top]==br[b-3])
{
if(top>=0)
{
push(stack,'\0');
top--;
break;
}
else
{
printf("Invalid Bracket on position %d",i+1);
getch();
exit(0);
}
}
break;
}
}
break;
}
}
}
while(top>=0)
{
pop(stack,post);
len++;
top--;
}
if(top==-1)
{
post[len]='\0';
}
printf("Postfix : ");
puts(post);
getch();
}
Shared By :Amit Parekh (SRIMCA college-Tarsadi) Show/Hide Program
3. Write a program to evaluate a postfix expression.,..
# include<stdio.h>
# include<string.h>
# include<ctype.h>
# include<stdlib.h>
# define Arnold '\0'
# define Blank ' '
# define Tab '\t'
#define Maxlen 64
static char infix [Maxlen+1], stack[Maxlen], postfix[Maxlen+1];
# define Empty (-1) /* empty stack */
static int top;
/* symbol types */
#define Operator (-10)
# define Operand (-20)
# define LeftParen (-30)
# define RightParen (-40)
static char *symbols = "()+-%*/";
/* Symbol precedence */
# define LeftParenPrec 0 /* ( */
# define AddPrec 1 /* + */
# define SubPrec 1 /* - */
# define MultPrec 2 /* * */
# define DivPrec 2 /* / */
# define RemPrec 2 /* % */
# define None 999 /* all else */
void read_input(void);
void infix_to_postfix(void);
void write_output(void);
void push(char symbol);
char pop (void);
int get_type(char );
int get_prec(char );
int white_space(char );
void full_stack();
void empty_stack();
void main()
{
char ans[2];
do
{
top = Empty; /* reset stack */
read_input();
infix_to_postfix();
write_output();
/* Selection */
printf("\n\n Another (y/n)");
gets(ans);
} while (toupper(ans[0]) == 'Y');
}
/* function infix to postfix conversion */
void infix_to_postfix(void)
{
int i,p, len, type, precedence;
char next ;
/* i for infix, p for postfix */
i = p = 0;
len = strlen(infix);
/* loop through input string */
while(i < len)
{
/* ignore whitepsce in infix expression */
if( !white_space(infix[i]))
{
type = get_type(infix[i]);
switch(type)
{
/* push left paren onto stack */
case LeftParen:
push(infix[i]);
break;
/* pop stack until matching left paren */
case RightParen:
while((next = pop()) != '(')
postfix[p++] = next;
break;
case Operand: postfix[p++] = infix[i];
break;
/* Pop stack until first operator of higher precedence and then stack this operator */
case Operator:
precedence = get_prec(infix[i]);
/* Anything on stack to pop */
while(top > Empty && precedence<= get_prec(stack[top]))
postfix[p++] = pop();
push(infix[i]);
break;
}
}
i++; /* next symbol in infix expression */
}
/* pop any remaining operators */
while(top > Empty )
postfix[p++] = pop();
postfix[p] = Arnold ; /* ensure a string */
}
/* Function to find operator type */
int get_type(char symbol )
{
switch(symbol)
{
case '(' :
return (LeftParen);
case ')':
return (RightParen);
case '+':
case '-':
case '%':
case '*':
case '/':
return (Operator);
default:
return (Operand);
}
}
/* Find precedence of the operator */
int get_prec(char symbol )
{
switch(symbol)
{
case '+':
return (AddPrec);
case '-':
return (SubPrec);
case '*':
return (MultPrec);
case '/':
return (DivPrec);
case '%':
return (RemPrec);
case '(':
return (LeftParenPrec);
default:
return (None);
}
}
/* Function push */
void push(char symbol)
{
/* check for overflow */
if(top > Maxlen)
full_stack();
else
stack[++top] = symbol;
}
/* Function pop */
char pop(void)
{
/* check for underflow */
if (top <= Empty )
empty_stack();
else
return (stack[top--]);
}
/* Exit in case of overflow */
void full_stack(void )
{
printf("\n Full Stsck ... exiting. \n");
exit(1);
}
/* Exit in case of underflow */
void empty_stack(void)
{
printf("\n Empty Stack ... exiting \n");
exit(2);
}
void read_input(void)
{
printf("\n Infix (up to %d chars): ", Maxlen);
gets(infix);
}
/* Output function */
void write_output(void)
{
printf("\n Infix: %s", infix);
printf("\n Postfix: %s \n",postfix);
}
/* function white space checking */
int white_space(char symbol)
{
return( symbol == Blank || symbol == Tab || symbol == Arnold);
}
//Write a program to evaluate a postfix expression.
char postfix[100];
static i,top;
long int stack[20];
void line();
void main()
{
int temp,p;
clrscr();
line();
printf("\n\t\tTO EVALUATE POSTFIX EXPRESSION LIKE : 12,5,+\n");
line();
printf("\nEnter Postfix Expression : ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
if(postfix[i]=='$' || postfix[i]=='^')
{
temp = stack[top-1];
p = stack[top-2];
while(temp>1)
{
stack[top-2] *=p;
temp--;
}
top--;
}
else if(postfix[i] == '+')
{
stack[top-2] += stack[top-1];
top--;
}
else if(postfix[i] == '-')
{
stack[top-2] -= stack[top-1];
top--;
}
else if(postfix[i] == '*')
{
stack[top-2] *= stack[top-1];
top--;
}
else if(postfix[i] == '/')
{
stack[top-2] /= stack[top-1];
top--;
}
else if(postfix[i]!=',')
{
stack[top] = postfix[i]-48;
while(postfix[i+1]!=',')
{
stack[top] *= 10 + postfix[i+1]-48;
i++;
}
top++;
}
}
printf("\nValue of Postfix Expression : %ld",stack[top-1]);
getch();
}
void line()
{
int i;
printf("+");
for(i=1;i<=76;i++)
printf("=");
printf("+");
}
/* Write a Program for to evaluate postfix expression */
char postfix[100];
static i,top;
long int stack[20];
void main()
{
int temp,p;
clrscr();
printf("\n\t\tfor example:12,14,+\n");
printf("\nEnter Postfix Expression : ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
if(postfix[i]=='$' || postfix[i]=='^')
{
temp = stack[top-1];
p = stack[top-2];
while(temp>1)
{
stack[top-2] *=p;
temp--;
}
top--;
}
else if(postfix[i] == '+')
{
stack[top-2] += stack[top-1];
top--;
}
else if(postfix[i] == '-')
{
stack[top-2] -= stack[top-1];
top--;
}
else if(postfix[i] == '*')
{
stack[top-2] *= stack[top-1];
top--;
}
else if(postfix[i] == '/')
{
stack[top-2] /= stack[top-1];
top--;
}
else if(postfix[i]!=',')
{
stack[top] = postfix[i]-48;
while(postfix[i+1]!=',')
{
stack[top] *= 10 + postfix[i+1]-48;
i++;
}
top++;
}
}
printf("\nValue of Postfix Expression : %ld",stack[top-1]);
getch();
}
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
Shared By :your name Show/Hide Program
4. Write a program to perform the following operation on a...
/*Write a program to perform the following operation on a simple queue. (Implement the
queue using array)
(a) Insert an element (b) Remove an element */
#include <conio.h>
static int f=-1;
static int r=-1;
void insertq(int p[],int n)
{
int x;
if(r==(n-1))
{
printf("\nQueue is overflow");
return;
}
printf("Enter element=");
scanf("%d",&x);
r++;
p[r]=x;
if(f==-1)
{
f=0;
}
}
void displayq(int p[])
{
int i;
if(f==(-1))
{
printf("\nQueue is underflow");
return;
}
else
{
i=f;
printf("\nElement of queue are under below");
printf("\n[%d",p[i]);
for(i=f+1;i<=r;i++)
{
printf(",%d",p[i]);
}
printf("]");
}
}
void deleteq(int p[])
{
int y;
if(f==-1)
{
printf("\nQueue is underflow");
return;
}
y=p[f];
printf("\nDeleted element is= %d",y);
if(f==r)
f=r=-1;
else
f++;
}
void main()
{
int *p,ch,n;
clrscr();
printf("Enter how many element u want to enter in queue=");
scanf("%d",&n);
p=(int *) calloc(n,sizeof(int));
do
{
clrscr();
printf("\n1..Insert.");
printf("\n2..Delete.");
printf("\n3..Display.");
printf("\n4..Exit");
printf("\nEnter choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: insertq(p,n);
getch();
break;
case 2: deleteq(p);
getch();
break;
case 3: displayq(p);
getch();
break;
case 4: return;
default: printf("Invalid choice.");
getch();
}
}while(ch);
getch();
}
/* Write a Program for queue that perform insertion and deletion operation */
void insert(int *,int *,int *);
void del(int *,int *,int *);
void disp(int *,int *,int *);
int isempty(int *);
int isfull(int *);
void main()
{
int rear=0,front=0;
int *queue,check;
int choice;
clrscr();
do
{
clrscr();
printf("\n\n1 : Insert an element.");
printf("\n2 : Remove an element.");
printf("\n3 : Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
check = isfull(&rear);
if(check==0)
{
insert(queue,&front,&rear);
disp(queue,&front,&rear);
}
break;
case 2 :
check = isempty(&front);
if(check==0)
{
del(queue,&front,&rear);
disp(queue,&front,&rear);
}
break;
case 3:
exit();
default :
printf("\n\nInvalid Choice");
}
getch();
}while(1);
}
void insert(int *q,int *f,int *r)
{
int item;
if(*f==0)
*f=1;
printf("\nEnter an item : ");
scanf("%d",&item);
*r+=1;
q[*r] = item;
}
//Simple Queue in Array: INSERT, DELETE, VIEW
#include<stdio.h>
void Qinsert(int q[],int *f,int *l,int val)
{
if(*l == 4)
{
printf("\nQueue is Full");
return;
}
else if(*l == -1)
*f = *l = 0;
else
*l = *l + 1;
q[*l]=val;
printf("\nItem is Inserted");
return;
}
int Qdelete(int q[],int *f,int *l)
{
int temp;
if(*f == -1)
{
printf("\nQueue is Empty");
return NULL;
}
else
{
temp=q[*f];
if(*f == *l)
*f=*l=-1;
else
*f = *f + 1;
return temp;
}
}
void main()
{
int q[5],first=-1,last=-1,ch,value;
int i;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
printf("Enter Value: ");
scanf("%d",&value);
Qinsert(q,&first,&last,value);
getch();
break;
case 2:
value=Qdelete(q,&first,&last);
if(value!=NULL)
printf("\nDeleted Item is: %d",value);
getch();
break;
case 3:
printf("\n");
if(first!=-1)
{
for(i=first;i<=last;i++)
printf(" %d",q[i]);
}
getch();
}
}while(ch!=4);
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi)Show/Hide Program
5. Write a program to perform the following operations on a simple queue....
/*Write a program to perform the following operations on a simple queue.
(implement the queue using linked list)
(a) Insert an element (b) Remove an element*/
#include<stdio.h>
struct node
{
int data;
struct node *link;
}*start;
int n;
static int c;
void inend(int i)
{
struct node *tmp,*q;
tmp=(struct node*) malloc(sizeof(struct node));
tmp->data=i;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void delfront()
{
if(start==NULL)
{
printf("\nQueue Is Empty\n");
return;
}
else
{
struct node *d;
d=start;
start=start->link;
free(d);
}
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("\nQueue Is Empty\n");
}
else
{
printf("\nYour Queue Is As Follow:\n");
q=start;
while(q!=NULL)
{
printf("%d ",q->data);
q=q->link;
}
}
}
void main()
{
int a,v,n;
struct node q;
clrscr();
while(1)
{
clrscr();
printf("\n1 For Insertion");
printf("\n2 For Deletion");
printf("\n3 For Display");
printf("\n4 For Exit");
printf("\nEnter The Choice:");
scanf("%d",&a);
switch(a)
{
case 1:
printf("\nEnter The Element:");
scanf("%d",&v);
inend(v);
getch();
break;
case 2:
delfront();
getch();
break;
case 3:
display();
getch();
break;
case 4:
return;
default:
printf("\nWrong Choice\n");
}
}
}
/* Write a Program to perform the following operation queue using linklist. */
/* INSERT, DELETE, ISEMPTY, ISFULL */
struct list
{
int info;
struct list *next;
}*start,*q,*node,*p;
void main()
{
int i;
void insert(struct list *);
void del(struct list *);
int isempty(struct list *);
void disp(struct list *);
int ret,s1,choice;
clrscr();
start = '\0';
do
{
clrscr();
printf("\n");
printf("\n\t1 : Insert element in Queue.\n");
printf("\t2 : Delete element from Queue.\n");
printf("\t3 : Exit from Program.\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert(start);
disp(start);
break;
case 2:
ret = isempty(start);
if(ret != 1)
del(start);
disp(start);
break;
case 3:
exit();
default :
printf("\n\n\t\t>>>>>>>>>> INVALID CHOICE <<<<<<<<<<<\n\n");
}
getch();
}while(s1);
}
int isempty(struct list *first)
{
if(first == '\0')
{
printf("\n\n\t\t>>>>>>>>> QUEUE IS UNDERFLOW <<<<<<<<<<\n\n");
return 1;
}
else
return 0;
}
void insert(struct list *p)
{
int item;
printf("\nEnter an Integer value : ");
scanf("%d",&item);
node = (struct list *)malloc(sizeof(struct list));
node->info = item;
node->next = '\0';
if (start == '\0')
start = node;
else
{
while(p->next != '\0')
p = p->next;
p->next = node;
}
}
void del(struct list *p)
{
int item;
if(p != '\0')
{
printf("\n\n\t\t%d is deleted.\n",p->info);
start=p->next;
return;
}
}
void disp(struct list *p)
{
if(p!='\0')
printf("\n\nQueue Contains : ");
while(p!='\0')
{
printf("%d ",p->info);
p = p->next;
}
}
//Simple Queue using Linked-List: INSERT, DELETE, VIEW
#include<stdio.h>
struct emp
{
int id;
struct emp *next;
}*first=NULL,*last=NULL,*link=NULL,*temp=NULL;
void Qinsert()
{
temp=(struct emp *) malloc(sizeof(struct emp));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
printf("Enter Value: ");
scanf("%d",&temp->id);
temp->next=NULL;
if(first == NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
last=temp;
}
printf("\nItem is Inserted");
}
}
void Qdelete()
{
if(first==NULL)
{
printf("\nQueue is Empty");
}
else
{
printf("\nThe Deleted Item is:");
printf("\n %d",first->id);
link=first;
first=first->next;
free(link);
if(first==NULL)
last=NULL;
}
}
void view()
{
printf("\n");
if(first==NULL)
printf("Queue is Empty");
else
{
link=first;
while(link!=NULL)
{
printf(" %d",link->id);
link=link->next;
}
}
}
void main()
{
int ch,value,i;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
Qinsert();
getch();
break;
case 2:
Qdelete();
getch();
break;
case 3:
view();
getch();
}
}while(ch!=4);
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
6. Write a program to perform the following operation on a circular queue....
/* Write a program to perform the following operation on a circular queue.
(implement the queue using array)
(a) Insert an element (b) Remove an element*/
int max;
static int front,rear,top;
int *queue;
void insert();
void del();
void disp();
void main()
{
int choice;
clrscr();
printf("Howmany size of queue u want to create= ");
scanf("%d",&max);
queue=(int *) malloc(sizeof(int)*max);
do
{
clrscr();
printf("\n\n1 : Insert into Queue.\n");
printf("2 : Delete From Queue.\n");
printf("3 : Exit from Program.\n");
printf("Enter your choice ======> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
disp();
getch();
break;
case 2:
del();
if(front!=0)
disp();
getch();
break;
case 3:
exit();
default:
getch();
printf("\n\n\t\tINVALID CHOICE\n");
}
}while(1);
}
void insert()
{
int item;
if((rear == max && front==1)|| rear+1==front)
{
printf("\n\n\t\tQUEUE IS OVERFLOW");
return;
}
if(front == 0)
{
front = 1;
rear = 1;
}
else if(rear==max)
rear = 1;
else
rear++;
printf("\nEnter an item : ");
scanf("%d",&item);
queue[rear] = item;
}
void del()
{
int item;
if(front == 0)
{
printf("\n\n\t\tQUEUE IS UNDERFLOW");
return;
}
item = queue[front];
if(front==rear)
front = rear = 0;
else if(front == max)
front = 1;
else
front += 1;
printf("\n\n\t\t %d is deleted.\n",item);
}
void disp()
{
int i,j;
printf("\n\nQueue Contains : ");
for(i=front;i<=rear;i++)
printf("%d ",queue[i]);
if(front>rear)
for(i=front;i<=max;i++)
{
printf("%d ",queue[i]);
if(i==max)
for(j=1;j<=rear;j++)
printf("%d ",queue[j]);
}
}
/* W.A.P to perform the following operations on a circlar queue (using Array)
(insert & delete) */
#include <stdio.h>
#include <conio.h>
#define n 5
int front=0, rear=0, cq[n];
void cqinsert(int x)
{
if((rear == n && front==1)|| rear+1==front)
{
printf("\nCircular Queue is Overflow");
return;
}
if (front==0)
{
front=1;
rear=1;
}
else if (rear==n)
rear=1;
else
rear++;
cq[rear]=x;
}
void cqdelete()
{
int y=0;
if(front==0)
{
printf("\nCircular Queue is Underflow");
getch();
return;
}
y=cq[front];
if (front==rear)
front=rear=0;
else if (front==n)
front=1;
else
front=front+1;
printf("\n %d is Deleted From Queue",y);
}
void display()
{
int i=0;
if(front==0)
{
printf("\nCircular Queue is Underflow");
return;
}
printf("\n\n");
for(i=front; i<=rear; i++)
printf("\t%d",cq[i]);
if(front>rear)
{
for(i=front; i<=n; i++)
printf("\t%d",cq[i]);
if(i==n)
{
for(i=0; i<=rear; i++)
printf("\t%d",cq[i]);
}
}
}
void main()
{
int x,c;
clrscr();
do
{
clrscr();
printf("\n1. For Insert\n2. For Delete\n3. For Display\n4. For Exit");
printf("\nEnter Your Choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nEnter An Element:");
scanf("%d",&x);
cqinsert(x);
break;
case 2:
cqdelete();
break;
case 3:
display();
break;
}
getch();
}while(c!=4);
}
//Priority Queue using Circular Queue in Array: INSERT, DELETE, VIEW
#include<stdio.h>
void PQinsert(int q[],int *f,int *l,int val,int n)
{
if(*l - *f == n || *f - *l == 1)
{
printf("\nQueue is Full");
return;
}
else if(*f == -1)
*f = *l = 0;
else if(*l == n)
*l=0;
else
*l = *l + 1;
q[*l]=val;
printf("\nItem is Inserted");
return;
}
int PQdelete(int q[],int *f,int *l)
{
int temp;
temp=q[*f];
if(*f == *l)
*f=*l=-1;
else if(*f == 4)
*f=0;
else
*f = *f + 1;
return temp;
}
void main()
{
int A[3],B[4],C[3],af=-1,al=-1,bf=-1,bl=-1,cf=-1,cl=-1,ch,value,p;
int i,f;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
printf("Enter Value: ");
scanf("%d",&value);
do
{
printf("Enter Priority: ");
scanf("%d",&p);
}while(p<1 || p>3);
switch(p)
{
case 1:
PQinsert(A,&af,&al,value,2);
break;
case 2:
PQinsert(B,&bf,&bl,value,3);
break;
case 3:
PQinsert(C,&cf,&cl,value,2);
}
getch();
break;
case 2:
f=10;
if(af!=-1)
value=PQdelete(A,&af,&al);
else if(bf!=-1)
value=PQdelete(B,&bf,&bl);
else if(cf!=-1)
value=PQdelete(C,&cf,&cl);
else
{
printf("\nQueue is empty");
f=0;
}
if(f!=0)
printf("\nDeleted Item is: %d",value);
getch();
break;
case 3:
if(af!=-1)
{ printf("\nQueue A:");
if(al<af)
{
for(i=af;i<=2;i++)
printf(" %d",A[i]);
for(i=0;i<=al;i++)
printf(" %d",A[i]);
}
else
{
for(i=af;i<=al;i++)
printf(" %d",A[i]);
}
}
if(bf!=-1)
{ printf("\nQueue B:");
if(bl<bf)
{
for(i=bf;i<=3;i++)
printf(" %d",B[i]);
for(i=0;i<=bl;i++)
printf(" %d",B[i]);
}
else
{
for(i=bf;i<=bl;i++)
printf(" %d",B[i]);
}
}
if(cf!=-1)
{ printf("\nQueue C:");
if(cl<cf)
{
for(i=cf;i<=2;i++)
printf(" %d",C[i]);
for(i=0;i<=cl;i++)
printf(" %d",C[i]);
}
else
{
for(i=cf;i<=cl;i++)
printf(" %d",C[i]);
}
}
getch();
}
}while(ch!=4);
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
7. Write a program to perform the following operations on a priority queue....
#include<iostream>
#include<conio.h>
using namespace std;
//->
struct Node
{
int data,priority;
struct Node *next;
};
class priority_queue
{
private:
Node *front,*rear;
public:
priority_queue()
{
front=rear=NULL;
}
void pqinsert(int x,int p)
{
struct Node *node=new Node;
node->data=x;
node->priority=p;
Node *tmp;
tmp=front;
if(tmp==NULL)
{
node->next=NULL;
front=rear=node;
}
else if(tmp->priority>p)
{
node->next=front;
front=node;
}
else
{
while(tmp->next!=NULL && tmp->next->priority<p)
{
tmp=tmp->next;
}
node->next=tmp->next;
tmp->next=node;
}
if(tmp==rear)
{
rear=tmp->next;
rear->next=NULL;
}
}
int pqdelete()
{
if(front==NULL)
{
cout<<"Queue is empty :";
return 0;
}
else
{
Node *temp;
temp=front;
int y=temp->data;
front=front->next;
delete temp;
return y;
}
}
void print_data()
{
Node *tmp;
tmp=front;
while(tmp!=NULL)
{
cout<<tmp->data<<"->"<<tmp->priority<<"\t";
tmp=tmp->next;
}
}
};
main()
{
priority_queue p1;
int ch,x,p;
do
{
cout<<"\n1.Insert An Elements ";
cout<<"\n2.Delete An Elements ";
cout<<"\n3.Display An Elements ";
cout<<"\n0.Exit From Program";
cout<<"\n\nEnter Your Choice:===============>";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Element to Insert :---->";
cin>>x;
cout<<"Enter Priority to Insert:------>";
cin>>p;
p1.pqinsert(x,p);
break;
case 2:
cout<<"\nDeleted data is :"<< p1.pqdelete();
break;
case 3:
p1.print_data();
break;
}
}while(ch);
}
/* Write a program to perform the following operations on a priority queue.
(a) Insert an element (b) Remove an element */
int max;
void insert(int *,int *,int *,int *);
void del(int *,int *,int *);
void disp(int *,int *,int *,int *);
void main()
{
int choice,f,r,*arr,*p,n;
clrscr();
f=r=0;
printf("\nHowmany value u want to store in queue= ");
scanf("%d",&max);
do
{
clrscr();
printf("\n\n1 : Insert into Queue.");
printf("\n2 : Delete from Queue.");
printf("\n3 : Display from Queue.");
printf("\n4 : Exit from program.");
printf("\nEnter Your choice ====> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert(arr,p,&f,&r);
getch();
break;
case 2:
del(arr,&f,&r);
getch();
break;
case 3:
if(f!=0)
disp(arr,p,&f,&r);
else
printf("\nQueue is underflow.");
getch();
break;
case 4:
return;
default:
printf("\n\nINVALID CHOICE\n");
getch();
}
}while(1);
}
void insert(int *arr,int *p,int *front,int *rear)
{
int item,pr,t1,t2,i,j;
if(*rear == max)
{
printf("\n\nPRIORITY QUEUE IS OVERFLOW.\n");
return;
}
printf("Enter Value : ");
scanf("%d",&item);
printf("Enter Priority : ");
scanf("%d",&pr);
if (*front==0)
*front = *rear = 1;
else
*rear += 1;
arr[*rear] = item;
p[*rear] = pr;
for(i=*front;i<=*rear;i++)
{
for(j=i+1;j<=*rear;j++)
{
if(p[j]<p[i])
{
t1 = arr[i];
t2 = p[i];
arr[i] = arr[j];
p[i] = p[j];
arr[j] = t1;
p[j] = t2;
}
}
}
}
void disp(int *k,int *p,int *f,int *r)
{
int i;
printf("\nQueue Contains :\n");
printf("\nElements : ");
for(i=*f;i<=*r;i++)
printf("%4d",k[i]);
printf("\nPriority : ");
for(i=*f;i<=*r;i++)
printf("%4d",p[i]);
}
void del(int *k,int *f,int *r)
{
if(*f == 0)
{
printf("\n\nPRIORITY QUEUE IS UNDERFLOW.\n");
return;
}
printf("\n\n%d is deleted.",k[*f]);
if(*f == *r)
{
*f = *r = 0;
}
else
*f+=1;
}
/* Write a Program for to create priority queue*/
#define SIZE 5
void insert(int *,int *,int *,int *);
void del(int *,int *,int *);
void disp(int *,int *,int *,int *);
void main()
{
int ch,f,r,arr[SIZE],p[SIZE],n;
f=r=0;
clrscr();
do
{
printf("\n\n1. For Insert\n2. For Delete\n3. For Exit");
printf("\nEnter Your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert(arr,p,&f,&r);
if(f!=0)
disp(arr,p,&f,&r);
break;
case 2:
del(arr,&f,&r);
if(f!=0)
disp(arr,p,&f,&r);
break;
}
}while(ch!=3);
}
void insert(int *arr,int *p,int *front,int *rear)
{
int item,pr,t1,t2,i,j;
if(*rear == SIZE)
{
printf("\n\n PRIORITY QUEUE IS OVERFLOW");
return;
}
printf("Enter Element:");
scanf("%d",&item);
printf("Enter Element Priority:");
scanf("%d",&pr);
if (*front==0)
*front = *rear = 1;
else
*rear += 1;
arr[*rear] = item;
p[*rear] = pr;
for(i=*front;i<=*rear;i++)
{
for(j=i+1;j<=*rear;j++)
{
if(p[j]<p[i])
{
t1 = arr[i];
t2 = p[i];
arr[i] = arr[j];
p[i] = p[j];
arr[j] = t1;
p[j] = t2;
}
}
}
}
void del(int *k,int *f,int *r)
{
if(*f == 0)
{
printf("\n\n PRIORITY QUEUE IS UNDERFLOW");
return;
}
printf("\n\n\t\t%d is deleted. !!!\n",k[*f]);
if(*f == *r)
*f = *r = 0;
else
*f+=1;
}
void disp(int *k,int *p,int *f,int *r)
{
int i;
printf("\n");
for(i=*f;i<=*r;i++)
printf("%4d",k[i]);
printf("\nPriority:\n");
for(i=*f;i<=*r;i++)
printf("%4d",p[i]);
}
//Priority Queue using Simple Queue in Array: INSERT, DELETE, VIEW
#include<stdio.h>
void PQinsert(int q[],int *f,int *l,int val,int n)
{
if(*l == n)
{
printf("\nQueue is Full");
return;
}
else if(*l == -1)
*f = *l = 0;
else
*l = *l + 1;
q[*l]=val;
printf("\nItem is Inserted");
return;
}
int PQdelete(int q[],int *f,int *l)
{
int temp;
temp=q[*f];
if(*f == *l)
*f=*l=-1;
else
*f = *f + 1;
return temp;
}
void main()
{
int A[3],B[4],C[3],af=-1,al=-1,bf=-1,bl=-1,cf=-1,cl=-1,ch,value,p;
int i,f;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
printf("Enter Value: ");
scanf("%d",&value);
do
{
printf("Enter Priority: ");
scanf("%d",&p);
}while(p<1 || p>3);
switch(p)
{
case 1:
PQinsert(A,&af,&al,value,2);
break;
case 2:
PQinsert(B,&bf,&bl,value,3);
break;
case 3:
PQinsert(C,&cf,&cl,value,2);
}
getch();
break;
case 2:
f=10;
if(af!=-1)
value=PQdelete(A,&af,&al);
else if(bf!=-1)
value=PQdelete(B,&bf,&bl);
else if(cf!=-1)
value=PQdelete(C,&cf,&cl);
else
{
printf("\nQueue is empty");
f=0;
}
if(f!=0)
printf("\nDeleted Item is: %d",value);
getch();
break;
case 3:
if(af!=-1)
{ printf("\nQueue A:");
for(i=af;i<=al;i++)
printf(" %d",A[i]);
}
if(bf!=-1)
{ printf("\nQueue B:");
for(i=bf;i<=bl;i++)
printf(" %d",B[i]);
}
if(cf!=-1)
{ printf("\nQueue C:");
for(i=cf;i<=cl;i++)
printf(" %d",C[i]);
}
getch();
}
}while(ch!=4);
}
//--------------------------
//Priority Queue in Linked-List: INSERT, DELETE, VIEW
#include<stdio.h>
struct emp
{
int id;
struct emp *next;
}*Afirst=NULL,*Alast=NULL,*Bfirst=NULL,*Blast=NULL;
struct emp *Cfirst=NULL,*Clast=NULL,*link=NULL,*temp=NULL;
void PQinsert(int p)
{
temp=(struct emp *) malloc(sizeof(struct emp));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
printf("Enter Value: ");
scanf("%d",&temp->id);
temp->next=NULL;
if(p==1)
{
if(Afirst == NULL)
{
Afirst=temp;
Alast=temp;
}
else
{
Alast->next=temp;
Alast=temp;
}
}
else if(p==2)
{
if(Bfirst == NULL)
{
Bfirst=temp;
Blast=temp;
}
else
{
Blast->next=temp;
Blast=temp;
}
}
else if(p==3)
{
if(Cfirst == NULL)
{
Cfirst=temp;
Clast=temp;
}
else
{
Clast->next=temp;
Clast=temp;
}
}
printf("\nItem is Inserted");
}
}
void PQdelete()
{
if(Afirst!=NULL)
{
if(Afirst->next==Alast->next)
{
printf("\nThe Deleted Item is:");
printf("\n %d",Afirst->id);
free(Afirst);
Afirst=Alast=NULL;
}
else
{
printf("\nThe Deleted Item is:");
printf("\n %d",Afirst->id);
link=Afirst;
Afirst=Afirst->next;
free(link);
}
}
else if(Bfirst!=NULL)
{
if(Bfirst->next==Blast->next)
{
printf("\nThe Deleted Item is:");
printf("\n %d",Bfirst->id);
free(Bfirst);
Bfirst=Blast=NULL;
}
else
{
printf("\nThe Deleted Item is:");
printf("\n %d",Bfirst->id);
link=Bfirst;
Bfirst=Bfirst->next;
free(link);
}
}
else if(Cfirst!=NULL)
{
if(Cfirst->next==Clast->next)
{
printf("\nThe Deleted Item is:");
printf("\n %d",Cfirst->id);
free(Cfirst);
Cfirst=Clast=NULL;
}
else
{
printf("\nThe Deleted Item is:");
printf("\n %d",Cfirst->id);
link=Cfirst;
Cfirst=Cfirst->next;
free(link);
}
}
else
printf("\nQueue is Empty");
}
void PQview()
{
if(Afirst!=NULL)
{ printf("\nQueue A:");
link=Afirst;
while(link!=NULL)
{
printf(" %d",link->id);
link=link->next;
}
}
if(Bfirst!=NULL)
{ printf("\nQueue B:");
link=Bfirst;
while(link!=NULL)
{
printf(" %d",link->id);
link=link->next;
}
}
if(Cfirst!=NULL)
{ printf("\nQueue C:");
link=Cfirst;
while(link!=NULL)
{
printf(" %d",link->id);
link=link->next;
}
}
}
void main()
{
int ch,p,i;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
do
{
printf("Enter Priority: ");
scanf("%d",&p);
}while(p<1 || p>3);
PQinsert(p);
getch();
break;
case 2:
PQdelete();
getch();
break;
case 3:
PQview();
getch();
}
}while(ch!=4);
}
8. Write a Program to implement Double ended queue (Input Restricted/Output restricted)...
/* Write a Program to implement Double ended queue (Input Restricted) */
int max;
void insert(int *,int *,int *);
void ldel(int *,int *,int *);
void rdel(int *,int *,int *);
void disp(int *,int *,int *);
int isempty(int *);
int isfull(int *);
void main()
{
int rear=0,front=0;
int i,*queue,check;
int choice;
clrscr();
printf("\nEnter howmany element u want to stote in dque= ");
scanf("%d",&max);
do
{
clrscr();
printf("\n1 : Insert an element.");
printf("\n2 : Delete element from left side.");
printf("\n3 : Delete element from right side.");
printf("\n4 : Display elemtent.");
printf("\n5 : Exit from Program.\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
check = isfull(&rear);
if(check==0)
{
insert(queue,&front,&rear);
disp(queue,&front,&rear);
}
getch();
break;
case 2 :
check = isempty(&front);
if(check==0)
{
ldel(queue,&front,&rear);
disp(queue,&front,&rear);
}
getch();
break;
case 3 :
check = isempty(&front);
if(check==0)
{
rdel(queue,&front,&rear);
disp(queue,&front,&rear);
}
getch();
break;
case 4:
disp(queue,&front,&rear);
getch();
break;
case 5:
return;
default :
printf("\n\nINVALID CHOICE\n");
}
}while(1);
}
void insert(int *q,int *f,int *r)
{
int item;
if(*f==0)
*f=1;
printf("\nEnter an item : ");
scanf("%d",&item);
*r+=1;
q[*r] = item;
}
void ldel(int *q,int *f,int *r)
{
printf("\n\n%d is deleted\n",q[*f]);
if(*f==*r)
{
printf("\n\n\t\t!!!!!! Queue is Empty !!!!!!\n");
*f = *r = 0;
}
else
*f+=1;
}
void rdel(int *q,int *f,int *r)
{
printf("\n\n%d is deleted\n",q[*r]);
if(*f==*r)
{
printf("\n\n\t\t!!!!!! Queue is Empty !!!!!!\n");
*f = *r = 0;
}
else
*r-=1;
}
void disp(int *q,int *f,int *r)
{
int i;
printf("\n\nQueue Contains : ");
if(*f!=0)
for(i=*f;i<=*r;i++)
{
printf("%d ",q[i]);
}
}
int isfull(int *r)
{
if(*r == max)
{
printf("\n\nQueue is Overflow\n");
return 1;
}
else
return 0;
}
int isempty(int *f)
{
if(*f == 0)
{
printf("\n\nQueue is Underflow.\n");
return 1;
}
else
return 0;
}
//8.2
///* Write a Program to implement Double ended queue (Output Restricted) */
int max;
void rinsert(int *,int *,int *);
void del(int *,int *,int *);
void linsert(int *,int *,int *);
void disp(int *,int *,int *);
int isempty(int *);
int isfull(int *);
void main()
{
int rear=0,front=0;
int i,*queue,check;
int choice;
clrscr();
printf("\nEnter howmany value u want to store in dque= ");
scanf("%d",&max);
do
{
clrscr();
printf("\n1 : Insert an element from right side.");
printf("\n2 : Insert an element from left side.");
printf("\n3 : Delete element.");
printf("\n4 : Display element.");
printf("\n5 : Exit from Program.\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
check = isfull(&rear);
if(check==0)
{
rinsert(queue,&front,&rear);
disp(queue,&front,&rear);
}
getch();
break;
case 2 :
linsert(queue,&front,&rear);
disp(queue,&front,&rear);
getch();
break;
case 3 :
check = isempty(&front);
if(check==0)
{
del(queue,&front,&rear);
disp(queue,&front,&rear);
}
getch();
break;
case 4:
disp(queue,&front,&rear);
getch();
break;
case 5:
return;
default :
printf("\n\nINVALID CHOICE\n");
break;
}
}while(1);
}
void linsert(int *q,int *f,int *r)
{
int item;
if(*f==1)
{
printf("\nQueue is Overflow\n");
return;
}
printf("\nEnter an item : ");
scanf("%d",&item);
if(*f==0)
*r = *f = max;
else
*f-=1;
q[*f] = item;
}
void rinsert(int *q,int *f,int *r)
{
int item;
if(*f==0)
*f=1;
printf("\nEnter an item : ");
scanf("%d",&item);
*r+=1;
q[*r] = item;
}
void del(int *q,int *f,int *r)
{
if(*f == 0)
{
printf("\n\nQueue is Empty\n");
return;
}
printf("\n\n%d is deleted\n",q[*f]);
if(*f == *r)
*f = *r = 0;
else
*f+=1;
}
void disp(int *q,int *f,int *r)
{
int i;
printf("\n\nQueue Contains : ");
if(*f!=0)
for(i=*f;i<=*r;i++)
{
printf("%d ",q[i]);
}
}
int isfull(int *r)
{
if(*r == max)
{
printf("\n\nQueue is Overflow\n");
return 1;
}
else
return 0;
}
int isempty(int *f)
{
if(*f == 0)
{
printf("\n\nQueue is Underflow.\n");
return 1;
}
else
return 0;
}
/* Write a Program for Input Restricted Queue that perform insertion and
deletion operation */
#define SIZE 5
void insert(int *,int *,int *);
void ldel(int *,int *,int *);
void rdel(int *,int *,int *);
void disp(int *,int *,int *);
int isempty(int *);
int isfull(int *);
void main()
{
int rear=0,front=0,ch,*queue,check;
clrscr();
do
{
printf("\n1. For Insert\n2. For Delete From Left\n3. For Delete From Right\4. For Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 :
check = isfull(&rear);
if(check==0)
{
insert(queue,&front,&rear);
disp(queue,&front,&rear);
}
break;
case 2 :
check = isempty(&front);
if(check==0)
{
ldel(queue,&front,&rear);
disp(queue,&front,&rear);
}
break;
case 3 :
check = isempty(&front);
if(check==0)
{
rdel(queue,&front,&rear);
disp(queue,&front,&rear);
}
break;
}
}while(ch!=4);
}
void insert(int *q,int *f,int *r)
{
int item;
if(*f==0)
*f=1;
printf("\nEnter an Element: ");
scanf("%d",&item);
*r+=1;
q[*r] = item;
}
void ldel(int *q,int *f,int *r)
{
printf("\n\n %d is ldeleted",q[*f]);
if(*f==*r)
{
printf("\n Queue is Empty");
*f = *r = 0;
}
else
*f+=1;
}
void rdel(int *q,int *f,int *r)
{
printf("\n\n %d is ldeleted",q[*r]);
if(*f==*r)
{
printf("\n Queue is Empty");
*f = *r = 0;
}
else
*r-=1;
}
void disp(int *q,int *f,int *r)
{
int i;
if(*f!=0)
for(i=*f;i<=*r;i++)
{
printf("%d ",q[i]);
}
}
int isfull(int *r)
{
if(*r == SIZE)
{
printf("\n Queue is Overflow");
return 1;
}
else
return 0;
}
int isempty(int *f)
{
if(*f == 0)
{
printf("\nQueue is Underflow");
return 1;
}
else
return 0;
}
//8.2
/* Write a Program for Output Restricted Queue that perform insertion and
deletion operation */
#define SIZE 5
void rinsert(int *,int *,int *);
void del(int *,int *,int *);
void linsert(int *,int *,int *);
void disp(int *,int *,int *);
int isempty(int *);
int isfull(int *);
void main()
{
int ch,rear=0,front=0,i,*queue,check;
clrscr();
do
{
printf("\n1. For Inset From Right\n2. For Insert Left Side\n3. For Delete\n4. For Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 :
check = isfull(&rear);
if(check==0)
{
rinsert(queue,&front,&rear);
disp(queue,&front,&rear);
}
break;
case 2 :
linsert(queue,&front,&rear);
disp(queue,&front,&rear);
break;
case 3 :
check = isempty(&front);
if(check==0)
{
del(queue,&front,&rear);
disp(queue,&front,&rear);
}
break;
}
}while(ch!=4);
}
void linsert(int *q,int *f,int *r)
{
int item;
if(*f==1)
{
printf("\nQueue is Overflow");
return;
}
printf("\nEnter an Element:");
scanf("%d",&item);
if(*f==0)
*r = *f = SIZE;
else
*f-=1;
q[*f] = item;
}
void rinsert(int *q,int *f,int *r)
{
int item;
if(*f==0)
*f=1;
printf("\nEnter an Element:");
scanf("%d",&item);
*r+=1;
q[*r] = item;
}
void del(int *q,int *f,int *r)
{
if(*f == 0)
{
printf("\n Queue is Empty");
return;
}
printf("\n %d is ldeleted",q[*f]);
if(*f == *r)
*f = *r = 0;
else
*f+=1;
}
void disp(int *q,int *f,int *r)
{
int i;
if(*f!=0)
for(i=*f;i<=*r;i++)
{
printf("%d ",q[i]);
}
}
int isfull(int *r)
{
if(*r == SIZE)
{
printf("\n Queue is Overflow");
return 1;
}
else
return 0;
}
int isempty(int *f)
{
if(*f == 0)
{
printf("\nQueue is Underflow");
return 1;
}
else
return 0;
}
//Input-Restricted Double-Ended Queue
//using Simple Queue in Array: INSERT, DELETE, VIEW
#include<stdio.h>
void DQinsert(int q[],int *f,int *l,int val)
{
if(*l == 4)
{
printf("\nQueue is Full");
return;
}
else if(*l == -1)
*f = *l = 0;
else
*l = *l + 1;
q[*l]=val;
printf("\nItem is Inserted");
return;
}
int DQdelete(int q[],int *f,int *l,int end)
{
int temp;
if(*f == -1)
{
printf("\nQueue is Empty");
return NULL;
}
else
{
if(*f == *l)
{
temp=q[*f];
*f=*l=-1;
}
else if(end==1)
{
temp=q[*f];
*f = *f + 1;
}
else
{
temp=q[*l];
*l = *l - 1;
}
return temp;
}
}
void main()
{
int q[5],first=-1,last=-1,ch,value;
int i;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
printf("Enter Value: ");
scanf("%d",&value);
DQinsert(q,&first,&last,value);
getch();
break;
case 2:
printf("\n1. Front\t 2. Rear");
do
{
printf("\nEnter End: ");
scanf("%d",&value);
}while(value<1 || value>2);
value=DQdelete(q,&first,&last,value);
if(value!=NULL)
printf("\nDeleted Item is: %d",value);
getch();
break;
case 3:
printf("\n");
if(first!=-1)
{
for(i=first;i<=last;i++)
printf(" %d",q[i]);
}
getch();
}
}while(ch!=4);
}
//Delete-Restricted Double-Ended Queue
//Delete-Restricted Double-Ended Queue
//using Simple Queue in Array: INSERT, DELETE, VIEW
#include<stdio.h>
void DQinsert(int q[],int *f,int *l,int val,int end)
{
if(end == 1)
{
if(*f == 0)
{
printf("\nCan not Insert at Front End");
return;
}
else if(*l == -1)
*f = *l = 4;
else
*f = *f - 1;
q[*f]=val;
printf("\nItem is Inserted");
}
else
{
if(*l == 4)
{
printf("\nCan not Insert at Rear End");
return;
}
else if(*f == -1)
*f = *l = 0;
else
*l = *l + 1;
q[*l]=val;
printf("\nItem is Inserted");
}
return;
}
int DQdelete(int q[],int *f,int *l)
{
int temp;
if(*f == -1)
{
printf("\nQueue is Empty");
return NULL;
}
else
{
temp=q[*f];
if(*f == *l)
*f = *l = -1;
else
*f = *f + 1;
return temp;
}
}
void main()
{
int q[5],first=-1,last=-1,ch,value;
int i,end;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
printf("Enter Value: ");
scanf("%d",&value);
printf("\n1. Front\t 2. Rear");
do
{
printf("\nEnter End: ");
scanf("%d",&end);
}while(end<1 || end>2);
DQinsert(q,&first,&last,value,end);
getch();
break;
case 2:
value=DQdelete(q,&first,&last);
if(value!=NULL)
printf("\nDeleted Item is: %d",value);
getch();
break;
case 3:
printf("\n");
if(first<last)
{
for(i=first;i<=last;i++)
printf(" %d",q[i]);
}
else if(first!=-1)
{
for(i=last;i<=first;i++)
printf(" %d",q[i]);
}
getch();
}
}while(ch!=4);
}
shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
9. (a) Write a program to create a singly linked list in LIFO fashion....
//9.c
//Write a program to create a sorted singly linked list.
struct list
{
int info;
struct list *next;
}*start,*p,*q,*node;
void main()
{
void disp(struct list *);
void create(struct list *);
int choice;
clrscr();
start=node='\0';
do
{
clrscr();
printf("\n\n1 : Add element in link list.");
printf("\n2 : Display linklist.");
printf("\n3 : Exit from Program.\n");
printf("\nEnter your choice ==========> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(start);
getch();
break;
case 2:
disp(start);
getch();
break;
case 3:
return;
default:
printf("\n\nINVALID CHOICE\n");
getch();
}
}while(1);
}
void disp(struct list *t)
{
printf("\nSorted link list contains : ");
while(t!='\0')
{
printf("%d ",t->info);
t= t->next;
}
}
void create(struct list *t)
{
int item,i,j;
p = t;
q = t->next;
printf("Enter an item ======> ");
scanf("%d",&item);
node = (struct list *)malloc(sizeof(struct list));
node->info = item;
node->next = '\0';
if(t == '\0')
start = node;
else if(node->info < t->info)
{
node->next = t;
start = node;
}
else
{
while(q!='\0')
{
if(node->info < q->info)
{
node->next = p->next;
p->next = node;
return;
}
else
{
p = q;
q = q->next;
}
}
p->next = node;
}
}
//9.e
/*
Write program perform the following operations on a singly linked list.
1. Insert an element
2. Delete an element
3. Find the sum of elements of the list
4. Count number of the nodes in the linked list
5. Search a given elements in the linked list.
6. Reverse the linked list.
7. Make a copy of the given linked list
8. Merge two linked list.
9. Find the union of the two given linked list
10. Find the intersection of the two given linked list.
*/
int count,flag=0,sum;
struct linklist
{
int info;
struct linklist *next;
}*start,*FIRST,*begin,*node,*p,*prev;
void insert1();
void insert2(struct linklist *);
void first();
void last(struct linklist *);
void specified(struct linklist *);
void del(struct linklist *);
void find(struct linklist *);
void reverse(struct linklist *);
void copy(struct linklist *);
void mergecreate();
void merge(struct linklist *,struct linklist *);
void union1(struct linklist *,struct linklist *);
void intersection(struct linklist *,struct linklist *);
void dispstart(struct linklist *);
void dispfirst(struct linklist *);
void dispmerge(struct linklist *);
void findsum(struct linklist *);
void main()
{
int choice,item;
int i;
clrscr();
start = FIRST = begin = p = node = '\0';
printf("Enter an Eelement -1 for Stop ==> ");
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
if(item!=-1)
p = start = node;
while(item!=-1)
{
count++; //Count Number of nodes
node->info = item;
p->next = node;
p = p->next;
node->next = '\0';
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
}
do
{
clrscr();
printf("\n1 : Insert Element in list.");
printf("\n2 : Delete Element From list.");
printf("\n3 : Find an Element from list.");
printf("\n4 : Count Number of Nodes.");
printf("\n5 : Reverse Given Linklist.");
printf("\n6 : Copy the Given Linklist.");
printf("\n7 : Merge(Concate) Two Linklist.");
printf("\n8 : Find Union of Two linklist.");
printf("\n9 : Find Intersection of Two linklist.");
printf("\n10: Find the Sum of Element.");
printf("\n11: Display Linklist.");
printf("\n12: Exit From Program.\n");
printf("\nEnter Your Choice =====> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert1();
getch();
break;
case 2:
del(start);
getch();
break;
case 3:
find(start);
getch();
break;
case 4:
printf("\n\t\t>>>>>>>> THERE ARE %d NODES IN LINKLIST. <<<<<<<<<\n",count);
getch();
break;
case 5:
FIRST = '\0';
reverse(start);
printf("\nLinklist Contains : ");
dispstart(start);
printf("\nREVERSE LINKLIST : ");
dispfirst(FIRST);
getch();
break;
case 6:
FIRST = '\0';
copy(start);
printf("\nLinklist Contains : ");
dispstart(start);
printf("\nLinklist Contains : ");
dispstart(FIRST);
getch();
break;
case 7:
begin = '\0';
if(flag==0)
mergecreate();
merge(start,FIRST);
printf("\nLinklist Contains : ");
dispstart(start);
printf("\nLinklist Contains : ");
dispstart(FIRST);
printf("\nMERGE LINKLIST : ");
dispstart(begin);
getch();
break;
case 8:
begin = '\0';
if(flag==0)
mergecreate();
union1(start,FIRST);
printf("\nLinklist Contains : ");
dispstart(start);
printf("\nLinklist Contains : ");
dispstart(FIRST);
printf("\nUnion Linklist : ");
dispstart(begin);
getch();
break;
case 9:
begin = '\0';
if(flag==0)
mergecreate();
intersection(start,FIRST);
printf("\nLinklist Contains : ");
dispstart(start);
printf("\nLinklist Contains : ");
dispstart(FIRST);
printf("\nIntersection List : ");
dispstart(begin);
getch();
break;
case 10:
sum = 0;
findsum(start);
printf("\n\nSUM OF ELEMENT IS %d.\n",sum);
getch();
break;
case 11:
printf("\nLinklist Contains : ");
dispstart(start);
getch();
break;
case 12:
return;
default:
printf("\n\nINVALID CHOICE\n");
}
}while(1);
}
void dispfirst(struct linklist *p)
{
while(p!='\0')
{
printf("%d ",p->info);
p = p->next;
}
}
void dispstart(struct linklist *first)
{
p=first;
while(p!='\0')
{
printf("%d ",p->info);
p = p->next;
}
}
void insert1()
{
int choice;
printf("\n\t1 : Insert at First Position.");
printf("\n\t2 : Insert at Last Position.");
printf("\n\t3 : Insert at Specified Position.");
printf("\n\n\tEnter Your Choice =======> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
first();
break;
case 2:
last(start);
break;
case 3:
specified(start);
break;
default:
printf("\nINVALID CHOICE\n");
}
}
void first()
{
int item;
printf("\nEnter an Item =====> ");
scanf("%d",&item);
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = item;
node->next = start;
start = node;
count++;
}
void last(struct linklist *p)
{
int item;
printf("\nEnter an Item =====> ");
scanf("%d",&item);
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info=item;
node->next = '\0';
if(start=='\0')
start = node;
else
while(p->next != '\0')
p = p->next;
p->next = node;
count++;
}
void specified(struct linklist *p)
{
int item,pos,n=2;
printf("\nEnter an Item =====> ");
scanf("%d",&item);
printf("\nEnter Position =====> ");
scanf("%d",&pos);
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info=item;
node->next = '\0';
if(pos==1)
{
node->next = start;
start = node;
count++;
return;
}
else
while(pos>n)
{
p = p->next;
if(p == '\0')
{
printf(" \nPosition Not Found.\n");
return;
}
n++;
}
node->next = p->next;
p->next = node;
count++;
}
void del(struct linklist *p)
{
int item;
if(start == '\0')
{
printf("\nLINKLIST IS EMPTY\n");
return;
}
printf("Enter Item that You want to DELETE ======> ");
scanf("%d",&item);
if(item == start->info)
{
printf("\n%d is deleted.\n",start->info);
start = start->next;
count--;
}
else
while(p!='\0')
{
prev = p;
p = p->next;
if(p->info == item)
{
printf("\n%d is deleted.\n",p->info);
prev->next = p->next;
count--;
return;
}
}
if(p=='\0')
printf("\nItem Does Not Found.\n");
}
void find(struct linklist *p)
{
int i=0,item;
if(start == '\0')
{
printf("\nLINKLIST IS EMPTY\n");
return;
}
printf("Enter Item that U want to Find =====> ");
scanf("%d",&item);
if(start->info == item)
{
i++;
printf("\nYOUR SELECTED ITEM IS %d AT POSITION %d.",item,i);
return;
}
else
while(p != '\0')
{
if(p->info == item)
{
i++;
printf("\nYOUR SELECTED ITEM IS %d AT POSITION %d.",item,i);
return;
}
else
{
i++;
}
p = p->next;
}
if(p == '\0')
printf("\nITEM IS NOT FOUND.\n");
}
void reverse(struct linklist *p)
{
if(p=='\0')
{
printf("\n\nLINKLIST IS EMPTY.\n");
return;
}
while(p != '\0')
{
node = (struct linklist *)malloc(sizeof(struct linklist));
node->info = p->info;
node->next = FIRST;
if(FIRST == '\0')
node->next = '\0';
FIRST = node;
p = p->next;
}
}
void copy(struct linklist *p)
{
struct linklist *q;
q = p;
if(p=='\0')
{
printf("\n\nLINKLIST IS EMPTY\n");
return;
}
while(p != '\0')
{
node = (struct linklist *)malloc(sizeof(struct linklist));
if(FIRST == '\0')
FIRST = q = node;
node->info = p->info;
q->next = node;
q = q->next;
node->next = '\0';
p = p->next;
}
printf("\n\nLINKLIST IS COPIED.\n");
}
void mergecreate()
{
int item;
flag = 1;
printf("Enter an Eelement in SECOND LIST -1 for Stop ==> ");
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
if(item!=-1)
p = FIRST = node;
while(item!=-1)
{
node->info = item;
p->next = node;
p = p->next;
node->next = '\0';
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
}
}
void merge(struct linklist *p,struct linklist *q)
{
struct linklist *t;
t = p;
if(p=='\0')
{
printf("\nFIRST LINKLIST IS EMPTY\n");
goto next1;
}
while(p != '\0')
{
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = p->info;
if(begin == '\0')
{
begin = t = node;
}
t->next = node;
t = t->next;
node->next = '\0';
p = p->next;
}
next1:
if(FIRST == '\0')
{
printf("\nSECOND LINKLIST IS EMPTY\n");
return;
}
node = (struct linklist*)malloc(sizeof(struct linklist));
if(start == '\0')
begin = t = node;
while(q!='\0')
{
node->info = q->info;
t->next = node;
t = t->next;
node->next = '\0';
node = (struct linklist*)malloc(sizeof(struct linklist));
q = q->next;
}
}
void union1(struct linklist *p,struct linklist *q)
{
int flag1=0,item;
struct linklist *t;
if(p=='\0')
{
printf("\nFIRST LINKLIST IS EMPTY\n");
goto next1;
}
while(p != '\0')
{
t = begin;
flag1 = 0;
while(t!='\0')
{
if(p->info == t->info)
{
flag1=1;
break;
}
t = t->next;
}
if(flag1==0)
{
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = p->info;
node->next = '\0';
if(begin == '\0')
{
begin = node;
}
else
{
t = begin;
while(t->next != '\0')
t = t->next;
t->next = node;
}
}
p = p->next;
}
next1:
if(q=='\0')
{
printf("\nSECOND LINKLIST IS EMPTY\n");
return;
}
while(q != '\0')
{
t = begin;
flag1 = 0;
while(t!='\0')
{
if(q->info == t->info)
{
flag1=1;
break;
}
t = t->next;
}
if(flag1==0)
{
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = q->info;
node->next = '\0';
if(begin == '\0')
{
begin = node;
}
else
{
t = begin;
while(t->next != '\0')
t = t->next;
t->next = node;
}
}
q = q->next;
}
}
void intersection(struct linklist *p,struct linklist *s)
{
struct linklist *t,*temp,*q;
int item,flag1,chkflag;
if(p=='\0')
{
printf("\n\nFIRST LINKLIST IS EMPTY\n");
return;
}
while(p!='\0')
{
flag1 = 0;
q = s;
while(q!='\0')
{
if(p->info == q->info)
{
item = p->info;
flag1 = 1;
break;
}
q = q->next;
}
if(flag1 == 1)
{
chkflag = 0;
temp = begin;
while(temp!='\0')
{
if(temp->info == item)
{
chkflag = 1;
break;
}
temp = temp->next;
}
if(chkflag == 0)
{
t = begin;
node = (struct linklist *)malloc(sizeof(struct linklist));
node->info = item;
node->next = '\0';
if(begin == '\0')
begin = node;
else
{
while(t->next != '\0')
t = t->next;
t->next = node;
}
}
}
p = p->next;
}
}
void findsum(struct linklist *p)
{
while(p!='\0')
{
sum+=p->info;
p = p->next;
}
}
//9.c
/* W.A.P. to create a sorted singly linked list. */
#include <stdio.h>
#include <conio.h>
struct list
{
int info;
struct list *next;
}*start,*p,*q,*node;
void create(struct list *t)
{
int item,i,j;
p = t;
q = t->next;
printf("Enter an Element:");
scanf("%d",&item);
node = (struct list *)malloc(sizeof(struct list));
node->info = item;
node->next = NULL;
if(t == NULL)
start = node;
else if(node->info < t->info)
{
node->next = t;
start = node;
}
else
{
while(q!=NULL)
{
if(node->info < q->info)
{
node->next = p->next;
p->next = node;
return;
}
else
{
p = q;
q = q->next;
}
}
p->next = node;
}
}
void disp(struct list *t)
{
while(t!=NULL)
{
printf("%d ",t->info);
t= t->next;
}
}
void main()
{
int ch;
clrscr();
start=node=NULL;
do
{
printf("\n1. For Insert\n2. For Display\n3. For Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create(start);
break;
case 2:
disp(start);
break;
}
}while(ch!=3);
}
//9.e
/*
Write a Program perform the following operations on a singly likked list.
1. Insert an element.
2. Delete an element.
3. Find the sum of elements of the list.
4. Count number of the nodes in the linked list.
5. Search given elements in the linked list.
6. Reverse the linked list.
7. Make a copy of the nodes in the linked list.
8. concate two linked list.
9. Merge two linked list.
10.Find the union of the given linked list.
11.Find the intersection of the two given linked list.
*/
int count,flag=0,sum;
struct linklist
{
int info;
struct linklist *next;
}*fintersection,*funion,*fmerge,*start,*FIRST,*FIRST1,*node,*p,*prev;
void insert1();
void insert2(struct linklist *);
void first();
void last(struct linklist *);
void specified(struct linklist *);
void del(struct linklist *);
void find(struct linklist *);
void reverse(struct linklist *);
void copy(struct linklist *);
void mergecreate();
void merge(struct linklist *,struct linklist *);
void union1(struct linklist *,struct linklist *);
void intersection(struct linklist *,struct linklist *);
void dispstart(struct linklist *);
void dispfirst(struct linklist *);
void dispmerge(struct linklist *);
void main()
{
int choice,item;
int i;
clrscr();
start = FIRST = p = node = '\0';
printf("Enter an Eelement -1 for Stop ==> ");
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
if(item!=-1)
p = start = node;
while(item!=-1)
{
count++; //Count Number of nodes
node->info = item;
p->next = node;
p = p->next;
node->next = '\0';
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
}
do
{
printf("\n\1. For Insert\n2. For Delete\n3. For Find\n4. For Count\n5. For Reverse\n6. For Copy LL\n7. For Merger 2 LL");
printf("\n8. For Union\n9. For Intersection\n10. For Sum of LL\n11. For Exit");
printf("\nEnter Your Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert1();
dispstart(start);
break;
case 2:
del(start);
dispstart(start);
break;
case 3:
find(start);
break;
case 4:
printf("\n %d Element in LinkList",count);
break;
case 5:
reverse(start);
dispstart(start);
printf("\nREVERSE LINKLIST : ");
dispfirst(FIRST);
break;
case 6:
copy(start);
dispstart(start);
printf("\nCopy of LinkList");
dispstart(FIRST);
break;
case 7:
if(flag==0)
mergecreate();
merge(start,FIRST1);
printf("\nLinklist Contains : ");
dispstart(start);
printf("\nLinklist Contains : ");
dispstart(FIRST1);
printf("\nMERGE LINKLIST : ");
dispstart(fmerge);
fmerge = '\0';
break;
case 8:
if(flag==0)
mergecreate();
union1(start,FIRST1);
printf("\nFirst Linklist");
dispstart(start);
printf("\nSecond Linklist");
dispstart(FIRST1);
printf("\nUnion Linklist");
dispstart(funion);
funion = '\0';
break;
case 9:
if(flag==0)
mergecreate();
intersection(start,FIRST1);
printf("\nFirst Linklist");
dispstart(start);
printf("\nSecond Linklist");
dispstart(FIRST1);
printf("\nIntersection LinkList");
dispstart(fintersection);
fintersection = '\0';
break;
case 10:
sum = 0;
dispstart(start);
printf("\nSum of Element is: %d\n",sum);
break;
}
}while(choice!=11);
}
void dispfirst(struct linklist *p)
{
while(p!='\0')
{
printf("%d ",p->info);
p = p->next;
}
FIRST = '\0';
}
void dispstart(struct linklist *first)
{
p=first;
while(p!='\0')
{
printf("%d ",p->info);
sum+=p->info;
p = p->next;
}
}
void insert1()
{
int choice;
printf("\n\t1 : Insert at First Position.");
printf("\n\t2 : Insert at Last Position.");
printf("\n\t3 : Insert at Specified Position.");
printf("\n\n\tEnter Your Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
first();
break;
case 2:
last(start);
break;
case 3:
specified(start);
break;
}
}
void first()
{
int item;
printf("\nEnter an Element:");
scanf("%d",&item);
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = item;
node->next = start;
start = node;
count++;
}
void last(struct linklist *p)
{
int item;
printf("\nEnter an Element:");
scanf("%d",&item);
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info=item;
node->next = '\0';
if(start=='\0')
start = node;
else
while(p->next != '\0')
p = p->next;
p->next = node;
count++;
}
void specified(struct linklist *p)
{
int item,pos,n=2;
printf("\nEnter an Element:");
scanf("%d",&item);
printf("\nEnter Position:");
scanf("%d",&pos);
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info=item;
node->next = '\0';
if(pos==1)
{
node->next = start;
start = node;
count++;
return;
}
else
{
while(pos>n)
{
p = p->next;
if(p == '\0')
{
printf("\nPosition Not Found");
return;
}
n++;
}
}
node->next = p->next;
p->next = node;
count++;
}
void del(struct linklist *p)
{
int item;
if(start == '\0')
{
printf("\n LinkList is Underflow");
return;
}
printf("Enter Item that You want to DELETE:");
scanf("%d",&item);
if(item == start->info)
{
printf("\n %d is deleted",start->info);
start = start->next;
count--;
}
else
{
while(p!='\0')
{
prev = p;
p = p->next;
if(p->info == item)
{
printf("\n %d is deleted",p->info);
prev->next = p->next;
count--;
return;
}
}
}
if(p=='\0')
printf("\nElement Not Found");
}
void find(struct linklist *p)
{
int i=0,item;
if(start == '\0')
{
printf("\n LinkList is Underflow");
return;
}
printf("Enter Element You want to Find:");
scanf("%d",&item);
if(start->info == item)
{
i++;
printf("\nElement is %d at Position %d",item,i);
return;
}
else
{
while(p != '\0')
{
if(p->info == item)
{
i++;
printf("\nElement is %d at Position %d",item,i);
return;
}
else
{
i++;
}
p = p->next;
}
}
if(p == '\0')
printf("\nElement Not Found");
}
void reverse(struct linklist *p)
{
if(p=='\0')
{
printf("\n LinkList is Underflow");
return;
}
while(p != '\0')
{
node = (struct linklist *)malloc(sizeof(struct linklist));
node->info = p->info;
node->next = FIRST;
if(FIRST == '\0')
node->next = '\0';
FIRST = node;
p = p->next;
}
}
void copy(struct linklist *p)
{
struct linklist *q;
q = p;
if(p=='\0')
{
printf("\n LinkList is Underflow");
return;
}
while(p != '\0')
{
node = (struct linklist *)malloc(sizeof(struct linklist));
if(FIRST == '\0')
FIRST = q = node;
node->info = p->info;
q->next = node;
q = q->next;
node->next = '\0';
p = p->next;
}
printf("\nLinkList is Copied");
}
void mergecreate()
{
int item;
flag = 1;
printf("Enter an Eelement in SECOND LIST -1 for Stop :");
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
if(item!=-1)
p = FIRST1 = node;
while(item!=-1)
{
node->info = item;
p->next = node;
p = p->next;
node->next = '\0';
scanf("%d",&item);
node = (struct linklist *)malloc(sizeof(struct linklist));
}
}
void merge(struct linklist *p,struct linklist *q)
{
struct linklist *t;
t = p;
if(p=='\0')
{
printf("\n First LinkList is Underflow");
goto next1;
}
while(p != '\0')
{
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = p->info;
if(fmerge == '\0')
{
fmerge = t = node;
}
t->next = node;
t = t->next;
node->next = '\0';
p = p->next;
}
next1:
if(FIRST1 == '\0')
{
printf("\n Second LinkList is Underflow");
return;
}
node = (struct linklist*)malloc(sizeof(struct linklist));
if(start == '\0')
fmerge = t = node;
while(q!='\0')
{
node->info = q->info;
t->next = node;
t = t->next;
node->next = '\0';
node = (struct linklist*)malloc(sizeof(struct linklist));
q = q->next;
}
}
void union1(struct linklist *p,struct linklist *q)
{
int flag1=0,item;
struct linklist *t;
if(p=='\0')
{
printf("\n\t\t>>>>>>>> FIRST LINKLIST IS EMPTY <<<<<<<<\n");
goto next1;
}
while(p != '\0')
{
t = funion;
flag1 = 0;
while(t!='\0')
{
if(p->info == t->info)
{
flag1=1;
break;
}
t = t->next;
}
if(flag1==0)
{
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = p->info;
node->next = '\0';
if(funion == '\0')
{
funion = node;
}
else
{
t = funion;
while(t->next != '\0')
t = t->next;
t->next = node;
}
}
p = p->next;
}
next1:
if(q=='\0')
{
printf("\n Second LinkList is Underflow");
return;
}
while(q != '\0')
{
t = funion;
flag1 = 0;
while(t!='\0')
{
if(q->info == t->info)
{
flag1=1;
break;
}
t = t->next;
}
if(flag1==0)
{
node = (struct linklist*)malloc(sizeof(struct linklist));
node->info = q->info;
node->next = '\0';
if(funion == '\0')
{
funion = node;
}
else
{
t = funion;
while(t->next != '\0')
t = t->next;
t->next = node;
}
}
q = q->next;
}
}
void intersection(struct linklist *p,struct linklist *s)
{
struct linklist *t,*temp,*q;
int item,flag1,chkflag;
if(p=='\0')
{
printf("\n First LinkList is Underflow");
return;
}
while(p!='\0')
{
flag1 = 0;
q = s;
while(q!='\0')
{
if(p->info == q->info)
{
item = p->info;
flag1 = 1;
break;
}
q = q->next;
}
if(flag1 == 1)
{
chkflag = 0;
temp = fintersection;
while(temp!='\0')
{
if(temp->info == item)
{
chkflag = 1;
break;
}
temp = temp->next;
}
if(chkflag == 0)
{
t = fintersection;
node = (struct linklist *)malloc(sizeof(struct linklist));
node->info = item;
node->next = '\0';
if(fintersection == '\0')
fintersection = node;
else
{
while(t->next != '\0')
t = t->next;
t->next = node;
}
}
}
p = p->next;
}
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//(a) Write a program to create a singly linked list in LIFO fashion.
//Singly Linked-List in FIFO fashion.
#include<stdio.h>
struct List
{
int no;
struct List *next;
}*start=NULL,*link=NULL,*temp=NULL;
void insert()
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
printf("Enter Value: ");
scanf("%d",&temp->no);
temp->next=NULL;
if(start == NULL)
start=temp;
else
{
link=start;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
printf("\nItem is Inserted at Last");
}
}
void delet()
{
if(start==NULL)
printf("\nList is Empty");
else if(start->next==NULL)
{
printf("\nThe Deleted Item is:");
printf("\n %d",start->no);
free(start);
start=NULL;
}
else
{
temp=start;
start=start->next;
printf("\nThe Deleted Item is:");
printf("\n %d",temp->no);
free(temp);
}
}
void view()
{
if(start==NULL)
printf("\nList is Empty");
else
{
printf("\nThe List is:");
link=start;
while(link!=NULL)
{
printf("\n %d",link->no);
link=link->next;
}
}
}
void main()
{
int ch,value,i;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
insert();
getch();
break;
case 2:
delet();
getch();
break;
case 3:
view();
getch();
}
}while(ch!=4);
}
//(b) Write a program to create a singly linked list in FIFO fashion.
//Singly Linked-List in FIFO fashion.
#include<stdio.h>
struct List
{
int no;
struct List *next;
}*start=NULL,*link=NULL,*temp=NULL;
void insert()
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
printf("Enter Value: ");
scanf("%d",&temp->no);
temp->next=NULL;
if(start == NULL)
start=temp;
else
{
link=start;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
printf("\nItem is Inserted at Last");
}
}
void delet()
{
if(start==NULL)
printf("\nList is Empty");
else if(start->next==NULL)
{
printf("\nThe Deleted Item is:");
printf("\n %d",start->no);
free(start);
start=NULL;
}
else
{
temp=start;
start=start->next;
printf("\nThe Deleted Item is:");
printf("\n %d",temp->no);
free(temp);
}
}
void view()
{
if(start==NULL)
printf("\nList is Empty");
else
{
printf("\nThe List is:");
link=start;
while(link!=NULL)
{
printf("\n %d",link->no);
link=link->next;
}
}
}
void main()
{
int ch,value,i;
clrscr();
do
{
do
{
clrscr();
printf("\n1. Insert\n2. Delete \n3. View\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
insert();
getch();
break;
case 2:
delet();
getch();
break;
case 3:
view();
getch();
}
}while(ch!=4);
}
//(c) Write a program to create a sorted singly linked list.
//Sorting in Linked-List
#include<stdio.h>
struct list
{
int no;
struct list *next;
}*start=NULL,*link=NULL,*temp=NULL;
void create()
{
temp=(struct list *) malloc(sizeof(struct list));
if(temp==NULL)
printf("\nNo sufficient Memory !!!");
else
{
printf("Enter No.: ");
scanf("%d",&temp->no);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
link=start;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
}
void asort(int n)
{
for(;n>0;n--)
{
link=start;
while(link->next!=NULL)
{
if(link->no > link->next->no)
{
link->no = link->no + link->next->no;
link->next->no = link->no - link->next->no;
link->no = link->no - link->next->no;
}
link=link->next;
}
}
}
void dsort(int n)
{
for(;n>0;n--)
{
link=start;
while(link->next!=NULL)
{
if(link->no < link->next->no)
{
link->no = link->no + link->next->no;
link->next->no = link->no - link->next->no;
link->no = link->no - link->next->no;
}
link=link->next;
}
}
}
void trav()
{
link=start;
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
void main()
{
int n,i;
clrscr();
do
{
printf("How many nodes you want to create? ");
scanf("%d",&n);
}while(n<0);
for(i=0;i<n;i++)
{
create();
}
if(start==NULL)
printf("\nLinked-List is Empty");
else
{
printf("\nThe Linked-List is:\n");
trav();
asort(n);
printf("\nThe Ascending Order:\n");
trav();
dsort(n);
printf("\nThe Descending Order:\n");
trav();
}
getch();
}
//(d) Cursor Implementation (Array implementation ) Of Linked List.
//Cursor Implementation (Array Implementation) of Linked-List
#include<stdio.h>
struct list
{
int no;
struct list *next;
}*start=NULL,*link=NULL,*temp=NULL;
void create(int *p,int n)
{
int i;
for(i=0;i<n;i++)
{
temp=(struct list *) malloc(sizeof(struct list));
if(temp==NULL)
printf("\nNo sufficient Memory !!!");
else
{
temp->no=*(p+i);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
link=start;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
}
}
void trav()
{
if(start==NULL)
printf("\nLinked-List is Empty");
else
{
printf("The Linked-List is:\n");
link=start;
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
}
void main()
{
int n,i;
int *p;
clrscr();
do
{
printf("How many elements you want to insert in Array? ");
scanf("%d",&n);
}while(n<0);
p=(int *) calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
printf("Enter element: ");
scanf("%d",p+i);
}
create(p,n);
trav();
getch();
}
//
////Perform concate, Union and Intersection Operations on Two Singly Linked-List
#include<stdio.h>
struct List
{
int no;
struct List *next;
}*L1start=NULL,*L2start=NULL,*link=NULL,*temp=NULL;
void insert(int l)
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo Sufficient Memory!!!");
else
{
printf("\nEnter ID: ");
scanf("%d",&temp->no);
temp->next=NULL;
if(l==1)
{
if(L1start==NULL)
L1start=temp;
else
{
link=L1start;
while(link->next!=NULL)
link=link->next;
link->next=temp;
}
printf("\nNode is Inserted at Last in List1");
}
else
{
if(L2start==NULL)
L2start=temp;
else
{
link=L2start;
while(link->next!=NULL)
link=link->next;
link->next=temp;
}
printf("\nNode is Inserted at Last in List2");
}
}
}
void delet(int l)
{
if(l==1)
{
if(L1start==NULL)
printf("\nLinked-List1 is Empty");
else
{
link=L1start;
while(link->next!=NULL)
{
temp=link;
link=link->next;
}
printf("\nDeleted Item is: %d",link->no);
if(link==L1start)
L1start=NULL;
else
temp->next=NULL;
free(link);
printf("\nLast Node is Deleted from List1");
}
}
else
{
if(L2start==NULL)
printf("\nLinked-List2 is Empty");
else
{
link=L2start;
while(link->next!=NULL)
{
temp=link;
link=link->next;
}
printf("\nDeleted Item is: %d",link->no);
if(link==L2start)
L2start=NULL;
else
temp->next=NULL;
free(link);
printf("\nLast Node is Deleted from List2");
}
}
}
void traverse()
{
if(L1start==NULL)
printf("\nLinked-List1 is Empty");
else
{
link=L1start;
printf("\nLinked-List1: ");
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
if(L2start==NULL)
printf("\nLinked-List2 is Empty");
else
{
link=L2start;
printf("\nLinked-List2: ");
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
}
void concate(int t)
{
struct List *trav=NULL;
if(t==1)
{
link=L2start;
while(link!=NULL)
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo Sufficient Memory!!!");
else
{
temp->no=link->no;
temp->next=NULL;
if(L1start==NULL)
L1start=temp;
else
{
trav=L1start;
while(trav->next!=NULL)
trav=trav->next;
trav->next=temp;
}
}
link=link->next;
}
}
else
{
link=L1start;
while(link!=NULL)
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo Sufficient Memory!!!");
else
{
temp->no=link->no;
temp->next=NULL;
if(L2start==NULL)
L2start=temp;
else
{
trav=L2start;
while(trav->next!=NULL)
trav=trav->next;
trav->next=temp;
}
}
link=link->next;
}
}
traverse();
}
void Union()
{
struct List *unionfst=NULL,*trav=NULL;
if(L1start!=NULL)
{
link=L1start;
while(link!=NULL)
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo Sufficient Memory!!!");
else
{
temp->no=link->no;
temp->next=NULL;
if(unionfst==NULL)
unionfst=temp;
else
{
trav=unionfst;
while(trav->next!=NULL)
{
if(trav->no!=temp->no)
trav=trav->next;
else
break;
}
if(trav->next==NULL && trav->no!=temp->no)
trav->next=temp;
}
}
link=link->next;
}
}
if(L2start!=NULL)
{
link=L2start;
while(link!=NULL)
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo Sufficient Memory!!!");
else
{
temp->no=link->no;
temp->next=NULL;
if(unionfst==NULL)
unionfst=temp;
else
{
trav=unionfst;
while(trav->next!=NULL)
{
if(trav->no!=temp->no)
trav=trav->next;
else
break;
}
if(trav->next==NULL && trav->no!=temp->no)
trav->next=temp;
}
}
link=link->next;
}
}
if(unionfst==NULL)
printf("\nUnion of Both Linked-List is Empty");
else
{
link=unionfst;
printf("\nUnion of Both Linked-List: ");
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
}
void intersect()
{
struct List *interfst=NULL,*trav1=NULL,*trav2=NULL;
if(L1start!=NULL && L2start!=NULL)
{
link=L1start;
while(link!=NULL)
{
trav1=L2start;
while(trav1!=NULL)
{
if(link->no==trav1->no)
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo Sufficient Memory!!!");
else
{
temp->no=link->no;
temp->next=NULL;
if(interfst==NULL)
interfst=temp;
else
{
trav2=interfst;
while(trav2->next!=NULL)
{
if(trav2->no!=temp->no)
trav2=trav2->next;
else
break;
}
if(trav2->next==NULL && trav2->no!=temp->no)
trav2->next=temp;
}
}
break;
}
else
trav1=trav1->next;
}
link=link->next;
}
}
if(interfst==NULL)
printf("\nIntersection of Both Linked-List is Empty");
else
{
link=interfst;
printf("\nIntersection of Both Linked-List: ");
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
}
int choice()
{
int L;
do
{
printf("You want perform the operation on\n");
printf("1. List1\n2. List2\nEnter Your Chice: ");
scanf("%d",&L);
}while(L<1 || L>2);
return L;
}
void main()
{
int ch,v,t;
do
{ do
{ clrscr();
printf("\n1. Insert\n2. Delete\n3. Concatenation\n4. Union");
printf("\n5. Intersection\n6. Traversal\n7. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>7);
switch(ch)
{ case 1:
insert(choice());
getch();
break;
case 2:
delet(choice());
getch();
break;
case 3:
do
{
printf("Concate\n");
printf("1. List1 before List2\n2. List2 before List1");
printf("\nEnter Your Chice: ");
scanf("%d",&t);
}while(t<1 || t>2);
concate(t);
getch();
break;
case 4:
Union();
getch();
break;
case 5:
intersect();
getch();
break;
case 6:
traverse();
getch();
break;
}
}while(ch!=7);
}
//Perform some Operations on Singly Linked-List
#include<stdio.h>
struct List
{
int no;
struct List *next;
}*start=NULL,*link=NULL,*temp=NULL;
void insert()
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
printf("\nNo Sufficient Memory!!!");
else
{
printf("\nEnter ID: ");
scanf("%d",&temp->no);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
link=start;
while(link->next!=NULL)
link=link->next;
link->next=temp;
}
printf("\nNode is Inserted at Last");
}
}
void delet()
{
if(start==NULL)
printf("\nLinked-List is Empty");
else
{
link=start;
while(link->next!=NULL)
{
temp=link;
link=link->next;
}
printf("\nDeleted Item is: %d",link->no);
if(link==start)
start=NULL;
else
temp->next=NULL;
free(link);
printf("\nLast Node is Deleted");
}
}
void sum()
{
if(start==NULL)
printf("\nLinked-List is Empty");
else
{
int s=0;
link=start;
while(link!=NULL)
{
s=s+link->no;
link=link->next;
}
printf("\nSum of Elements of the List is %d",s);
}
}
void count_node()
{
int n=0;
link=start;
while(link!=NULL)
{
n++;
link=link->next;
}
printf("\nTotal no. of nodes in the List are %d",n);
}
void search(int val)
{
if(start==NULL)
printf("\nLinked-List is Empty");
else
{
link=start;
while(link!=NULL)
{
if(link->no==val)
{
printf("\nThe Number is Exist in the List");
break;
}
link=link->next;
}
if(link==NULL)
printf("\nThe Number is not Exist in the List");
}
}
void reverse_LL()
{
struct List *fst,*trav=NULL;
if(start==NULL)
{
printf("\nLinked-List is Empty");
return;
}
else if(start->next!=NULL)
{
fst=link=start;
start=NULL;
while(link->next!=NULL)
{
while(link->next->next!=NULL)
{
link=link->next;
}
if(start==NULL)
{
start=link->next;
link->next=NULL;
}
else
{
trav=start;
while(trav->next!=NULL)
trav=trav->next;
trav->next=link->next;
link->next=NULL;
}
link=fst;
}
if(trav!=NULL)
{
trav->next->next=fst;
fst->next=NULL;
}
else
{
start->next=link;
link->next=NULL;
}
}
printf("\nLinked-List is Reversed");
link=start;
printf("\nLinked-List: ");
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
void copy_LL()
{
struct List *first=NULL,*trav=NULL;
if(start==NULL)
printf("\nLinked-List is Empty");
else
{
link=start;
while(link!=NULL)
{
temp=(struct List *) malloc(sizeof(struct List));
if(temp == NULL)
{ printf("\nNo Sufficient Memory!!!");
break;
}
else
{
temp->no=link->no;
temp->next=NULL;
if(first==NULL)
first=temp;
else
{
trav=first;
while(trav->next!=NULL)
trav=trav->next;
trav->next=temp;
}
}
link=link->next;
}
printf("\nLinked-List is copied");
trav=first;
while(trav!=NULL)
{
printf("\n%d",trav->no);
temp=trav;
trav=trav->next;
free(temp);
}
}
}
void traverse()
{
if(start==NULL)
printf("\nLinked-List is Empty");
else
{
link=start;
printf("\nLinked-List: ");
while(link!=NULL)
{
printf(" %d",link->no);
link=link->next;
}
}
}
void main()
{
int ch,v;
do
{ do
{ clrscr();
printf("\n1. Insert\n2. Delete\n3. Sum of elements of the List");
printf("\n4. No. of Nodes\n5. Searching\n");
printf("6. Reverse the List\n7. Copy the List");
printf("\n8. Traversal\n9. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>9);
switch(ch)
{ case 1:
insert();
getch();
break;
case 2:
delet();
getch();
break;
case 3:
sum();
getch();
break;
case 4:
count_node();
getch();
break;
case 5:
printf("Enter the Value: ");
scanf("%d",&v);
search(v);
getch();
break;
case 6:
reverse_LL();
getch();
break;
case 7:
copy_LL();
getch();
break;
case 8:
traverse();
getch();
}
}while(ch!=9);
}
10. Write a program to add two polynomials in two variables....
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Add Two Polynomial Equation
#include<stdio.h>
struct pol_eq
{
int coef,exp;
struct pol_eq *next;
}*fst1=NULL,*fst2=NULL,*link=NULL,*temp=NULL;
int insert(int e)
{
int pre_ex=10,co,ex;
do
{
printf("Enter Co-efficient: ");
scanf("%d",&co);
printf("Enter Exponent: ");
scanf("%d",&ex);
if(pre_ex>ex)
{
if(co!=0)
{
pre_ex=ex;
temp=(struct pol_eq *) malloc(sizeof(struct pol_eq));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
temp->coef=co;
temp->exp=ex;
temp->next=NULL;
if(e==1)
{
if(fst1 == NULL)
fst1=temp;
else
{
link=fst1;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
else if(e==2)
{
if(fst2 == NULL)
fst2=temp;
else
{
link=fst2;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
}
}
}
else
{
if(e==1)
{
if(fst1!=NULL)
{
while(fst1->next!=NULL)
{
link=fst1;
fst1=fst1->next;
free(link);
}
free(fst1);
fst1=NULL;
}
}
else if(e==2)
{
if(fst2!=NULL)
{
while(fst2->next!=NULL)
{
link=fst2;
fst1=fst1->next;
free(link);
}
free(fst2);
fst2=NULL;
}
}
return 0;
}
}while(ex>0);
return 1;
}
void show(int e)
{
if(e==1)
{
if(fst1==NULL)
printf("\nFirst Equation is 0");
else
{
printf("\nThe First Equation: ");
if(fst1->coef > 1)
printf(" %d",fst1->coef);
else if(fst1->coef < -1)
printf("- %d",fst1->coef * -1);
else if(fst2->coef == 1)
printf("+ ");
else if(fst2->coef == -1)
printf("- ");
if(fst1->exp != 1 && fst1->exp != 0)
printf("x^%d ",fst1->exp);
else if(fst1->exp == 1)
printf("x ");
link=fst1->next;
while(link!=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
}
else if(e==2)
{
if(fst2==NULL)
printf("\nSecond Equation is 0");
else
{
printf("\nThe Second Equation: ");
if(fst2->coef > 1)
printf(" %d",fst2->coef);
else if(fst2->coef < -1)
printf("- %d",fst2->coef * -1);
else if(fst2->coef == 1)
printf("+ ");
else if(fst2->coef == -1)
printf("- ");
if(fst2->exp != 1 && fst2->exp != 0)
printf("x^%d ",fst2->exp);
else if(fst2->exp == 1)
printf("x ");
link=fst2->next;
while(link!=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
}
}
void add()
{
int c;
link=fst1;
temp=fst2;
printf("\nThe Addition: ");
while(link!=NULL && temp!=NULL)
{
if(link->exp == temp->exp)
{
c=link->coef + temp->coef;
if(c != 0)
{
if(c > 1)
printf("+ %d",c);
else if(c < -1)
printf("- %d",c * -1);
else if(c == 1)
printf("+ ");
else if(c == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
}
link=link->next;
temp=temp->next;
}
else if(link->exp > temp->exp)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
else
{
if(temp->coef > 1)
printf("+ %d",temp->coef);
else if(temp->coef < -1)
printf("- %d",temp->coef * -1);
else if(temp->coef == 1)
printf("+ ");
else if(temp->coef == -1)
printf("- ");
if(temp->exp != 1 && temp->exp != 0)
printf("x^%d ",temp->exp);
else if(temp->exp == 1)
printf("x ");
temp=temp->next;
}
}
if(link!=NULL)
{
while(link !=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
else
{
while(temp != NULL)
{
if(temp->coef > 1)
printf("+ %d",temp->coef);
else if(temp->coef < -1)
printf("- %d",temp->coef * -1);
else if(temp->coef == 1)
printf("+ ");
else if(temp->coef == -1)
printf("- ");
if(temp->exp != 1 && temp->exp != 0)
printf("x^%d ",temp->exp);
else if(temp->exp == 1)
printf("x ");
temp=temp->next;
}
}
}
void main()
{
int a;
clrscr();
do
{
a=insert(1);
if(a==0)
{
printf("Invalid Equation\n");
getch();
clrscr();
}
}while(a==0);
printf("First Equation is Inserted\n");
getch();
clrscr();
do
{
a=insert(2);
if(a==0)
{
printf("Invalid Equation\n");
getch();
clrscr();
}
}while(a==0);
printf("Second Equation is Inserted\n");
getch();
clrscr();
show(1);
show(2);
add();
getch();
}
11. Write a program to subtract two polynomials in two variables....
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Subtract Two Polynomial Equation
#include<stdio.h>
struct pol_eq
{
int coef,exp;
struct pol_eq *next;
}*fst1=NULL,*fst2=NULL,*link=NULL,*temp=NULL;
int insert(int e)
{
int pre_ex=10,co,ex;
do
{
printf("Enter Co-efficient: ");
scanf("%d",&co);
printf("Enter Exponent: ");
scanf("%d",&ex);
if(pre_ex>ex)
{
if(co!=0)
{
pre_ex=ex;
temp=(struct pol_eq *) malloc(sizeof(struct pol_eq));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
temp->coef=co;
temp->exp=ex;
temp->next=NULL;
if(e==1)
{
if(fst1 == NULL)
fst1=temp;
else
{
link=fst1;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
else if(e==2)
{
if(fst2 == NULL)
fst2=temp;
else
{
link=fst2;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
}
}
}
else
{
if(e==1)
{
if(fst1!=NULL)
{
while(fst1->next!=NULL)
{
link=fst1;
fst1=fst1->next;
free(link);
}
free(fst1);
fst1=NULL;
}
}
else if(e==2)
{
if(fst2!=NULL)
{
while(fst2->next!=NULL)
{
link=fst2;
fst1=fst1->next;
free(link);
}
free(fst2);
fst2=NULL;
}
}
return 0;
}
}while(ex>0);
return 1;
}
void show(int e)
{
if(e==1)
{
if(fst1==NULL)
printf("\nFirst Equation is 0");
else
{
printf("\nThe First Equation: ");
if(fst1->coef > 1)
printf(" %d",fst1->coef);
else if(fst1->coef < -1)
printf("- %d",fst1->coef * -1);
else if(fst2->coef == 1)
printf("+ ");
else if(fst2->coef == -1)
printf("- ");
if(fst1->exp != 1 && fst1->exp != 0)
printf("x^%d ",fst1->exp);
else if(fst1->exp == 1)
printf("x ");
link=fst1->next;
while(link!=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
}
else if(e==2)
{
if(fst2==NULL)
printf("\nSecond Equation is 0");
else
{
printf("\nThe Second Equation: ");
if(fst2->coef > 1)
printf(" %d",fst2->coef);
else if(fst2->coef < -1)
printf("- %d",fst2->coef * -1);
else if(fst2->coef == 1)
printf("+ ");
else if(fst2->coef == -1)
printf("- ");
if(fst2->exp != 1 && fst2->exp != 0)
printf("x^%d ",fst2->exp);
else if(fst2->exp == 1)
printf("x ");
link=fst2->next;
while(link!=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
}
}
void subtract()
{
int c;
link=fst1;
temp=fst2;
printf("\nThe Addition: ");
while(link!=NULL && temp!=NULL)
{
if(link->exp == temp->exp)
{
c=link->coef - temp->coef;
if(c != 0)
{
if(c > 1)
printf("+ %d",c);
else if(c < -1)
printf("- %d",c * -1);
else if(c == 1)
printf("+ ");
else if(c == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
}
link=link->next;
temp=temp->next;
}
else if(link->exp > temp->exp)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
else
{
if(temp->coef > 1)
printf("+ %d",temp->coef);
else if(temp->coef < -1)
printf("- %d",temp->coef * -1);
else if(temp->coef == 1)
printf("+ ");
else if(temp->coef == -1)
printf("- ");
if(temp->exp != 1 && temp->exp != 0)
printf("x^%d ",temp->exp);
else if(temp->exp == 1)
printf("x ");
temp=temp->next;
}
}
if(link!=NULL)
{
while(link !=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
else
{
while(temp != NULL)
{
if(temp->coef > 1)
printf("+ %d",temp->coef);
else if(temp->coef < -1)
printf("- %d",temp->coef * -1);
else if(temp->coef == 1)
printf("+ ");
else if(temp->coef == -1)
printf("- ");
if(temp->exp != 1 && temp->exp != 0)
printf("x^%d ",temp->exp);
else if(temp->exp == 1)
printf("x ");
temp=temp->next;
}
}
}
void main()
{
int a;
clrscr();
do
{
a=insert(1);
if(a==0)
{
printf("Invalid Equation\n");
getch();
clrscr();
}
}while(a==0);
printf("First Equation is Inserted\n");
getch();
clrscr();
do
{
a=insert(2);
if(a==0)
{
printf("Invalid Equation\n");
getch();
clrscr();
}
}while(a==0);
printf("Second Equation is Inserted\n");
getch();
clrscr();
show(1);
show(2);
subtract();
getch();
}
12. Write a program to multiply two polynomials in two variables....
Shared By :Khushbu Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Write a Program to Multiply Two Polynomials in two variables
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
struct polymulti
{
char sign;
int coeff;
int expo;
struct polymulti *next;
}*temp=NULL,*first1=NULL,*first2=NULL,*first3=NULL,*trav1=NULL,*trav2=NULL;
struct polymulti * insert(struct polymulti *fst)
{
int node,i;
clrscr();
printf("\nenter total node:");
scanf("%d",&node);
for(i=0;i<node;i++)
{
trav1=fst;
temp=(struct polymulti *)malloc(sizeof(struct polymulti));
flushall();
printf("\nenter sign:");
scanf("%c",&temp->sign);
printf("\nenter coefficienct:");
scanf("%d",&temp->coeff);
printf("\nenter exponent:");
scanf("%d",&temp->expo);
temp->next=NULL;
if(fst==NULL)
{
fst=temp;
trav1=temp;
}
else
{
trav1=fst;
while(trav1->next!=NULL)
trav1=trav1->next;
trav1->next=temp;
temp->next=NULL;
}
}
return fst;
}
void display(struct polymulti *fst)
{
struct polymulti *trv;
trv=fst;
while(trv!=NULL)
{
if(trv->expo==0)
printf("%c %d",trv->sign,trv->coeff);
else
printf("%c %d%c^%d ",trv->sign,trv->coeff,'x',trv->expo);
trv=trv->next;
}
printf("\n");
}
void multiplication()
{
int n1,n2;
struct polymulti *trv;
n1=1;
n2=1;
trav1=first1;
trav2=first2;
if(trav1==NULL && trav2==NULL)
{
printf("\nlist does't exist");
}
else
{
while(trav1!=NULL && trav2!=NULL)
{
temp=(struct polymulti *)malloc(sizeof(struct polymulti));
temp->next=NULL;
if((trav1->sign=='-' && trav2->sign=='-')||(trav1->sign=='+' && trav2->sign=='+'))
{
temp->sign='+';
temp->coeff=trav1->coeff*trav2->coeff;
temp->expo=trav1->expo+trav2->expo;
}
else
{
temp->sign='-';
temp->coeff=trav1->coeff*trav2->coeff;
temp->expo=trav1->expo+trav2->expo;
}
if(first3==NULL)
{
first3=temp;
trv=temp;
}
else
{
trv=first3;
while(trv->next!=NULL)
{
trv=trv->next;
}
trv->next=temp;
temp->next=NULL;
}
trav1=trav1->next;
trav2=trav2->next;
}
if(trav1==NULL)
{
while(trav2!=NULL)
{
temp=(struct polymulti *)malloc(sizeof(struct polymulti));
temp->sign=trav2->sign;
temp->coeff=trav2->coeff;
temp->expo=trav2->expo;
temp->next=NULL;
trv=first3;
while(trv->next!=NULL)
{
trv=trv->next;
}
trv->next=temp;
temp->next=NULL;
trav2=trav2->next;
}
}
else if(trav2==NULL)
{
while(trav1!=NULL)
{
temp=(struct polymulti *)malloc(sizeof(struct polymulti));
temp->sign=trav1->sign;
temp->coeff=trav1->coeff;
temp->expo=trav1->expo;
temp->next=NULL;
trv=first3;
while(trv->next!=NULL)
{
trv=trv->next;
}
trv->next=temp;
temp->next=NULL;
trav1=trav1->next;
}
}
display(first3);
}
}
void main()
{
clrscr();
first1=insert(first1);
display(first1);
first2=insert(first2);
display(first1);
display(first2);
multiplication();
getch();
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Multiply Two Polynomial Equation
#include<stdio.h>
struct pol_eq
{
int coef,exp;
struct pol_eq *next;
}*fst1=NULL,*fst2=NULL,*link=NULL,*temp=NULL;
int insert(int e)
{
int pre_ex=10,co,ex;
do
{
printf("Enter Co-efficient: ");
scanf("%d",&co);
printf("Enter Exponent: ");
scanf("%d",&ex);
if(pre_ex>ex)
{
if(co!=0)
{
pre_ex=ex;
temp=(struct pol_eq *) malloc(sizeof(struct pol_eq));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
temp->coef=co;
temp->exp=ex;
temp->next=NULL;
if(e==1)
{
if(fst1 == NULL)
fst1=temp;
else
{
link=fst1;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
else if(e==2)
{
if(fst2 == NULL)
fst2=temp;
else
{
link=fst2;
while(link->next!=NULL)
{
link=link->next;
}
link->next=temp;
}
}
}
}
}
else
{
if(e==1)
{
if(fst1!=NULL)
{
while(fst1->next!=NULL)
{
link=fst1;
fst1=fst1->next;
free(link);
}
free(fst1);
fst1=NULL;
}
}
else if(e==2)
{
if(fst2!=NULL)
{
while(fst2->next!=NULL)
{
link=fst2;
fst1=fst1->next;
free(link);
}
free(fst2);
fst2=NULL;
}
}
return 0;
}
}while(ex>0);
return 1;
}
void show(int e)
{
if(e==1)
{
if(fst1==NULL)
printf("\nFirst Equation is 0");
else
{
printf("\nThe First Equation: ");
if(fst1->coef > 1)
printf(" %d",fst1->coef);
else if(fst1->coef < -1)
printf("- %d",fst1->coef * -1);
else if(fst2->coef == 1)
printf("+ ");
else if(fst2->coef == -1)
printf("- ");
if(fst1->exp != 1 && fst1->exp != 0)
printf("x^%d ",fst1->exp);
else if(fst1->exp == 1)
printf("x ");
link=fst1->next;
while(link!=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
}
else if(e==2)
{
if(fst2==NULL)
printf("\nSecond Equation is 0");
else
{
printf("\nThe Second Equation: ");
if(fst2->coef > 1)
printf(" %d",fst2->coef);
else if(fst2->coef < -1)
printf("- %d",fst2->coef * -1);
else if(fst2->coef == 1)
printf("+ ");
else if(fst2->coef == -1)
printf("- ");
if(fst2->exp != 1 && fst2->exp != 0)
printf("x^%d ",fst2->exp);
else if(fst2->exp == 1)
printf("x ");
link=fst2->next;
while(link!=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
}
}
void mul()
{
struct pol_eq *start=NULL,*link2=NULL,*link3=NULL;
int c,e;
link=fst1;
while(link!=NULL)
{
link2=fst2;
while(link2 != NULL)
{
c=link->coef * link2->coef;
e=link->exp + link2->exp;
if(start==NULL)
{
temp=(struct pol_eq *) malloc(sizeof(struct pol_eq));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
temp->coef=c;
temp->exp=e;
temp->next=NULL;
start=temp;
}
}
else
{
link3=start;
while(link3->next!=NULL)
{
if(link3->exp == e)
{
link3->coef +=c;
break;
}
else
link3=link3->next;
}
if(link3->next == NULL && link3->exp == e)
{
link3->coef +=c;
}
else if(link3->next == NULL && link3->exp != e)
{
temp=(struct pol_eq *) malloc(sizeof(struct pol_eq));
if(temp == NULL)
printf("\nNo sufficient Memory");
else
{
temp->coef=c;
temp->exp=e;
temp->next=NULL;
link3=start;
while(link3->next!=NULL && link3->next->exp > e)
{
link3=link3->next;
}
temp->next=link3->next;
link3->next=temp;
}
}
}
link2=link2->next;
}
link = link->next;
}
printf("\n\nThe Multiplication: ");
link=start;
while(link!=NULL)
{
if(link->coef > 1)
printf("+ %d",link->coef);
else if(link->coef < -1)
printf("- %d",link->coef * -1);
else if(link->coef == 1)
printf("+ ");
else if(link->coef == -1)
printf("- ");
if(link->exp != 1 && link->exp != 0)
printf("x^%d ",link->exp);
else if(link->exp == 1)
printf("x ");
link=link->next;
}
}
void main()
{
int a;
clrscr();
do
{
a=insert(1);
if(a==0)
{
printf("Invalid Equation\n");
getch();
clrscr();
}
}while(a==0);
printf("First Equation is Inserted\n");
getch();
clrscr();
do
{
a=insert(2);
if(a==0)
{
printf("Invalid Equation\n");
getch();
clrscr();
}
}while(a==0);
printf("Second Equation is Inserted\n");
getch();
clrscr();
show(1);
show(2);
mul();
getch();
}