Sie sind auf Seite 1von 51

Date of Exp: …………... Exp. No.

1
Faculty Signature………. Roll No. ………………….

AIM: Program to Insert element in Array

ALGORITHM:
Here LA is linear array with N elements and K is the positive integer such that K<=N. This algorithm inserts an
element ITEM at the Kth position in array.

1. Set J=:N.
2. Repeat steps 3 and 4 while J>=K.
3. Set LA[J+1]:=LA[J].
4. SET:=J-1.
5. Set LA[K]:=ITEM.
6. Set N:+N+1.
7. Exit.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>

void main()
{
int j,k,n,p,a[10];
clrscr();
printf(" Enter number of elements to be inserted: ");
scanf("%d",&n);
printf("\n Enter the elements of array: ");
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
}
printf("\n Enter the new element to be inserted: ");
scanf("%d",&k);
printf("\n Enter the index position of new element: ");
scanf("%d",&p);
for(j=n;j>p;j--)
{
a[j]=a[j-1];
}
a[p]=k;
n++;
printf("Enter altered array is : ");
for(j=0;j<n;j++)
{
printf("\n %d",a[j]);
}
getch();
}

OUTPUT
Date of Exp: …………... Exp. No. 2
Faculty Signature………. Roll No. ……………….…

AIM: Program to Delete element from Array

ALGORITHM:
Hera a LA is linear array with N elements and k IS THE POSITIVE integer such that K<=N. This algorithm
deletes the Kth element from LA.
1. Set ITEM:=LA[K].
2. Repeat for J:=KtoN-1:
Set LA[J]:=LA[J+1]
3. Set N=N-1.
4. Exit.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
void main()

{
int i,n,p,a[10];
clrscr();
printf("\n Enter number of elements: ");
scanf("%d",&n);
printf("\n Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter position of the element to be deleted: ");
scanf("%d",&p);
p--;
for(i=p;i<n-1;i++)
{
a[i]=a[i+1];
}
printf("\n Altered array:\n");
for(i=0;i<n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}
OUTPUT
Date of Exp: …………... Exp. No. 3
Faculty Signature………. Roll No. …………….………

AIM: Program to Remove Duplicate Values in Array

ALGORITHM:

1. Create an auxiliary array temp[] to store unique elements.


2. Traverse input array and one by one copy unique elements of arr[] to temp[]. Also keep track of count of
unique elements. Let this count be j.
3. Copy j elements from temp[] to arr[] and return j

PROGRAM

CODING

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,k,a[10];
clrscr();
printf("\nEnter the number of elements: ");
scanf("%d",&n);
printf("\n Enter the elements of array: \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n-1;k++)
{
a[k]=a[k+1];
}
--n;
--j;
}
}
}

printf("\nAltered array: \n");


for(i=0;i<n;i++)
printf("%d\n",a[i]);

getch();
}

OUTPUT
Date of Exp: …………... Exp. No. 4
Faculty Signature………. Roll No. ……………..……

AIM: Program for Matrix Addition, Subtraction, Multiplication & Transpose

ALGORITHM:

MATMUL(A,B,C,M,P,N)
Let A be an M*P matrix array and B be a P*N matrix array. This algorithm stores the product of A and B in an
M*N matrix array C.
1. Repeat steps 2 to 4 for I= 1 to M
2. Repeat Steps 3 and 4 for J =1 to N
3. Set C [I,J] = 0
4. Repeat for K = 1 to P :
1. C [I,J] = C [I,J] + A [I,K] * B [K,J]
5. Exit.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>

void main()
{int i,j,k,ch,r,c,x[5][5],y[5][5],z[5][5];
clrscr();
printf("\nEnter the number of rows : ");
scanf("%d",&r);
printf("Enter the number of columns : ");
scanf("%d",&c);
printf("\nEnter the first matrix \n ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&x[i][j]);

}
printf("\nEnter second matrix \n") ;
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{ scanf("%d",&y[i][j]);
}

}
printf("\tMENU");
printf("\n1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Transpose");

printf("\nEnter your choice : ");


scanf("%d",&ch);

switch(ch)
{
case 1 : { printf("\nSum of the two matrix is :\n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
printf("%d\t",z[i][j]=x[i][j]+y[i][j]);
}
printf("\n");
}
} break;

