Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 LINEAR SEARCH
AIM
To implement linear search using C program.
ALGORITHM
Start
1 .i=1
2. if(k=A[i]) then
3. print „Successful‟at location i
4. Go to step 14
5. else
6. i=i+1
7. If(i<=n) then
8. Go to step 2
9. else
10. printf „Unsuccessful‟
11. Go to step 14
12. end if
13. end if
Stop
SOURCE CODE
// 1.1 LINEAR SEARCH //
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[20],x,flag=0;
clrscr();
printf("LINEAR SEARCH");
//GET THE INPUT
printf("\n\nEnter the number of elements\n");
scanf("%d",&n);
printf("\nEnter the array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element to be searched\n");
scanf("%d",&x);
// PERFORMS SEARCHING
for(i=0;i<n;i++)
{
if(x==a[i])
{
// PRINTS THE FOUND ELEMENT & ITS POSITION
printf("\nThe given element %d is found in position %d\n", x,i+1);
flag=1;
break;
}
}
// PRINTS IF THE ELEMENT IS NOT FOUND
if(flag==0)
{
printf("\nElement not found\n");
}
getch();
}
OUTPUT
RESULT
Thus linear search has been implemented using C program.
1.2 BINARY SEARCH
AIM
To implement binary search using C program.
ALGORITHM
Start
1.L=1,u=n
2.flag=False
3. While(flag!=True) and (l<u) do
4. mid=(l+u)/2
5. if(k=A[mid]) then
6. print „Successful‟
7. flag=TRUE
8. Return(mid)
9. end if
10. if(k<A[mid]) then
11. U=mid-1
12. else
13. l=mid+1
14. end if
15. end while
16. if(flag=FALSE) then
17. print “unsuccessful‟
18. return -1
19. end if
Stop
SOURCE CODE
//1.2 BINARY SEARCH //
//HEADER FILES
#include<stdio.h>
#include<conio.h>
//DECLARATION
int i,mid,low,high,n,x,a[20],index;
int binsearch();
void main()
{
clrscr();
// GETS THE INPUT
printf("\nEnter the number of elements :\n");
scanf("%d",&n);
printf("\nEnter the array elements in sorted order:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element to search:\n");
scanf("%d",&x);
index=binsearch();
if(index!=-1)
{
printf("\nElement %d is found in position %d\n",x,index+1);
}
else
{
printf("\nElement not found\n");
}
getch();
}
// PERFORMS BINARY SEARCH
int binsearch()
{
low=0;
high=n-1;
while (low<high)
{
mid=(low+high)/2;
if(x==a[mid])
{
return mid;
}
else if(x>a[mid])
{
low=mid+1;
}
else if(x<a[mid])
{
high=mid-1;
}
}
return -1;
}
OUTPUT
RESULT
Thus binary search has been implemented using C program.
2.3 BUBBLE SORT
AIM
To implement bubble sort using C program.
ALGORITHM
For i=1 to n-1
For j=1 to n-i
If(a[j]>a(j+1)
Swap(a[j],a[j+1])
End if
End for
End for
SOURCE CODE
// 2.3 BUBBLE SORT
#include<stdio.h>
#include<conio.h>
int a[20],k,i,n,temp,j;
void get(); // TO GET INPUT
void sort(); // TO PERFORM SORTING
void display(int[],int); // TO DISPLAY SORTED ELEMENTS
void main()
{
clrscr();
printf("\n\t\tBUBBLE SORT\n");
get();
sort();
getch();
}
// GETS THE NUMBER OF ELEMENTS AND DISPLAYS IT IN
UNSORTED ORDER
void get()
{
printf("\nEnter the number of elements:\n");
scanf("%d",&n);
printf("\nThe elements are:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe unsorted elements are:\n");
display(a,n);
}
// PERFORMS SORTING OPERATION
void sort()
{
for(j=1;j<=n;j++)
{
for(k=1;k<=n-j;k++)
{
if(a[k]>a[k+1])
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
}
}
printf("\nPASS: %d",j);
display(a,n);
}
printf("\n\nSorted elements are:\n");
display(a,n);
}
//DISPLAYS THE OUTPUT
void display(int a[], int n)
{
for(i=1;i<=n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus bubble sort has been implemented using C program.
2.8 QUICK SORT
AIM
To implement quick sort using C program.
ALGORITHM
Quick sort(A,first,last)
1. If(first<last) then
2. P=Partition(A,first,last)
3. Quick sort(A,first,P-1)
4. Quick sort(A,P+1,last)
5. Return
SOURCE CODE
/* 2.8 QUICK SORT */
#include<stdio.h>
#include<conio.h>
int a[10],i,j,n,temp,pivot;
void qsort();
void interchange();
int partition();
void display();
void main()
{
clrscr();
printf("\n QUICK SORT \n");
printf("\nEnter the number of elements:\n");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
qsort(0,n-1);
display();
getch();
}
void qsort(int left,int right)
{
if(left<right)
{
j=partition(a,left,right);
qsort(left,j-1);
qsort(j+1,right);
}
}
int partition(int a[],int left,int right)
{
if(left<right)
{
pivot=a[left];
i=left;
j=right+1;
do
{
// Find bigger than pivot value from left to right
do
{
i++;
}while(a[i]<pivot);
// Find smaller than pivot value from right to left
do
{
j--;
}while(a[j]>pivot);
if(i<j)
{
interchange(a,i,j);
}
}while(i<j);
// Insert pivot as partition element
interchange(a,left,j);
}
return j;
}
void interchange(int a[],int i,int j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void display()
{
printf("\nSorted elements are:\n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus quick sort has been implemented using C program.
2.7 MERGE SORT
AIM
To implement merge sort using C program.
ALGORITHM
Void merge sort(int a[], int temp[], int n)
{
Msort(A, temp, 0, n-1);
}
Void msort(int a[], int temp[]. Int left, int right)
{
Int center;
If(left<right)
{
Center=(right+left)/2;
Msort(A, temp, left, right);
Msort(A, temp,center+1, right);
Merge(A, temp, left, center+1, right);
}
}
SOURCE CODE
/* 2.7 MERGE SORT */
#include<stdio.h>
#include<conio.h>
int i,n,a[10];
void partition();
void msort();
void display();
void main()
{
clrscr();
printf("\n\t\t MERGE SORT\n");
printf("\nEnter the number of elements:\n");
scanf("%d",&n);
printf("\nEnter the values \n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
partition(1,n);
display(n);
getch();
}
//partition upto 1 or 2 elements
void partition(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(low,mid);
partition(mid+1,high);
msort(low,mid,high);
}
}
// Performs sorting and merging
void msort(int low,int mid,int high)
{
int b[10];
int i,j,m,k;
m=low;
i=low;
j=mid+1;
// compare two partition
while(i<=mid && j<=high)
{
if(a[i]<a[j])
{
b[m]=a[i];
i=i+1;
}
else
{
b[m]=a[j];
j=j+1;
}
m=m+1;
}
//append remaining element
if(i<=mid)
{
for(k=i;k<=mid;k++)
{
b[m]=a[k];
m=m+1;
}
}
else if(j<=high)
{
for(k=j;k<=high;k++)
{
b[m]=a[k];
m=m+1;
}
}
// copy to a[]
for(k=low;k<=high;k++)
{
a[k]=b[k];
}
}
void display(int n)
{
int i;
printf("\nThe sorted array is:\n");
for(i=1;i<=n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus merge sort has been implemented using C program.
2.2 SELECTION SORT
AIM
To implement selection sort using C program.
ALGORITHM
Straight selection sort:
For i=n to (n-1) do
J=selection min(i,n)
If(i!=j) then
Swap(A[i],A[j])
End if
End for
Stop
Selection Minimum:
Min=A[L]
Min loc=L
For i=L+i to R do
If (min>A[i]) then
Min=A[i]
Min loc=i
End if
End for
Return(min loc)
SOURCE CODE
//2.2 SELECTION SORT
#include<stdio.h>
#include<conio.h>
int i,j,t,n,k,a[20],min;
void get(); // TO GET INPUT
void sort(); // TO PERFORM SORTING
void display(int[],int);
void main()
{
clrscr();
printf("\n\t\t SELECTION SORT");
get();
sort();
getch();
}
// GETS THE NUMBER OF ELEMENTS AND DISPLAYS IT IN
UNSORTED ORDER
void get()
{
printf("\nEnter the number of elements:\n");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe unsorted elements are:\n");
display(a,n);
}
// PERFORMS SORTING OPERATION
void sort()
{
for(k=1;k<n;k++)
{
min=k;
for(j=k+1;j<=n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=k)
{
t=a[k];
a[k]=a[min];
a[min]=t;
}
printf("\n PASS: %d",k);
display(a,n);
}
printf("\nSorted Elements are:\n");
display(a,n);
}
//DISPLAYS THE OUTPUT
void display(int a[], int n)
{
for(i=1;i<=n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus selection sort has been implemented using C program.
2.1 INSERTION SORT
AIM
To implement insertion sort using C program.
ALGORITHM
Void insertion_sort(int a[],int n)
{
IntI,j,temp;
For(i=0;i<n;i++)
{
Temp=a[i];
For (j=1;j<0&&a[j-1]>temp;j--)
{
A[j]=a[j-1];
}
A[j]=temp;
}
}
SOURCE CODE
//2.1 INSERTION SORT
#include<stdio.h>
#include<conio.h>
int a[20],i,j,k,n,key;
void get(); // TO GET INPUT
void sort(); // TO PERFORM SORTING
void display(int[],int);
void main()
{
clrscr();
printf("\n\t\tINSERTION SORT\n");
get();
sort();
getch();
}
// GETS THE NUMBER OF ELEMENTS AND DISPLAYS IT IN
UNSORTED ORDER
void get()
{
printf("\nEnter the number of elements:\n");
scanf("%d",&n);
printf("\nThe array elements are:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe unsorted elements are:\n");
display(a,n);
}
// PERFORMS SORTING OPERATION
void sort()
{
for(j=2;j<=n;j++)
{
key=a[j];
i=j-1;
while((i>0) && (key < a[i]))
{
a[i+1]=a[i];
i=i-1;
}
a[i+1]=key;
printf("\nPass %d :", j-1);
display(a,n);
}
printf("\n\nSorted elements are:\n");
display(a,n);
}
//DISPLAYS THE OUTPUT
void display(int a[], int n)
{
for(i=1;i<=n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus insertion sort has been implemented using C program.
2.4 SHELL SORT
AIM
To implement shell sort using C program.
ALGORITHM
Void shell sort(int A[], int N)
{
IntI,j,k,temp;
For(k=N/2;k>0;k=k/2)
For(i=k;i<N;i++)
{
Temp=A[i];
For(j=1;j>=k && A[j=k]>temp;j=j-k)
{
A[j]=A[j-k];
}
A[j]=temp;
}
}
SOURCE CODE
// 2.4 SHELL SORT
#include<stdio.h>
#include<conio.h>
int n,i,t,dist,a[20],swap;
void get(); // TO GET INPUT
void shellsort(); // TO PERFORM SORTING
void display(int[],int);// TO DISPLAY THE OUTPUT
void main()
{
clrscr();
printf("\n\t\tSHELL SORT\n");
get();
shellsort();
getch();
}
// GETS THE NUMBER OF ELEMENTS AND DISPLAYS IT IN
UNSORTED ORDER
void get()
{
printf("\nEnter the number of elements:");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nThe unsorted elements are:\n");
display(a,n);
}
// PERFORMS SORTING OPERATION
void shellsort()
{
dist=n/2;
do
{
do
{
swap=0;
for(i=1;i<=n-dist;i++)
{
if(a[i]>a[i+dist])
{
t=a[i];
a[i]=a[i+dist];
a[i+dist]=t;
swap=1;
}
}
printf("\nDistance %d:", dist);
display(a,n);
}while(swap);
dist=dist/2;
}while(dist);
printf("\n\nSorted Elements are", dist);
display(a,n);
}
//DISPLAYS THE OUTPUT
void display(int a[], int n)
{
for(i=1;i<=n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus shell sort has been implemented using C program.
2.6 RADIX SORT
AIM
To implement radix sort using C program.
ALGORITHM
Start
For i=1 to c do
For j=1 to n do
X=Entract(A[j],i)
Enque(Qx,A[j])
End for
For k=0 to b-1 do
While Qx is not empty do
Y=Dequeue(Qx)
Insert(A,y)
End while
End for
End for
Stop
SOURCE CODE
/* 2.6 RADIX SORT */
#include<stdio.h>
#include<conio.h>
int a[15],n,r,i,divisor,digit=0,j,k,max,p,tc,sc;
int bucket[10][10],bc[10];
void get();
void rsort();
void display(int[],int);
void main()
{
clrscr();
printf("\n\t\tRADIX SORT");
get();
rsort();
getch();
}
void get()
{
printf("\nEnter the number of elements:\n");
scanf("%d",&n);
printf("\nEnter the array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe unsorted elements are:\n");
display(a,n);
}
void rsort()
{
// TO FIND THE MAXIMUM ELEMENT
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
// TO IDENTIFY THE MAXIMUM NUMBER OF DIGITS IN THE
GIVEN INPUT
while(max>0)
{
digit++;
max=max/10;
}
// TO SORT THE ELEMENTS USING MODULO DIVISION
divisor=1;
for(p=0;p<digit;p++)
{
for(i=0;i<10;i++)
{
bc[i]=0;
}
for(i=0;i<n;i++)
{
r=(a[i]/divisor)%10;
bucket[r][bc[r]]=a[i];
bc[r]++;
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bc[k];j++)
{
a[i++]=bucket[k][j];
}
}
divisor=divisor*10;
}
printf("\nSorted elements are:\n");
display(a,n);
}
//TO PRINT THE ELEMENTS IN SORTED ORDER
void display(int a[], int n)
{
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus radix sort has been implemented using C program.
2.5 HEAP SORT
AIM
To implement heap sort using C program.
ALGORITHM
Create heap:
Start
1. i=1
2. while(i<=n) do
3. x=A[i]
4. B[i]=x
5. j=1
6. while(j>1) do
7. if(B[j]>B[j/2)
8. temp=B[j]
9. B[j]=B[j/2]
10. B[j/2]=temp
11. j=j/2
12. else
13. j=1
14. end if
15. end while
16. i=i+1
17. end while
Stop
Remove max:
1. temp=B[j]
2. B[i]=B[1]
3. B[1]=B[i]
4. Stop
Rebuild heap:
1.If(i=1) then
2. exit
3.j=1
4.Flag=True
5.while(Flag=TRUE) do
6. leftchild=2*j
7.if(rightchild<=i) then
8. if(B[j]<=B[leftchild] and B[left child]>= B[rightchild] then
9.swap(B[j],B[left child]
10.j=leftchild
11.else
12.if[B[j]<=B[rightchild] and B[rightchild]>=B[leftchild] then
13.swap(B[j],B[rightchild]
14.j=right child
15.else
16.Flag=False
17.End if
18.End if
19.else
20.if(leftchild<=i) then
21.if(B[j]<=B[leftchild] then
22.swap(B[j],B[leftchild])
23.j=leftchild
24.else
25.Flag=False
26.End if
27.End if
28.End if
29.End while
30.Stop
SOURCE CODE
/* 2.5 HEAPSORT */
#include<stdio.h>
#include<conio.h>
int a[20],n,i,j,key,t;
void getdata(); // TO GET INPUT
void heapify(int[],int);// TO CREATE HEAP
void hsort(int[],int); // TO PERFORM SORTING
void adjust(int[],int,int); // TO ADJUST TO HEAP STRUCTURE
void display(int[],int); // TO DISPLAY OUTPUT
void main()
{
clrscr();
printf("\n\t\tHEAP SORT");
getdata();
getch();
}
// GETS THE NUMBER OF ELEMENTS IN UNSORTED ORDER
void getdata()
{
printf("\nEnter the number of elements\n");
scanf("%d", &n);
printf("\nEnter the elements to be sorted\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
hsort(a,n);
printf("\nSorted elements are:\n");
display(a,n);
}
// PERFORMS SORTING OPERATION
void hsort(int a[],int n)
{
heapify(a,n);
for(i=n;i>=2;i--)
{
t=a[i];
a[i]=a[1];
a[1]=t;
adjust(a,1,i-1);
}
}
// CREATES HEAP
void heapify(int a[], int n)
{
for(i=n/2;i>=1;i--)
{
adjust(a,i,n);
}
}
// TO CREATE A HEAP STRUCTURE
void adjust(int a[], int i,int n)
{
j=2*i;
key=a[i];
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
{
j=j+1;
}
if(key>a[j])
{
break;
}
a[j/2]=a[j];
j=2*j;
}
a[j/2]=key;
printf("\nHeap created is:\t");
for(i=1;i<=n;i++)
{
printf("%d\t",a[i]);
}
}
// DISPLAYS SORTED LIST
void display(int a[], int n)
{
for(i=1;i<=n;i++)
{
printf("\t%d",a[i]);
}
}
OUTPUT
RESULT
Thus heap sort has been implemented using C program.
3.1 SINGLE LINKED LIST IMPLEMENTATION
AIM
To implement single linked list using C program.
ALGORITHM
CREATE A LIST:-
Algorithm for declaration of structure node:
Strut NODE
DATA:Data Field
LINK:Link Field (Address of next struct node)
End Struct
Algorithm for CREATELIST()
CREATELIST()
HEAD, LAST, NEWNODE : NODE
Step 1: Set NEWNODE = GETNODE()
Step 2: CALL READNODE(NEWNODE)
Step 3: Set HEAD=NEWNODE
Step 4: Set LAST =NEWNODE
Step 5: If you want to add another node proceed otherwise Stop
Step 6: Set NEWNODE = GETNODE()
Step 7: CALL READNODE(NEWNODE)
Step 8: Assign LAST->Link=newnode
Step 9: Assign LAST=LAST->LINK
Step 10: Goto STEP 5
END CREATELIST()
INSERTION OF A NODE:-
Inserting as a First node in the List:
Insert First(Head:Node)
Newnode:Node
Step 1: Set NEWNODE=GETNODE()
Step 2: CALL READNODE(NEWNODE)
Step 3: If(HEAD==NULL)
Set HEAD =NEWNODE
Return
[End of if structure]
Step 4: Assign NEWNODE->LINK=HEAD
Step 5: Assign HEAD=NEWNODE
END INSERT_FIRST()
Inserting a last node in the list:
INSERT_LAST(HEAD:NODE)
LAST,NEWNODE:NODE
Step 1: Set NEWNODE=GETNODE()
Step 2: CALL READNODE(NEWNODE)
Step 3: If(HEAD==NULL)
Set HEAD =NEWNODE
Return
[End of if structure]
Step 4:Set LAST=HEAD
Step 5: Repeat while(LAST->LINK!=NULL)
Assign LAST->LINK=NEWNODE
[End of while structure]
Step 6: Assign LAST->LINK=NEWNODE
END INSERT_LAST()
Inserting an Intermediate node in the list:
INSERT_MIDDLE(HEAD:NODE)
LAST,NEWNODE:NODE;
CONDITION: Data of any one node in the list for insert
Step 1: Set NEWNODE=GETNODE()
Step 2: CALL READNODE(NEWNODE)
Step 3: If(HEAD==NULL)
Set HEAD =NEWNODE
Return
[End of if structure]
Step 4: Print, “Enter the data of node after which the insertion is to be
made”
Step 5: READ CONDITION
Step 6: Set LAST=HEAD
Step 7: Repeat while(LAST!=NULL)
Step 8: If(LAST->DATA==CONDITION) then
Assign NEWNODE->LINK=LAST->LINK
Assign LAST->LINK=NEWNODE
Return
Else
Assign LAST=LAST->LINK
[End of if structure]
Step 9: [End of step 7 while structure]
Step 10: Print ,”CONDITION IS NOT POSSIBLE”
END INSERT_MIDDLE()
MODIFICATION OF A NODE:
MODIFY_NODE(HEAD:NODE)
Step 1: If(HEAD==NULL)
Return
[End of if structure]
Step 2: Print “Enter the data of node to be modified”
Step 3: READ CONDITION
Step 4: Set LAST=HEAD
Step 5: Repeat while(LAST!=NULL)
Step 6: If(LAST->DATA==CONDITION) then
READ,LAST->DATA
Return
Else
Assign LAST=LAST->LINK
[End of if structure]
Step 7: [End of Step 5 while structure]
Step 8: Printf,”CONDITION IS NOT AVAILABLE”
END MODIFY_NODE()
DELETE OF A NODE:
Algorithm for releasing the memory for the node to be deleted:
RELEASE_NODE(NEWNODE:NODE)
Step 1: Deallocate the space for the node of newnode
Step 2: Return
End RELEASENODE()
Deleting the first node from the list:
DELETE_FIRST(HEAD:NODE)
DELNODE:NODE
Step 1: If(HEAD==NULL)
Print “List is empty”
Return
[End of if structure]
Step 2: Set DELNODE=HEAD
Step 3: Assign HEAD=HEAD->LINK
Step 4: print “Deleted data is”, DELNODE->DATA
Step 5: CALL RELEASENODE(DELNODE)
END DELETE_FIRST()
Deleting the last node from the list:
DELETE_LAST(HEAD:NODE)
LAST,PREV,DELNODE:NODE
Step 1: If(HEAD==NULL)
Print “List is empty”
Return
Step 2: If(HEAD->LINK==NULL) THEN
Set DELNODE=HEAD
Set HEAD=NULL
Print “Deleted Data is”,DELNODE->DATA
Return
[End of if structure]
Step 3: Set LAST=HEAD
Step 4: Repeat while(LAST->LINK!=NULL)
Step 5: Assign PREV=LAST
Step 6: Assign LAST=LAST->LINK
Step 7: [End of step 4 while loop]
Step 8: Set DELNODE=LAST
Step 9: PREV->LINK=NULL
Step 10: Print “Deleted data is”, DELNODE->DATA
Step 11: CALL RELEASENODE(DELNODE)
END DELETE_LAST()
Deleting an intermediate node from the list:
DELETE_MIDDLE(HEAD:NODE)
LAST,PREV,DELNODE:NODE
Step 1: If(HEAD==NULL)
Print “List is empty”
Return
[End of if structure]
Step 2: print “Enter the data of the node in the list for deletion”
Step 3: Read DELDATA
Step 4: If(HEAD->DATA==DELDATA) then
Set DELNODE=HEAD
Assign HEAD=HEAD->LINK
Print “Deleted data is”, DELNODE->DATA
CALL RELEASENODE(DELNODE)
Return
[End of if structure]
Step 5: Set LAST=HEAD->LINK
Step 6: PREV=HEAD
Step 7: Repeat while(LAST!=NULL)
Step 8: If(LAST->DATA==DELDATA) then
Set DELNODE=LAST
Assign PREV->LINK=LAST->LINK
Print “The deleted data is”, DELNODE->DATA
CALL RELEASENODE(DELNODE)
Return
Else
Assign LAST=LAST->LINK
Assign PREV=PREV->LINK
[End of if structure]
Step 9: [End of Step 7 while loop]
Step 10: print “DELDATA is not available in the list”
END DELETE_MIDDLE()
TRAVERSAL OF A LIST:
VIEW_LIST(HEAD:NODE)
Step 1: If(HEAD==NULL)
Print “List is empty”
Return
[End of if structure]
Step 2: while(HEAD!=NULL)
Step 3: print “The data is”, HEAD ->DATA
Step 4: HEAD=HEAD->LINK
Step 5: [End of step 2 while structure]
END VIEW_LIST()
COUNT THE NUMBER OF NODES IN THE LIST:
COUNT_LIST(HEAD:NODE)
COUNT:INTEGER
Step 1: Set COUNT=0
Step 2: If(HEAD=NULL)
Print, “List if Empty”
Return COUNT
Step 3: while(HEAD!=NULL)
Step 4: COUNT=COUNT+1
Step 5: HEAD=HEAD->LINK
Step 6: [End of Step 2 while structure]
Step 7: Return COUNT
END COUNT_LIST()
SOURCE CODE
// SINGLE LINKED LIST IMPLEMENTATION
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL,*tail=NULL,*newnode,*p;
void append(); // Append
void insert(); // to insert an element
void del(); // to delete an element
void display();
void append()
{
int value;
printf("\nEnter data to append: ");
scanf("%d",&value);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
tail->next=newnode;
}
tail=newnode;
printf("\nData Appended.");
}
void insert()
{
int value,pos,i;
struct node *newnode,*prevnode;
printf("\nEnter data to insert: ");
scanf("%d",&value);
printf("\nEnter the position: ");
scanf("%d",&pos);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
p=head;
if(pos==1)
{
newnode->next = head;
head = newnode;
}
else
{
for(i=1;i<pos;i++)
{
prevnode = p;
p=p->next;
}
prevnode->next = newnode;
newnode->next = p;
}
printf("\nElement inserted\n");
}
void del()
{
int pos,i;
struct node *newnode,*prevnode;
printf("Enter the position: ");
scanf("%d",&pos);
p=head;
if(pos==1)
{
head = p->next;
free(p);
}
else
{
for(i=1;i<pos;i++)
{
prevnode = p;
p=p->next;
}
prevnode->next = p->next;
if(p->next == NULL) // delete last node
{
tail = prevnode;
}
free(p);
printf("\n Element delete\n");
}
}
void display()
{
p=head;
printf("\nThe List elements: ");
while(p!=NULL)
{
printf(" %d -> ",p->data);
p=p->next;
}
printf("NULL\n");
}
// Main function
void main()
{
int choice;
do
{
printf("\n---------------------------------------------");
printf("\nSINGLE LINKED LIST\n\n");
printf(" 1.APPEND \n 2.INSERT \n 3.DELETE \n 4.DISPLAY \n
5.EXIT \n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
append();
display();
break;
case 2:
insert();
display();
break;
case 3:
del();
display();
break;
case 4:
display();
break;
case 5:
exit(0);
}
}while(choice<=4);
}
OUTPUT
RESULT
Thus single linked list has been implemented using C program.
3.2 DOUBLY LINKED LIST IMPLEMENTATION
AIM
To implement doubly linked list using C program.
ALGORITHM
Procedure DOUBINS(L,R,M,X)
Variables used:
L left most node address
R right most node address
NEWNew node address to be inserted
LPTRLeft link of a node
RPTRRight link of a node
INFOInformation field of the node
NODEName of an element of the list
M Pointer variable
X It contains information to be entered in the node
1. [Obtain new node from availability stack]
NEW NODE
2. [Copy information field]
INFO(NEW) X
3. [Insertion into an empty list?]
If R = NULL
Then LPTR(NEW) RPTR(NEW) NULL
L R NEW
Return
4. [Left most insertion?]
If M = L
Then LPTR(NEW) NULL
RPTR(NEW) M
LPTR(M) NEW
L NEW
Return
5. [Insert in middle]
LPTR(NEW) LPTR(M)
RPTR(NEW) M
LPTR(M) NEW
RPTR(LPTR(NEW)) NEW
Return
Procedure DOUBDEL(L, R, OLD)
Variables used:
LLeft most node
RRight most node
LPTR Left link of a node
RPTR Right link of a node
OLD The address of the node to be deleted
1. [Underflow?]
If R = NULL
Then Write(„UNDERFLOW‟)
Return
2. [Delete node]
Restore(OLD)
Return
SOURCE CODE
// DOUBLY LINKED LIST IMPLEMENTATION
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev,*next;
};
struct node *head=NULL,*tail=NULL,*newnode,*p;
void append(); // Append
void insert(); // to insert an element
void del(); // to delete an element
void displayforward();
void displaybackward();
void append()
{
int value;
printf("Enter data to append: ");
scanf("%d",&value);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
if(head==NULL)
{
newnode->prev = NULL;
head=newnode;
}
else
{
tail->next=newnode;
newnode->prev = tail;
}
tail=newnode;
printf("\nData Appended.");
}
void insert()
{
int value,pos,i;
printf("Enter data to insert: ");
scanf("%d",&value);
printf("Enter the position: ");
scanf("%d",&pos);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
p=head;
if(pos==1)
{
head = newnode;
newnode->prev = NULL;
newnode->next = p;
}
else
{
for(i=1;i<pos;i++)
{
p=p->next;
}
newnode->prev = p->prev;
newnode->next = p;
p->prev->next = newnode;
p->prev = newnode;
}
printf("\nElement inserted\n");
}
void del()
{
int pos,i;
printf("Enter the position: ");
scanf("%d",&pos);
p=head;
if(pos==1)
{
head = p->next;
head->prev = NULL;
free(p);
}
else
{
for(i=1;i<pos;i++)
{
p=p->next;
}
//Last node
if (p->next == NULL)
{
p->prev->next = NULL;
tail = p->prev;
}
else
{
p->prev->next = p->next;
p->next->prev = p->prev;
}
free(p);
printf("Element delete\n");
}
}
// Display the elements from top
void displayforward()
{
if(head==NULL)
{
printf("The List is empty");
}
else
{
p=head;
printf("The contents of the list are:\n");
while(p!=NULL)
{
printf(" %d -> ", p->data);
p=p->next;
}
printf("null");
}
}
// Display the elements from bottom
void displaybackward()
{
if(head==NULL)
{
printf("The List is empty");
}
else
{
p=tail;
printf("The contents of the list are:\n");
while(p!=NULL)
{
printf(" %d -> ",p->data);
p=p->prev;
}
printf("null");
}
}
// Main function
void main()
{
int choice;
do
{
printf("\n---------------------------------------------");
printf("\nDOUBLY LINKED LIST\n\n");
printf(" 1.APPEND \n 2.INSERT \n 3.DELETE \n 4.DISPLAY
FORWARD \n 5.DISPLAY BACKWARD \n 6.EXIT \n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
append();
displayforward();
break;
case 2:
insert();
displayforward();
break;
case 3:
del();
displayforward();
break;
case 4:
displayforward();
break;
case 5:
displaybackward();
break;
case 6:
exit(0);
}
}while(choice<=5);
}
OUTPUT
RESULT
Thus the doubly linked list has been implemented using C program.
3.3 STACK USING LINKED LIST REPRESENTATION
AIM
To implement stack using linked list using C program.
ALGORITHM
Step 1: Start the program.
Step 2: Declare the Stack data structure using pointer.
Step 3: Implement stack and read the choice of operation.
Step 4: Enter the elements to push into the stack.
Step 5: If the next space of the stack is found to be NULL then declare
top of the
stack is empty, create a new node and make the top of the as new
node.
Step 6: If the top of the stack is found to be full pop out the element
from the stack.
Step 7: If the top of the stack is found to be empty, print “the stack is
empty”
otherwise display the stack.
Step 8: Exit the stack.
Step 9: Stop the program.
SOURCE CODE
// STACK USING LINKED LIST REPRESENTATION
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*top=NULL;
void push(); // to insert an element
void pop(); // to delete an element
void display(); // to show the stack
// PUSH Operation
void push()
{
int value;
struct node *newnode;
printf("Enter a number to insert: ");
scanf("%d", &value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=value;
if(top==NULL)
{
top=newnode;
newnode->next=NULL;
}
else
{
newnode->next=top;
top=newnode;
}
printf("\nData Inserted.");
}
// POP Operation
void pop()
{
struct node *temp;
if(top==NULL)
{
printf("\nThe stack is empty!");
return;
}
printf("\nPoped value is %d",top->data);
temp=top;
top=top->next;
free(temp);
}
// Show stack
void display()
{
struct node *ptr=top;
printf("\nThe stack is: ");
while(ptr!=NULL)
{
printf("\n%d", ptr->data);
ptr=ptr->next;
}
printf("\nNULL\n");
}
// Main function
void main()
{
int choice;
clrscr();
do
{
printf("\n---------------------------------------------");
printf("\n\t\tSTACK USING LINKED LIST\n\n");
printf("1:PUSH\n2:POP\n3:DISPLAY\n4:EXIT\n");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
display();
break;
case 2:
pop();
display();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}while(choice<=4);
}
OUTPUT
RESULT
Thus stack using linked list has been implemented using C program.
3.4 QUEUE USING LINKED LIST IMPLEMENTATION
AIM
To implement queue using linked list using C program.
ALGORITHM
Step 1: Start the program.
Step 2: Declare the Queue data structure using pointer.
Step 3: Create a node and read the first element along with the link to
another
node.
Step 4: Read the element to be inserted by allocating memory. If the
new
element is inserted, print “The element is inserted ”.
Step 5: Perform delete operation and print “the node is deleted”.
Step 6: Display the elements in the list.
Step 7: Exit the Linked queue.
Step 8: Stop the program.
SOURCE CODE
// QUEUE USING LINKED LIST IMPLEMENTATION
#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL,*newnode,*ptr;
void enqueue(); // to insert an element
void dequeue(); // to delete an element
void display(); // to show the stack
// PUSH Operation
void enqueue()
{
int value;
printf("Enter a number to insert: ");
scanf("%d",&value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
printf("\nData Inserted.");
}
// POP Operation
void dequeue()
{
struct node *temp;
if(front==NULL)
{
printf("\nThe queue is empty!");
return;
}
else
{
printf("\nPoped value is %d",front->data);
temp=front;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
}
free(temp);
}
}
// Show stack
void display()
{
ptr=front;
printf("\nThe queue is: \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
printf("NULL\n");
}
// Main function
void main()
{
int choice;
do
{
printf("\n---------------------------------------------");
printf("\n\t\tQUEUE USING LINKED LIST\n\n");
printf("1:ENQUEUE\n2:DEQUEUE\n3:DISPLAY\n4:EXIT\n");
printf("\nEnter your choice: \t");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
display();
break;
case 2:
dequeue();
display();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}while(choice<=4);
}
OUTPUT
RESULT
Thus queue using linked list has been implemented using C program.
4.1 ARRAY IMPLEMENTATION OF STACK
AIM
To perform array implementation of stack using C program.
ALGORITHM
Push Operation:
Void push(stack,top,maxstk,item)
Step 1:(stack is already filled?)
If top=maxstk then
print stack overflow and return
Step 2: Else
set top=top+1;
Step 3: set stack[top]=item
Step 4: return
End
Pop Operation:
Begin
Pop(stack,top,item)
Step 1: (is there is an item in stack for removal)
If top=-1 then
print stack is underflow and return
Step 2: else set item=stack[top]
Step 3: set top=top-1
Step 4:retuen
End
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int choice,n,a[30],top=-1,i;
void push(); // TO INSERT THE ELEMENT INTO THE STACK
void pop(); // TO DELETE THE ELEMENT FROM THE STACK
void peek(); // TO RETURN THE TOP VALUE OF THE STACK
void display();
void main()
{
//clrscr();
printf("\n ARRAY IMPLEMENTATION OF STACK");
do
{
printf("\nMain menu");
printf("\n1.Stack
creation\n2.Push\n3.Pop\n4.Peek\n5.Display\n6.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the stack size:");
scanf("%d",&n);
break;
case 2:
push();
display();
break;
case 3:
pop();
display();
break;
case 4:
peek();
display();
break;
case 5:
display();
break;
case 6:
exit(1);
}
}while(choice<=6);
getch();
}
void push()
{
if(top==n-1)
{
printf("\nThe stack is full\n");
}
else
{
printf("\nEnter the element to insert:");
top++;
scanf("%d",&a[top]);
}
}
void pop()
{
if (top==-1)
{
printf("\nThe stack is empty");
}
else
{
printf("\n The element popped is %d\n",a[top]);
top--;
}
}
void peek()
{
if (top==-1)
{
printf("\nThe stack is empty");
}
else
{
printf("\n The top element is %d\n",a[top]);
}
}
void display()
{
if (top==-1)
{
printf("\nThe stack is empty");
}
else
{
printf("\nThe stack elements are\n");
for(i=top;i>=0;i--)
{
printf("\t%d",a[i]);
}
}
}
OUTPUT
RESULT
Thus the array implementation of stack is performed using C program.
4.2 EVALUATING ARITHMETIC EXPRESSION
AIM
To implement evaluation of arithmetic expression using C program.
ALGORITHM
1. A string INFIX containing the infix expression
2. A stack opstk, which may contain:
- All arithmetic operators
- Parenthesis " (“ and “) "
- Null character " \O "
3. A string POSTFIXES containing the final postfix expression.
4. Push ' \o ' out OPSTACK as its first entry
5. Read the next character CH from the INFIX string
6.Test CH and:
- If CH is an operand, append it to the POSTFIX string
- If CH is a left parenthesis, then push CH onto the stack