case 2 : { printf("Subtraction of the two matrix is :\n");


for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
printf("%d\t",z[i][j]=x[i][j]-y[i][j]);
}
printf("\n");
}
} break;
case 3 : { printf("Product of the two matrix is :\n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{ z[i][j]=0;
for(k=0;k<r;k++)
{
z[i][j]=z[i][j]+(x[i][j]*y[i][j]);
}
printf("%d\t",z[i][j]);
}
printf("\n");
}
} break;

case 4 : { printf("Transpose of the first matrix is :\n");


for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
printf("%d\t",z[i][j]=x[i][j]-y[i][j]);
}
printf("\n");
}
}
}
getch();

OUTPUT
Date of Exp: …………... Exp. No. 5
Faculty Signature………. Roll No. ………….………

AIM: Implement Sparse Matrices using 3-Tuple Notation

ALGORITHM:

1. The array A is of length NNZ and holds all the nonzero entries of M in left-to-right top-to-bottom ("row-
major") order.
2. The array IA is of length m + 1. It is defined by this recursive definition:
3. IA[0] = 0
4. IA[i] = IA[i − 1] + (number of nonzero elements on the (i-1)-th row in the original matrix)
5. Thus, the first m elements of IA store the index into A of the first nonzero element in each row of M, and
the last element IA[m] stores NNZ, the number of elements in A, which can be also thought of as the index
in A of first element of a phantom row just beyond the end of the matrix M. The values of the i-th row of
the original matrix is read from the elements A[IA[i]] to A[IA[i + 1] − 1] (inclusive on both ends), i.e. from
the start of one row to the last index just before the start of the next.
6. The third array, JA, contains the column index in M of each element of A and hence is of length NNZ as
well.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
void display(int a[5][5],int r,int c);
void main()
{
int a[5][5]={0},r,c,i,j,ch;
clrscr();

printf("\nEnter number of rows : ");


scanf("%d",&r);
printf("Enter number of columns : ");
scanf("%d",&c);
printf("MENU");
printf("\n1.Upper Right Triangular Matrix");
printf("\n2.Upper Left Triangular Matrix");
printf("\n3.Lower Right Triangular Matrix");
printf("\n4.Lower Left Triangular Matrix");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{case 1 : printf("Enter the elements of the matrix\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(j>=i)
scanf("%d",&a[i][j]);
printf("Upper Right Triangular Matrix : \n");
display (a,r,c);
break;

case 2 : printf("Enter the elements of the matrix\n");


for(i=0;i<r;i++)
for(j=0;j<c-i;j++)
scanf("%d",&a[i][j]);
printf("Upper Left Triangular Matrix : \n");
display (a,r,c);
break;

case 3 : printf("Enter the elements of the matrix\n");


for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c-i-1;j++)
a[i][j]=0;
printf("Lower Right Triangular Matrix : \n");
display (a,r,c);
break;

case 4 : printf("Enter the elements of the matrix\n");


for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(j<=i)
scanf("%d",&a[i][j]);
printf("Lower Left Triangular Matrix : \n");
display (a,r,c);
break;

default : printf("Incorrect Choice Entered !!!");

getch();

void display(int a[5][5],int r,int c)


{ int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}

OUTPUT
Date of Exp: …………... Exp. No. 6
Faculty Signature………. Roll No. ……………..…

AIM: Program to perform Pop, Push & Traverse operations on Stack

ALGORITHM

PUSH(MAXSTK,TOP,STACK,ITEM)
This procedure pushes an item onto a stack.
1. If TOP=MAXSTK, print OVERFLOW and Return.
2. Set TOP=TOP+1.
3. Set STACK[TOP]=ITEM.
4. Return.

POP(STACK,TOP,ITEM)
This procedure deletes the top element of stack and assigns it to variable ITEM.
1. If TOP=0, print UNDERFLOW and Return.
2. Set ITEM=STACK[TOP].
3. Set TOP=TOP-1.
4. Return.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#define SIZE 10

int stack[SIZE];
int top=-1;
int overflow();
int underflow();

void push();
void pop();
void display();

void main()
{
int z;
char c='y';
clrscr();
while(c=='y'|| c=='Y')
{
printf("\n Press 1 for Push");
printf("\n Press 2 for Pop");
printf("\n Press 3 for Traverse");
printf("\n Enter your choice: ");
scanf("%d",&z);
switch(z)
{
case 1:
printf("\n Push Operation");
push();
break;
case 2:
printf("\n Pop operation");
pop();
break;
case 3:
printf("\n Traverse operation");
display();
break;
default:
printf("\n Wrong choice");
}
printf("\n Do you want to continue (Y/N): ");
fflush(stdin);
scanf("%c",&c);
}
getch();
}

int overflow()
{
if(top==SIZE-1)
{
printf("\n Stack overflow");
return 1;
}
else
return 0;
}

int underflow()
{
if(top==-1)
{
printf("\n Stack underflow");
return 1;
}
else
return 0;
}
void push()
{
int a;
if(!overflow())
{
printf("\n Enter the element to insert: ");
scanf("%d",&a);
top=top+1;
stack[top]=a;
}
}

void pop()
{
if(!underflow())
{
printf("\n Popped element is: %d",stack[top]);
top=+top-1;
}
}

void display()
{
int i;
if(!underflow())
{
printf("\n After traverse program elements are: ");
for(i=0;i<=top;i++)
{
printf("%d\t",stack[i]);
}
}
}
OUTPUT
Date of Exp: …………... Exp. No. 7
Faculty Signature………. Roll No. ………………

AIM: Program to Convert Infix Expression to Postfix Expression using Stack

ALGORITHM:

POLISH(Q,P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix
expression P.
1) Push “(“ onto stack and add “)” to the end of Q.
2) Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty.
3) If an operand is encountered, add it to P.
4) If a left parenthesis is encountered, push it into STACK.
5) If an operator (*) is encountered then:
a) Repeatedly pop from STACK and add it to P each operator on top of STACK which has the same
precedence than (*).
b) Add (*) to STACK.
6) If a right parenthesis is encountered then.
a) Repeatedly pop from STACK and add to P each operator on top of STACK until a left parenthesis is
encountered.
b) Remove the left parenthesis.
7) Exit.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>

char stack[50];
int top=-1;
void in_to_post(char infix[]);
void push (char);
char pop();

void main()
{
char infix[25];
clrscr();
printf("\n Enter infix expression: ");
gets(infix);
in_to_post(infix);
getch();
}
void push(char symb)
{
if(top>=49)
{
printf("\n Stack overflow!!!");
getch();
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}

char pop()
{
char item;
if(top==-1)
{
printf("\n Stack empty!!!");
getch();
return(0);
}
else
{
item=stack[top];
top--;
}
return item;
}

int preced(char ch)


{
switch(ch)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
}

void in_to_post(char infix[])


{
int length;
static int i=0,pos=0;
char symbol,temp;
char postfix[40];
length=strlen(infix);
push('(');
while(i<length)
{
symbol=infix[i];
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default:
postfix[pos++]=symbol;
break;
}
i++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
printf("\n Resultant postfix expression: ");
puts(postfix);
return;
}

OUTPUT
Date of Exp: …………... Exp. No. 8
Faculty Signature………. Roll No. ………………

AIM: Program to Convert Infix Expression to Prefix Expression using Stack

ALGORITHM:

REVERSE POLISH(Q,P)
1) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent prefix
expression P.
2) Reverse the set of string.
3) Push “(“ onto stack and add “)” to the end of Q.
4) Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty.
5) If an operand is encountered, add it to P.
6) If a left parenthesis is encountered, push it into STACK.
7) If an operator (*) is encountered then:
a) Repeatedly pop from STACK and add it to P each operator on top of STACK which has the same
precedence than (*).
b) Add (*) to STACK.
8) If a right parenthesis is encountered then.
a) Repeatedly pop from STACK and add to P each operator on top of STACK until a left parenthesis is
encountered.
b) Remove the left parenthesis.
9) Reverse the string again.
10) Exit.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define max 100

char st[max];
int top=-1;
void push(char st[],char);
char pop(char st[]);
void infixpostfix(char source[],char target[]);
int getpriorty(char);
void reverse(char str[]);
char infix[100],postfix[100],temp[100];
int main()
{
clrscr();
printf("\n Enter any infix expression:");
gets(infix);
reverse(infix);
strcpy(postfix," ");
infixpostfix(temp,postfix);
reverse(postfix);
printf("\n Prefix string:");
puts(temp);
getch();
return 0;
}

void reverse(char str[])


{
int len,i=0,j=0;
len=strlen(str);
j=len-1;
while(j>=0)
{
if(str[j]=='(')
temp[i]=')';
else if(str[j]==')')
temp[i]='(';
else
temp[i]=str[j];
i++;
j--;
}

temp[i]='\0';
}

int getpriority(char op)


{
if(op=='/'|| op=='*'|| op=='%')
return 1;
else if(op=='+'|| op=='-')
return 0;
}

void push(char st[],char val)


{
if(top==(max-1))
printf("\n Stack overflow!");
else
{
top=top+1;
st[top]=val;
}
}

char pop(char st[])


{
char val;
if(top==-1)
printf("\n Stack underflow:");
else
{
val=st[top];
top--;
}
return val;
}

void infixpostfix(char source[],char target[])


{
int i=0,j=0;
char temp;
strcpy(target,"");
while(source[i]!='\0')
{
if(source[i]=='(')
{
push(st,source[i]);
i++;
}
else if(source[i]==')')
{
while((top!=-1)&&(st[top]!='('))
{
target[j]=pop(st);
j++;
}
if(top==-1)
{
printf("\n Incorrect Expression!");
exit(1);
}
temp=pop(st);
i++;
}
else if(isdigit(source[i])|| isalpha(source[i]))
{
target[j]=source[i];
j++;
i++;
}
else if(source[i]=='+'|| source[i]=='-'||source[i]=='*'||source[i]=='/'||source[i]=='%')
{
while((top!=-1)&&(st[top]!='(')&&(getpriority(st[top]) >getpriority(source[i])))
{
target[j]=pop(st);
j++;
}
push(st,source[i]);
i++;
}
else
{
printf("\n Wrong expression!");
exit(1);
}
}

while((top!=-1)&&(st[top]!='('))
{
target[j]=pop(st);
j++;
}
target[j]='\0';
}

OUTPUT
Date of Exp: …………... Exp. No. 9
Faculty Signature………. Roll No. ………………

AIM: Program to Evaluate Postfix Expression using Stack

ALGORITHM:

This algorithm is to find the value of an arithmetic expression P within postfix notation.
1. Add a right parenthesis “)“ at the end of P.
2. Add P from left to right and repeat step 3 and 4 for each element of P until the sentinel “)” is encountered.
3. If an operand is encountered, put it in stack.
4. If an operator is encountered, then,
a. Remove the top two elements of stack where A is the top element and B is next to top element.
b. Evaluate B(*)A.
c. Place the result of(b) back on stack.
5. Set VALUE equal to the top element of stack.
6. Exit.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#include<math.h>

void evaluate();
void push(float f);
float pop();

char q[20];
float stack[20];
int top=-1;

void main()
{
clrscr();

printf("\n Enter postfix expression: ");


gets(q);
evaluate();
getch();
}
void evaluate()
{
float a,b,value[20];
int i;
for(i=0;q[i]!='\0';i++)
{
if(q[i]>=65 && q[i]<=92 || q[i]>=97 && q[i]<=121)
{
fflush(stdin);
printf("\n Enter the value of %c: ",q[i]);
scanf("%f",&value[i]);
}
}
for(i=0;i<q[i]!='\0';i++)
{
if(q[i]>=65 && q[i]<=92 || q[i]>=97 && q[i]<=121)
{
push(value[i]);
}
else
{
b=pop(); /*top element*/
a=pop(); /*second top*/
if(q[i]=='^')
push(pow(a,b));
else if(q[i]=='/')
push(a/b);
else if(q[i]=='*')
push(a*b);
else if(q[i]=='+')
push(a+b);
else if(q[i]=='-')
push(a-b);
}
}
printf("\n Answer after solving postfix expression %f",stack[top]);
}

void push(float f)
{
top++;
stack[top]=f;
}

float pop()
{
return(stack[top--]);
}
OUTPUT
Date of Exp: …………... Exp. No. 10
Faculty Signature………. Roll No. ………………

AIM: Program to Reverse a String using Stack

ALGORITHM:

1. Create a new stack.


2. Push all elements of the sting from starting till the end to the stack.
3. Pop elements one by one and put them back to sting.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#include<string.h>

char stack[20];
int top=-1;

void push(char);
char pop();

void main()
{
char str[20],ch;
int l,i;
clrscr();
printf("\nEnter String: ");
gets(str);
l=strlen(str);
for(i=0;i<l;i++)
push(str[i]);
printf("\nReversed String: ");
for(i=0;i<l;i++)
{
ch=pop();
printf("%c",ch);
}
getch();
}
void push(char c)
{
top++;
stack[top]=c;
}
char pop()
{
char c;
c=stack[top];
top--;
return c;
}

OUTPUT
Date of Exp: …………... Exp. No. 11
Faculty Signature………. Roll No. ………………

AIM: Program to perform Insert, Delete & Display operations on Queue.

ALGORITHM

QINSERT(QUEUE,N,FRONT,REAR,ITEM)
This procedure inserts an element(item) into a queue.
1. If FRONT=1 and REAR=N, or if FRONT=REAR+1 then, write OVERFLOW and return.
2. If FRONT=NULL, then,
a) Set FRONT=1, and REAR=1
b) Else if REAR=N, then,
c) Set REAR=1.
d) Else
e) Set REAR=REAR+1.
3. Set QUEUE [REAR]=ITEM.
4. Return.

QDELETE(QUEUE,N,FRONT,REAR,ITEM)
This procedure deletes an element from a queue and assigns it to variable ITEM.
1) If FRONT=NULL, then, write UNDERFLOW and Return.
2) Set ITEM=QUEUE [FRONT].
3) If FRONT=REAR, then:
a) Set FRONT=NULL and REAR=NULL.
b) Else if FRONT=N then
c) Set FRONT=1.
d) Else
e) Set FRONT=FRONT+1.
4) Return.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#define MAX 50

void insert_queue();
void delete_queue();
void display();

int queue[MAX],f=-1,r=-1;
void main()

{
int choice;
char ch='y';
clrscr();

while(ch=='y'|| ch=='Y')
{
printf("\n1. Insertion");
printf("\n2. Deletion");
printf("\n3. Display");
printf("\n \n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:insert_queue();
break;
case 2:delete_queue();
break;
case 3:display();
break;
default: printf("\n Wrong choice enetered!!!!");

}
printf("\n\nDo you want to continue? (Y/N): ");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}

void insert_queue()
{
int ele;
printf("\nEnter element you want to insert : ");
scanf("%d",&ele);
if(r==MAX-1)
{ printf("\nQueue OVERFLOW");
}
else
{
if(f==-1)
{
f=0;
r=0;
}
else
{
r=r+1;
}
queue[r]=ele;
}
}

void display()
{
int i;
if(f==-1)
{
printf("\n Queue UNDERFLOW");
}

else
{
for(i=f;i<=r;i++)
printf("\nElement: %d",queue[i]);
}
}

void delete_queue()
{ int i;
if(f==-1)
{
printf("Queue UNDERFLOW");
}
else
{
printf("\n Deleted Element is :%d",queue[f]);
f=f+1;
}
}
OUTPUT
Date of Exp: …………... Exp. No. 12
Faculty Signature………. Roll No. …………………

AIM: Program to perform Insert, Delete & Display operations on Circular Queue

ALGORITHM:

ALGORITHM FOR INSERTION

1. If REAR = SIZE-1 then


REAR = 0
Else
REAR=REAR + 1
2. If FRONT = REAR then
Write ("Circular Queue Overflow")
3. CQ[REAR]=X
4. If FRONT = -1 then
FRONT=0
5. Exit

ALGORITHM FOR DELETION

1. If FRONT = -1 then
Write ("Circular Queue Underflow")
2. Return (CQ [FRONT])
3. If FRONT = REAR then
FRONT=REAR=-1
4. If FRONT = SIZE-1 then
FRONT=0
Else
FRONT=FRONT+1
5. Exit.

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 50

void enqueue();
void dequeue();
void display();
int cqueue[MAX],f=-1,r=-1;
void main()
{
int choice;
char ch='y';
clrscr();

while(ch=='y'|| ch=='Y')
{
printf("\n1. Insertion");
printf("\n2. Deletion");
printf("\n3. Display");
printf("\n Enter your choice :");
scanf("%d",&choice);

switch(choice)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
default: printf("\nYou have entered wrong choice");
}
printf("\nDo you want to continue? (Y/N): ");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}

void enqueue()
{
int ele;
if((f==0 && r==MAX-1)|| (f==r+1))
{
printf("\nQueue Overflow");
}
else
{
printf("\nEnter the element you want to insert : ");
scanf("%d",&ele);
if(f==-1)
{
f=0;
r=0;
}
else
{
if(r==MAX-1)
r=0;
else
r=r+1;
}
cqueue[r]=ele;
}
}

void dequeue()
{
if(f==-1)
{
printf("\nQueue Underflow ");
}
else
{
printf("Deleted element is :\t%d ",cqueue[f]);

if(f==r)
{
r=-1;
f=-1;
}
}
}

void display()
{
int i;
if(f==-1)
{
printf("\nQueue Underflow");
}
else
{
if(f<=r)
{
for(i=f;i<=r;i++)
printf("\nElement: %d",cqueue[i]);
}
else
{
for(i=f;f<MAX;i++)
printf("\nElement: %d",cqueue[i]);
for(i=0;i<r;i++)
printf("\nElement: %d",cqueue[i]);
}
}
}
OUTPUT
Date of Exp: …………... Exp. No. 13
Faculty Signature………. Roll No. …………..………

AIM: Program to perform Insertion (Beginning, Between, End), Deletion (Beginning, Between, End) &
Traverse operations on Link List

ALGORITHM

ALGORITHM FOR INSERTION IN START :


1. Create a newNode with given value.
2. Check whether list is Empty (head == NULL)
3. If it is Empty then, set newNode→next = NULL and head = newNode.
4. If it is Not Empty then, set newNode→next = head and head = newNode.
5. Exit.

1. ALGORITHM FOR INSERTION IN BETWEEN

1. Create a newNode with given value.


2. Check whether list is Empty (head == NULL)
3. If it is Empty then, set newNode → next = NULL and head = newNode.
4. If it is Not Empty then, define a node pointer temp and initialize with head.
5. Keep moving the temp to its next node until it reaches to the node after which we want to insert the
newNode (until temp1 → data is equal to location, here location is the node value after which we want
to insert the newNode).
6. Every time check whether temp is reached to last node or not. If it is reached to last node then
display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function.
Otherwise move the temp to next node.
7. Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'
8. Exit.

2. ALGORITHM FOR INSERTION IN END

1. Create a newNode with given value and newNode → next as NULL.


2. Check whether list is Empty (head == NULL).
3. If it is Empty then, set head = newNode.
4. If it is Not Empty then, define a node pointer temp and initialize with head.
5. Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next is
equal to NULL).
6. Set temp → next = newNode.
7. Exit

3. ALGORITHM FOR DELETION IN START

1. Check whether list is Empty (head == NULL)


2. If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
4. Check whether list is having only one node (temp → next == NULL)
5. If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
6. If it is FALSE then set head = temp → next, and delete temp.
7. Exit

4. ALGORITHM FOR DELETION IN END

1. Check whether list is Empty (head == NULL)


2. If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head.
4. Check whether list has only one Node (temp1 → next == NULL)
5. If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
(Setting Empty list condition)
6. If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same until it
reaches to the last node in the list. (until temp1 → next == NULL)
7. Finally, Set temp2 → next = NULL and delete temp1.
8. Exit

5. ALGORITHM FOR DELETION IN BETWEEN :

1. Check whether list is Empty (head == NULL)


2. If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head.
4. Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And every
time set 'temp2 = temp1' before moving the 'temp1' to its next node.
5. If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!!'.
And terminate the function.
6. If it is reached to the exact node which we want to delete, then check whether list is having only one
node or not
7. If list has only one node and that is the node to be deleted, then set head = NULL and
delete temp1 (free(temp1)).
8. If list contains multiple nodes, then check whether temp1 is the first node in the list (temp1 == head).
9. If temp1 is the first node then move the head to the next node (head = head → next) and delete temp1.
10. If temp1 is not first node then check whether it is last node in the list (temp1 → next == NULL).
11. If temp1 is last node then set temp2 → next = NULL and delete temp1 (free(temp1)).
12. If temp1 is not first node and not last node then set temp2 → next = temp1 → next and
delete temp1 (free(temp1)).
13. Exit

6. ALGORITHM FOR DISPLAY

1. Check whether list is Empty (head == NULL)


2. If it is Empty then, display 'List is Empty!!!' and terminate the function.
3. If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
4. Keep displaying temp → data with an arrow (--->) until temp reaches to the last node
5. Finally display temp → data with arrow pointing to NULL (temp → data ---> NULL).
6. Exit
PROGRAM

CODING

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

void insert();
void display();
void insert_middle();
void insert_end();
void delet();
void delet_middle();
void delet_end();

struct node
{
int info;
struct node *next;
}*start=NULL;

void main()
{
char ch='y';
int a;
clrscr();

while(ch=='y' || ch=='Y')
{
printf("\n Press 1 for insertion in start");
printf("\n Press 2 for insertion in middle");
printf("\n Press 3 for insertion in end");
printf("\n Press 4 for display");
printf("\n Press 5 for deletion in start");
printf("\n Press 6 for deletion in middle");
printf("\n Press 7 for deletion in end\n");
printf("\n ENTER YOUR CHOICE : ");
scanf("%d",&a);
switch(a)
{
case 1:
insert();
break;
case 2:
insert_middle();
break;
case 3:
insert_end();
break;
case 4:
display();
break;
case 5:
delet();
break;
case 6:
delet_middle();
break;
case 7:
delet_end();;
break;
default: printf("\nYou Have Entered Wrong Choice !!");
}
printf("\n\n DO YOU WANT TO CONTINUE ?? Y/N:-\t");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}

void insert()
{
struct node *ptr;
int item;
printf("\n Enter the element to be inserted at start: ");
scanf("%d",&item);
ptr=(struct node*)malloc(sizeof(struct node));
ptr->info=item;
if(start==NULL)
{
start=ptr;
ptr->next=NULL;
}
else
{
ptr->next=start;
start=ptr;
}
}

void display()
{
struct node*temp;
if(start==NULL)
{
printf("\n LIST CONTAINS NO MODE");
}
else
{
temp=start;
while(temp!=NULL)
{
printf("\n\n ELEMENTS ARE : %d",temp->info);
temp=temp->next;
}
}
}

void insert_middle()
{
struct node*current,*prev=NULL,*temp;
int item;
printf("\nEnter the element to be inserted");
scanf("%d",&item);
temp=(struct node*)malloc(sizeof (struct node));
temp->info=item;
temp->next=NULL;
current=start;
while(current!=NULL)
{
if(current->info>=item)
break;
prev=current;
current=current->next;
}
if(prev==NULL)
start=temp;
else
prev->next=temp;
temp->next=current;
printf("Element inserted is : %d",temp->info);

void insert_end()
{
int item;
struct node *current,*temp;
printf("\n Enter the element to be inserted");
scanf("%d",&item);
temp=(struct node*)malloc(sizeof(struct node));
temp->info=item;
temp->next=NULL;
if(start==NULL)
start=temp;
else

{
current=start;
while(current->next!=NULL)

current=current->next;
}
current->next=temp;

void delet()
{
struct node *temp;
printf("\n\nDeleted element is : %d",start->info);
temp=start;
start=start->next;
free(temp);
}

void delet_middle()
{
struct node*current,*prev;
int item;
printf("Ente element to be deleted : ");
scanf("%d",&item);
if(start==NULL)
{
printf("\n Underflow");
}
current=start;
if(start->next==NULL)
{ start=NULL;
}
else
{
while(current->info!=item)
prev=current;
current=current->next;
}
prev->next=current->next;
printf("The deleted element is: %d",current->info);
free(current);
}

void delet_end()
{
struct node *current,*prev;
current=start;
if(start->next==NULL)
{
start=NULL;
}
else
{
while(current->next!=NULL)
{ prev=current;
current=current->next;
}
prev->next=NULL;

}
printf("Elemented deleted is : %d",current->info);
free(current);
}

OUTPUT
Date of Exp: …………... Exp. No. 14
Faculty Signature………. Roll No. ………………

AIM: Program to perform Pop, Push & Traverse operations on Dynamic Stack

ALGORITHM:

PUSH Algorithm

1. Declare a node from avail list with same size as of rest of the node in the list.
2. Set ptr-> info = item
3. Set ptr-> link = top
4. top = ptr
5. exit

POP Algorithm

1. If ( top == NULL) then,


write Underflow and exit
2. Set temp = top
3. Set top= top -> link
4. Free variable temp.
5. Exit

Traverse Algorithm :

1. if ( top == NULL) then ,


write Underflow and exit
2. set temp = top
3. while ( temp != NULL)
write temp -> info
temp = temp-> next
4. exit

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void push();
void display();
void pop();

struct node
{
int info;
struct node *next;
}*top=NULL;

void main()
{
char ch='y';
int a;
clrscr();
while(ch=='y' || ch=='Y')
{
printf("\n Press 1 for push operation on dynamic stack ");
printf("\n Press 2 for pop operation on dynamic stack");
printf("\n Press 3 for display");

printf("\n ENTER YOUR CHOICE : ");


scanf("%d",&a);
switch(a)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default: printf("\n You Have Entered Wrong Choice !!");
}
printf("\n DO YOU WANT TO CONTINUE ?? Y/N: ");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}

void push()
{
struct node *ptr;
int item;
printf("\n Enter the element to be inserted at start: ");
scanf("%d",&item);
ptr=(struct node*)malloc(sizeof(struct node));
ptr->info=item;
if(top==NULL)
{
top=ptr;
ptr->next=NULL;
}
else
{
ptr->next=top;
top=ptr;
}
}

void display()
{
struct node*temp;
if(top==NULL)
{
printf("\n LIST CONTAINS NO MODE");
}
else
{
temp=top;
while(temp!=NULL)
{
printf("\n ELEMENTS ARE : %d",temp->info);
temp=temp->next;
}
}
}

void pop()
{
struct node *temp;
printf("\n Deleted element is : %d",top->info);
temp=top;
top=top->next;
free(temp);
}
OUTPUT
Date of Exp: …………... Exp. No. 15
Faculty Signature………. Roll No. …………..………

AIM: Program to perform Insert, Delete & Display operations on Dynamic Queue

ALGORITHM:

ALGORITHM FOR INSERTION :

1. If REAR >= SIZE – 1 then


Write “Queue is Overflow”
2. REAR = REAR + 1
3. QUEUE [REAR] = X
4. If FRONT = -1 then
FRONT = 0
5. Exit

ALGORITHM FOR DELETION :

1. If FRONT = -1 then
Write “Queue is Underflow”
2. Return QUEUE [FRONT]
If FRONT = REAR then
FRONT = 0
REAR = 0
Else
FRONT = FRONT + 1
3. Exit

PROGRAM:

CODING

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

void enqueue();
void dequeue();
void display();

struct node
{
int info;
struct node*next;
}*rear=NULL;
void main()
{ char ch='y';
int a;
clrscr();
while(ch=='y'||ch=='Y')
{
printf("\n1. Insert elements into Dynamic Queue");
printf("\n2. Delete elements from Dynamic Queue");
printf("\n3. Traverse elements of Dynamic Queue");
printf("\nEnter your choice : ");
scanf("%d",&a);
switch(a)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3:display();
break;
default: printf("\nYou have entered a wrong choice");
}
printf("\nDo you want to continue ? Y/N ");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}
void enqueue()
{
int item;
struct node *current,*temp;
printf("\n Enter the element: ");
scanf("%d",&item);
temp=(struct node*)malloc(sizeof(struct node));
temp->info=item;
temp->next=NULL;
if(rear==NULL)
rear=temp;
else
{
current=rear;
while(current->next!=NULL)
{
current=current->next;
}
current->next=temp;
}
}
void dequeue()
{
struct node *temp;
printf("\n Deleted element is : %d",rear->info);
temp=rear;
rear=rear->next;
free(temp);
}
void display()
{
struct node*temp;
if(rear==NULL)
{
printf("\nQueue contain no element");
}

else
{
temp=rear;
printf("\nElements in Queue are: ");
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
}

OUTPUT

Das könnte Ihnen auch gefallen