You are on page 1of 53

1.AIM: To write a program to sort the elements of given array using selection sort.

DESCRIPTION: A selection-based sorting algorithm is a comparison-based


algorithm that divides the list into two parts, the sorted part on the left and the
unsorted part on the right. Initially, the sorted section is empty, and the unsorted
section contains the entire list. When sorting a small list, selection sort can be
used. 

Best case - ohm(n)


Average case - teta(n^2)
Worst case - O(n^3)

ALGORITHM:
Selection_Sort (A[ ] , N)
 Step 1 : start Begin
 Step 2 : Set POS = K 
Step 3 : Repeat for J = K + 1 to N –1 
             Begin 
If A[ J ] < A [ POS ]
Set POS = J 
Step 4 : End For Swap A [ K ] End For with A [ POS ] 
Step 5 : Stop

Page 1 Roll no:160121748021


PROGRAM:
//selection sort
#include<stdio.h>
int main(){
int a[100],I,j,temp,n,key;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n-1;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i+
+)
{
key=i;
for(j=i+1;j<n;j++)
{
if(a[key]>a[j])
{
key=j;
}
}
if(key!=i){
temp=a[i];
a[i]=a[key];
a[key]=temp;
}
}
printf("sortedarray\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;

Page 2 Roll no:160121748021


OUTPUT:

CONCLUSION:
Time complexity:
Worst case=average case=best case=O(n^2)

In the first iteration, (n - 1) comparisons are performed, (n - 2) in the second, and so


on until the last iteration. The total number of comparisons are n * (n - 1) / 2. The
number of swaps performed is at most n - 1.

Space complexity:
Any extra data structure apart from the input array is not used, the space
complexity is O(1).

Page 3 Roll no:160121748021


2.AIM: To write a program to sort the elements of given array using radix sort.

DESCRIPTION: Radix sort is a sorting algorithm that sorts the elements by first
grouping the individual digits of the same place value. Then, sort the elements
according to their increasing/decreasing order.

Best case - ohm(n)


Average case -
theta(n*log(n))
Worst case - O(n*log(n))
ALGORITHM:
countingSort(arr, n, div)
Step 1:let cnt[0...b] and tempArray[0...n] be new arrays
Step 2:for i = 0 to 9 do cnt[i] = 0
Step 3:for i = 0 to n-1 do cnt[(arr[i] / div) % 10] = cnt[(arr[i] / div) % 10] + 1
Step 4:for i = 1 to 9 do cnt[i] = cnt[i-1] + cnt[i]
Step 5:for i = n-1 downto 0 do
tempArray[cnt[(arr[i] / div) %10]-1] = arr[i]
cnt[(arr[i] / div) %10] = cnt[(arr[i] / div) %10] - 1
Step 6:for i = 0 to n-1 do arr[i] = tempArray[i]
radixSort(arr, n)
Step 1:mx = arr[0]
Step 2:for i = 1 to n-1
if mx < arr[i]
mx = arr[i]
i = 1 ,q = mx/i
Step 3:end for
Step 4: while q > 0 do i = i * 10, q = mx/i
Step 5:end while
Page 4 Roll no:160121748021
Page 5 Roll no:160121748021
PROGRAM:
//Radix sort in C
#include<stdio.h>
#include<math.h>
int maxArray(int a[],int n)
{
int max=a[0],i; for(i=1;i<n;i++)
{
if(a[i]>max)
max = a[i];
}
return max;
}
int digits_of_max(int max)
{
int x=0,rem=0;
while(max>0)
{
rem=max%10;
x=x+1;
max=max/10;
}
return x;
}
void printArray(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
int radixsort(int a[],int x,int n)
{
int p=1,i;
while(p<pow(10,x))
{
int count[10];
//Initializing elements of count array to zero
for(i=0;i<10;i++)
{
count[i] = 0;
}
for(i=0;i<n;i++)
{
Page 6 Roll no:160121748021
count[(a[i]/p)%10] += 1;
}
for(i=1;i<10;i++)
{
count[i] = count[i]+count[i-1]
}
int temp[100];
//Initializing elements of temp array to zero
for(i=0;i<n;i++)
{
temp[i] = 0;
}
for(i=n-1;i>=0;i--)
{
count[(a[i]/p)%10]--;temp[count[(a[i]/p)%10]] = a[i];
}
for(i=0;i<n;i++)
{
a[i] = temp[i];
}
p *= 10;
}
printArray(a,n);
}
void main()
{
int a[100],n,i,j,max,x;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("sorted elements:\n");
max=maxArray(a,n);
x=digits_of_max(max);
radixsort(a,x,n);

Page 7 Roll no:160121748021


OUTPUT:

CONCLUSION:
Time complexity:

Let K be the number of digits in the maximum element of the array and b be the base of
array elements.

It takes O(N + b) time to sort a particular digit, and the algorithm runs the counting sort
for all the K digits. Therefore, the total time complexity is O(K * (N + b))

best case = worst case = average case = O(K * (N + b))

Space complexity:
O(n+k) to hold counts, indices and output arrays.

Page 8 Roll no:160121748021


3.AIM: To write a program to sort the elements of the given array using merge sort.

DESCRIPTION: Merge sort is a divide-and-conquer algorithm based on the idea of


breaking down a list into several sub-lists until each sublist consists of a single
element and merging those sublists in a manner that results into a sorted list.

Best case - ohm(n*log(n))


Average case -
theta(n*log(n)) Worst case -
O(n*log(n))

ALGORITHM:
merge_sort(arr, beg, end)    
Step 1:if beg < end  
Step 2:set mid = (beg + end)/2  
Step 3:merge_sort(arr, beg, mid)  
step 4:merge_sort(arr, mid + 1, end)  
step 5:merge (arr, beg, mid, end)  
Step 6:end of if  
Merge(a, l, m, h )
Page 9 Roll no:160121748021
Step 1:i=l,j=m+1,k=l
Step 2:while i<=m and j<=h do
if a[i]<=m then c[k++]=a[i++]
else c[k++]=a[j++]
end if
Step 3:end while
Step 4: while j<=h do c[k++]=a[j++]
Step 5:end while
Step 6:for i=l to k-1 do a[i]=c[i]
Step 7:end for

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
} k+
+;
}
while (i < n1) {
arr[k] = L[i];
i++;
Page 10 Roll no:160121748021
k++;

Page 11 Roll no:160121748021


}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main()
{
int arr_size,arr[100],i,j;
printf("Enter the size of the array\n");
scanf("%d",&arr_size);
printf("Enter the elements of the array\n");
for(i=0;i<arr_size;i++)
{
scanf("%d",&arr[i]);
}
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}

Page 12 Roll no:160121748021


OUTPUT:

CONCLUSION:
Time complexity:
Worst case=average case=best case=O(nlogn)
The array gets divided into subarrays recursively which will create O(nlogn) rows
where each element is present in each row, it takes O(n) time to merge.That is, It
takes O(nlogn) time to complete the process.

Space complexity:
An auxiliary array of size at most n is used to store the merged subarray, the space
complexity is O(n).

Page 13 Roll no:160121748021


4.AIM: To write a program to sort the elements of the given array using quick sort.

DESCRIPTION:Quicksort is a sorting algorithm based on the divide and conquer


approach where
1. An array is divided into subarrays by selecting a pivot element (element selected
from the array).
While dividing the array, the pivot element should be positioned in such a way that
elements less than pivot are kept on the left side and elements greater than pivot are on
the right side of the pivot.
2. 2.The left and right subarrays are also divided using the same approach. This process
continues until each subarray contains a single element. Elements are sorted.

Best case - ohm(n*log(n))


Average case - teta(n*log(n))
Worst case - O(n^2)
ALGORITHM:
QUICKSORT (array A, start, end)     
Step 1: if (start < end)     
Step 2:p = partition(A, start, end)  
Step 3: QUICKSORT (A, start, p - 1)    
Step 4: QUICKSORT (A, p + 1, end)    
Step 5:end if   

PARTITION (array A, start, end)     
Step 1: pivot =A[start]     
Page 14 Roll no:160121748021
Step 2:i = start-1     
Step 3: for j =  start to end -1 
do if (A[j] < pivot) 
 then i = i + 1     
swap A[i] with A[j]      
Step 4:swap A[i+1] with A[end]     
Step 5: return i+1  

PROGRAM:
//Quick sort in C
#include<stdio.h>
void printArray(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void quickSort(int a[],int first,int last)
{
int p,i,j,temp;
i = first;
j = last;
if(i<j)
{
p = first;
while(i<j)
{
while(a[i] <= a[p] && i<last)
{
i++;
}
while(a[j] > a[p])
{
j--;
}
if(i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[j];
Page 15 Roll no:160121748021
a[j] = a[p];
a[p] = temp;
quickSort(a,first,j-1);
quickSort(a,j+1,last);
}
//printArray(a,n);
}
void main(){
int n,a[100],i;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int first = 0,last = n-1;
quickSort(a,first,last);
printArray(a,n);
}

OUTPUT:

Page 16 Roll no:160121748021


CONCLUSION:
Time complexity:
Worst case O(n^2): the array is sorted when the left most element is the pivot, or the
array is reverse sorted when the right most element is the pivot. The time
complexity becomes quadratic since partitioning the array results in highly
unbalanced subarrays in such cases.

Average and best case O(nlogn): The array is divided into two subarrays recursively
which will create O(log n) rows where each element is present in each row exactly
once. For each row, it takes O(n) time to merge every pair of subarrays. So the overall
time complexity becomes O(n log n).

Space complexity:
Worst case:O(n)
Best case:O(logn)
Additional space is required for creating stack frames in recursive call

Page 17 Roll no:160121748021


5.AIM: To write a c program to perform various operations on a single linked
list using menu driven program .

DESCRIPTION: A linked list is a dynamic data structure where each element


(called a node) is made up of two items: the data and a reference (or pointer),
which points to the next node. A linked list is a collection of nodes where each
node is connected to the next node through a pointer. The first node is called a
head and if the list is empty then the value of head is NULL..

Various operations performed are


1.creation
2.insertion
3.Deletion
4.Traversing
5.Searching
ALGORITHM:
Insert new node at front in linked list
step1- create a new node
Step2- new->link=header->link
Step3- new->data=item
Step4- header->link=new.
Step5-stop
Insert new node at end of the linked list
1.create a new node
2.ptr=header
3.while(ptr->link!=null)
ptr=ptr->link
4.ptr->link=new
5.new->data=item
6.new->link=null
7.stop
Insert new node at any position in linked list
1.Create new node
2.ptr=header
3.Enter the position
Page 18 Roll no:160121748021
4.for i=1 to pos-1
ptr=ptr->ilink;
new->link=ptr->link;
5.ptr->link=new;
6.new->data=item
7.stop.
Deleting a node at the beginning
1.If (header = = NULL) display “List is Empty”;
2.else do ptr = header;
header = header -> link;
free(ptr);
Deleting a node at the end
1.ptr = header;
2.while(ptr -> link != NULL)
ptr1=ptr;
ptr = ptr -> link;
3. ptr1 -> link = NULL;
4.free(ptr);
Deleting a node at the given position
1.ptr = header ;
2.for i=1 to pos-1
ptr=ptr->link
3.ptr1 = ptr -> link
4.ptr -> link = ptr1-> link;
5.free(ptr1);
Search operation
1.Ptr=header
2.Flag=-1
3.Enter the key element
4.While(ptr!=NULL)
4.1. If(ptr->data==key)
Flag=1
break;
4.2 Else
Ptr=ptr->next
5.If(flag==-1) display “Element not found”
6.else display “Element found at location”.

Page 19 Roll no:160121748021


PROGRAM:
//operations on linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*head,*temp;
struct node *head=NULL;
void traverse(){
int c=0;
struct node *temp3;
if(head==NULL) {
printf("List is empty\n");
return;
}
else{
temp3=head;
printf("The List elements are:\n")
while(temp3!=NULL){
printf("%d ",temp3->data );
temp3=temp3->next ;
c++;
}
}
printf("No of elements:%d\n",c);
}
void deletion(){
int z,p,i;
struct node *temp1;
if (head==NULL){
printf("The list is empty\n");
}
else{
printf("Delete element at 1.beginning 2.ending 3 specific position:");
scanf("%d",&z);
if (z==1){
temp=head;
head=head->next;
temp->next=NULL;
return;
}
if(z==2){

Page 20 Roll no:160121748021


temp1=head;
temp=head;
while(temp->next!=NULL){
temp=temp->next ;
}
while (temp1->next!=temp){
temp1=temp1-
>next;
}
temp1->next=NULL;
temp=temp1;
return;
}
if (z==3){
printf("Enter the position:");
scanf("%d",&p);
temp1=head;
for (i=0;i<p-1;i++){
temp1=temp1->next;
}
temp1->next=temp1->next->next;
return;
}
}
}
void search(){
int l=0,key;
temp=head;
printf("Enter element to be searched:");
scanf("%d",&key);
if (head==NULL){
printf("The list is empty\n");
}
while ((temp->data!=key) &&(temp->next!=NULL)){
temp=temp->next;
l++;
}
if ((temp->next==NULL)&&(temp->data!=key)){
printf("Element is not found\n");
}
else{
printf("Element is found at location %d\n",l);
}
}
void insertion(){
Page 21 Roll no:160121748021
int z,p,i;
struct node *temp1,*new1,*temp2;
new1=(struct node *)malloc(sizeof(struct node));
if(head==NULL){
printf("Enter the data:");
head=new1;
}
else{printf("Insert element at 1.beginning 2.ending 3 specific position:");
scanf("%d",&z);
printf("Enter the data:");
}
scanf("%d",&new1->data);
new1->next=NULL;
if (z==1){
new1->next=head;
head=new1;
return;
}
if(z==2){
temp2=head;
while(temp2->next!=NULL)
{
temp2=temp2->next ;
}
temp2->next=new1;
new1->next=NULL;
temp=new1;
return;
}
if (z==3){
printf("Enter the position:");
scanf("%d",&p);
temp1=head;
for (i=0;i<p-1;i++){
temp1=temp1->next;
}
new1->next=temp1->next;
temp1->next=new1;
temp1=new1;
return;
}
}
void main(){
int x=1,a;
while(x==1){
Page 22 Roll no:160121748021
printf("MENU LIST\n"); printf("1.Insert\n2.Delete\n3.Search\
n4.Traverse\n5.exit\n"); printf("Enter choice:");
scanf("%d",&a);
switch(a){
case 1:insertion();
break;
case 2:deletion();
break;
case 3:search();
break;
case 4:traverse();
break;
case 5:exit(0);
}
printf("Do you want to continue the process(YES-1/NO-0):");
scanf("%d",&x);
}
}

OUTPUT:

Page 23 Roll no:160121748021


Page 24 Roll no:160121748021
CONCLUSION:
Time complexity :

To access the i-th element, to traverse all elements , O(√N * N) time is used and to
insert element at current point, delete current element , Insert element at front ,O(1)
time is used and to insert the element at end, O(√N * N) time is used.

Space Complexity:
O(1)
Extra space is not needed beyond a fixed number of variables.

Page 25 Roll no:160121748021


6.AIM: Menu driven program to perform all operations on double linked list.

DESCRIPTION:Doubly linked list is a type of linked list in which each node apart
from storing its data has two links. The first link points to the previous node in the
list and the second link points to the next node in the list. The first node of the list
has its previous link pointing to NULL similarly the last node of the list has its next
node pointing to NULL.

Operations performed are


1.creation
2Insertion-beginning ,end,middle
3.Deletion-beginning,end,middle
4.Searching
5.displaying
ALGORITHM:
Insertion of a node at the front
1.if(header==NULL)
1.1.new->prev=NULL;
1.2.new->next=NULL;
1.3.header=new;
2.else
2.1.new->next=ptr;
2.2ptr->prev=new;
2.3new->prev=NULL;
2.4header=new;
Insertion of a node at the end
1. Create a new node
2. Read the item
3. new->data=item
4. ptr= header
5. while(ptr->next!=NULL)
5.1 ptr=ptr->next
6. new->next=NULL;
7. ptr->next=new;
8. new->prev=ptr;
Insertion of a node at any position in the list
Page 26 Roll no:160121748021
1. create a node new
2. read item
3. new->data=item
4. ptr=header;
5. Read the position where the element is to be inserted
6. for i=1 to pos-1
6.1 ptr=ptr->next;
7. 1 ptr1=ptr->next;
7.2 new->next=ptr1;
7.3 ptr1->prev=new;
7.4 new->prev=ptr;
7.5 ptr->next=new;
Deletion at beginning
1.ptr=header
2.ptr1=ptr->next;
3.header=ptr1;
4.if(ptr1!=NULL)
ptr1->prev=NULL;
5. free(ptr);
Deletion at End
1. ptr=header
2. while(ptr->next!=NULL)
2.1 ptr=ptr->next;
3. end while
4. ptr1=ptr->prev;
5. ptr1->next=NULL;
Deletion at any position
1. ptr=header
for i=0 to pos-1
ptr=ptr->next
2. ptr1=ptr->prev;
3. ptr2=ptr->next;
4. ptr1->next=ptr2;
5. ptr2->prev=ptr1;
6. free(ptr);
Displaying elements of a list
1. ptr=header;
2. if(header = = NULL display "The list is empty”
3. else display “The elements are ”
while(ptr!=NULL)
display “ptr->data”;
if(ptr->next = = NULL)
break;
ptr=ptr->next;

Page 27 Roll no:160121748021


end while
4.End if

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
}*head,*tail,*new,*temp;
struct node *head=NULL;

void create(){
struct node *new; int data,c=1;
while(c){
new=(struct node *)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",&data);
new->data=data;
new->next=NULL;
if(head==NULL){
head=new;
tail=new;
}
else{
tail->next=new;
new->prev=tail;
tail=new;
}
printf("do you want to cotinue(1-yes/0-no)");
scanf("%d",&c);
}
}
void traverse(){
int count=0;
struct node *temp;
if(head==NULL) {
printf("List is empty\n");
return;
}
else{
temp=head;
printf("The List elements are:");
while(temp!=tail){
Page 28 Roll no:160121748021
printf("%d ",temp->data );
temp=temp->next ; count+
+;
}
printf("%d ",temp->data );
}
printf("\nNo of elements:%d\n",count+1);
}
void deletion(){
int z,p,i;
struct node *temp;
temp=head;
if (head==NULL){
printf("The list is empty\n"); return
}
printf("Delete element at 1.beginning 2.ending 3.specific position:");
scanf("%d",&z);
if (z==1){
head=head->next;
head->prev=NULL;
temp->next=NULL;
return;
}
if(z==2){
while (temp->next!=tail){
temp=temp->next;
}
temp->next=NULL;
tail->prev=NULL;
tail=temp;
return;
}
if (z==3){
printf("Enter the position:");
scanf("%d",&p);
temp=head;
for (i=0;i<p-1;i++){
temp=temp->next;
}
temp->next=temp->next->next;
temp->next->next->prev=temp;
return;
}
}

Page 29 Roll no:160121748021


void search(){
int l=0,key;
struct node *temp;
temp=head;
printf("Enter element to be searched:");
scanf("%d",&key);
if (head==NULL){
printf("The list is empty\n");
}
while ((temp->data!=key) &&(temp->next!=NULL)){
temp=temp->next;
l++;
}
if ((temp->next==NULL)&&(temp->data!=key)){
printf("Element is not found\n");

Page 30 Roll no:160121748021


}
else{
printf("Element is found at location %d\n",l);
}
}
void insertion(){
int z,p,i;
struct node *temp,*new1;
new1=(struct node *)malloc(sizeof(struct node));
printf("Insert element at 1.beginning 2.ending 3 specific position:");
scanf("%d",&z);
printf("Enter the data:");
scanf("%d",&new1->data);
if (z==1){
new1->next=head;
new1->prev=NULL;
head->prev=new1;
head=new1;
return;
}
if(z==2){
new1->next=NULL;
new1->prev=tail;
tail->next=new1;
tail=new1;
return;
}
if (z==3){
printf("Enter the position:");
scanf("%d",&p);
temp=head;
for (i=0;i<p-1;i++){
temp=temp->next;
}
new1->next=temp->next;
temp->next=new1;
new1->prev=temp;
new1->next->prev=new1;
temp=new1;
return;
}
}
void main(){
int x=1,a;

Page 31 Roll no:160121748021


while(x==1){
printf("MENU LIST\n");
printf("1.create\n2.Insert\n3.delete\n4.search\n5.traverse\n6.exit\n");
printf("enter your choice:");
scanf("%d",&a);
switch(a){
case 1:create();
break;
case 2:insertion();
break;
case 3:deletion();
break;
case 4:search();
break;
case 5:traverse();
break;
case 6:exit(0);
}
printf("Do you want to continue the process(YES-1/NO-0):");
scanf("%d",&x);
}
}

OUTPUT:

Page 32 Roll no:160121748021


CONCLUSION:
Time complexity:
The time required to insert and delete the elements is Θ(1) and to traverse the
elements is Θ(n).
Same complexity:
Page 33 Roll no:160121748021
The space complexities for all the operations is Θ(1) since no new space is required.

7.AIM: To reverse elements in a linked list

DESCRIPTION: If the linked list has two or more elements, we can use


three pointers to implement an iterative solution. We use a function
to reverse the linked list. Passing the head pointer as the sole argument,
the function will return the head of the reversed list.

ALGORITHM:
algorithm for push operation:
step 1:allocate memory for the new node and name it as new_node
step 2:set new_node->data=val
step 3:if top=null
set new_node->next=null
set top=new_node
else
set new_node->next=top
set top=new_node
step 4:end if
step 5:end
algorithm for pop operation:
step 1:if top=null
print “underflow”
goto step 5
step 2:end if
step 3:set ptr=top
step 4:set top=top->next
step 5:free ptr
step 6:end
Page 34 Roll no:160121748021
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

static void reverse(struct Node** head_ref)


{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;
current = next;
}
*head_ref = prev;
}

void push(struct Node** head_ref, int new_data)


{
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void printList(struct Node* head)


{
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
Page 35 Roll no:160121748021
}

int main()
{

struct Node* head = NULL;

push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);

printf("Given linked list\n");


printList(head);
reverse(&head);
printf("\nReversed linked list \n");
printList(head);
getchar();
}

OUTPUT:

CONCLUSIONS:

Page 36 Roll no:160121748021


Time complexity:

Θ(n) time is taken to reverse a linked list since n elements are traversed.

Space complexity:
Θ(1) space is required as no new space is used.

Page 37 Roll no:160121748021


8.AIM: To perform operations on stacks using arrays.

DESCRIPTION: Stack is an abstract data type with a bounded(predefined)


capacity. It is a simple data structure that allows adding and removing elements in a
particular order. Every time an element is added, it goes on the top of the stack and
the only element that can be removed is the element that is at the top of the stack, just
like a pile of objects.

The basic operations to be served by stocks


1.push
2.pop
3.top
4.display
ALGORITHM:

algorithm for push operation:

step 1: if top = max-1

print overflow

step 2:end if

step 3: set top = top + 1

step 4: set stack[top] = value

step 5: end

algorithm for pop operation:

step 1: if top = null

print underflow
Page 38 Roll no:160121748021
step 2:end if

step 3: set val = stack[top]

step 4: set top = top - 1

step 5: end

algorithm for peek operation:

step 1: if top = null

print stack is empty

goto step 3

step 2: return stack[top]

step 3: end

PROGRAM:
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
while(choice != 4)
{
printf("MENU LIST\n");
printf("1.Push\n2.Pop\n3.Show\n4.Exit");
printf("Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
Page 39 Roll no:160121748021
break;
}
case 3:
{
show();
break;
}
case 4:
{
printf("Exiting");
break;
}
default:
{
printf("Enter a valid choice ");
}
};
}
}
void push ()
{
int x;
if (top==n )
printf("\n Too many elements");
else
{
printf("Enter the value");
scanf("%d",&x);
top=top +1;
stack[top]=x;
}
}

void pop ()
{
if(top==-1)
printf("No elements");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
{ printf("%d\n",stack[i]);
}
Page 40 Roll no:160121748021
if(top == -1)
{
printf("Stack is empty");
}
OUTPUT:

Page 41 Roll no:160121748021


CONCLUSION:
Time Complexity:
In the push function a single element is inserted at the last position. This takes a
single memory allocation operation which is done in constant time.
In array implementation, only an arithmetic operation is performed i.e., the
top pointer is decremented by 1. This is a constant time function.
Only a memory address is accessed.
Hence all the operations take O(1) time.

Space Complexity:
O(1), As no extra space is being used

Page 42 Roll no:160121748021


9.AIM: To perform operations on stacks using linked list.

DESCRIPTION: A stack is represented using nodes of a linked list. Each nodes


consists os two parts:data and next (storing the address of next node).The data part of
each node contains the assigned value and the next points to the node containing the
next item in the stack.The top refers top the topmost node in the stack.

Various operations performed are


1.push
2.pop
3.top
4.display
ALGORITHM:
algorithm for push operation:
step 1:allocate memory for the new node and name it as new_node
step 2:set new_node->data=val
step 3:if top=null
set new_node->next=null
set top=new_node
else
set new_node->next=top
set top=new_node
step 4:end if
step 5:end
algorithm for pop operation:
step 1:if top=null
print “underflow”
goto step 5
step 2:end if
step 3:set ptr=top
step 4:set top=top->next
step 5:free ptr
step 6:end

Page 43 Roll no:160121748021


PROGRAM:
//Stack using linked list
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*top=NULL,*new,*temp;

void push(){
new=(struct node*)malloc(sizeof(struct node));
printf("enter the data:\n");
scanf("%d",&new->data);
if(top==NULL){
new->next=NULL;
top=new;
}
else{
new->next=top;
top=new;
}
}
void pop(){
if(top==NULL){
printf("Stack is empty\n");
}
else{
temp=top;
top=top->next;
temp->next=NULL;
printf("element deleted:%d",temp->data);
}
}
void Top(){
if(top==NULL){
printf("Stack is empty\n");
}
else{
printf("Top most element is %d\n",top->data);
}
}
void display(){
temp=top;
if(temp==NULL){
printf("Stack is empty\n");
Page 44 Roll no:160121748021
}
else{
while(temp->next!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void main(){
int choice = 1,c;

while(choice){
printf("What do you want to perform?\n");
printf("1.Push\n2.Pop\n3.Top\n4.Display\n");
scanf("%d",&c);
switch(c){
case 1 :push();
break;
case 2 : pop();
break;
case 3 : Top();
break;
case 4 : display();
break;
default : printf("Invalid number\n");
}
printf("\nDo you want to continue?\n");
printf("1.YES\n0.NO\n");
scanf("%d",&choice);
}
}

Page 45 Roll no:160121748021


OUTPUT:

Page 46 Roll no:160121748021


CONCLUSION:
Time Complexity:
Only a new node is created and the pointer of the last node is updated. This
includes only memory allocation operations. Hence insertion is done in constant
time.
Only the first node is deleted and the top pointer is updated. This is a constant time
operation and also, It takes constant time to access the top element.

Space Complexity:
No extra space is used to insert, delete, and access the elements.
Page 47 Roll no:160121748021
Hence, O(1) space is used
10.AIM: Converting Infix expression to postfix expression.

DESCRIPTION:
When the operator is written in between the operands, then it is known as infix
notation. The postfix expression is an expression in which the operator is written after
the operands. To convert infix expression to postfix expression, stack data structure is
used. By scanning the infix expression from left to right, when we will get any
operand, simply add them to the postfix form, and for the operator and parenthesis, add
them in the stack maintaining the precedence of them.Here only {+, −, ∗,/, ^} operators
are considered, other operators are neglected.

SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
char s[100];
int top=-1;
void push(char z){
top=top+1;
s[top]=z;
}
char pop(){
if(top == -1)
return -1;
else
return s[top--];
}
int priority(char p){
switch(p){
case '(':return 0;
case '+':
case '-':return 1;
case '*':
case '/':return 2;
case '^':return 3;
default:return 0;
Page 48 Roll no:160121748021
}
}
void main(){
char ex[100],*e,c;
printf("Enter the expression: ");
scanf("%s",&ex);
e=ex;
printf("Postfix expression :");
while(*e!='\0'){
if (isalnum(*e)){
printf("%c",*e);
}
else if (*e=='('){
push(*e);
}
else if (*e==')'){
while((c = pop()) != '(')
printf("%c", c);
}
else{
while(priority(s[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e=e+1;
}
while (top!=-1){
printf("%c",pop());
}

}
OUTPUT:

Page 49 Roll no:160121748021


CONCLUSION:
Time complexity:
Worst case time complexity: Θ(n^2)
Average case time complexity: Θ(n^2)
Best case time complexity: Θ(n^2)
Complexity of algorithm in both worst and best case is O(n^2), as expression is iterated
two times simultaneously, firstly for scanning the infix expression and secondly while
poping out of stack.

Space Complexity:
 Θ(n)
For storing Infix expression of n literals the space complexity is O(n) and for stack to
hold atmost n literals the space complexity is O(n)

Page 50 Roll no:160121748021


11.AIM: Evaluation of postfix expression.

DESCRIPTION:

Postfix notation is a notation for writing arithmetic expressions in which the operands
appear before their operators. There are no precedence rules to learn, and parentheses
are never needed. Evaluation of postfix expression can be easily done using stack.
From the postfix expression, when some operands are found, pushed them in the stack.
When some operator is found, two items are popped from the stack and the operation is
performed in correct sequence. After that, the result is also pushed in the stack for
future use. After completing the whole expression, the final result is also stored in the
stack top.

SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
char s[100];
int top=-1;
void push(char z){
top=top+1;
s[top]=z;
}
char pop(){
if(top == -1)
return -1;
else
return s[top--];
}
Page 51 Roll no:160121748021
int priority(char p){
switch(p){
case '(':return 0;
case '+':
case '-':return 1;
case '*':
case '/':return 2;
case '^':return 3;
default:return 0;
}
}
void main(){
char ex[100],*e,c;
printf("Enter the expression: ");
scanf("%s",&ex);
e=ex;
printf("Postfix expression :");
while(*e!='\0'){
if (isalnum(*e)){
printf("%c",*e);
}
else if (*e=='('){
push(*e);
}
else if (*e==')'){
while((c = pop()) != '(')
printf("%c", c);
}
else{
while(priority(s[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e=e+1;
}
while (top!=-1){
printf("%c",pop());
}

OUTPUT:

Page 52 Roll no:160121748021


CONCLUSIONS:
Time complexity:
Worst case time complexity: Θ(n^2)
Average case time complexity: Θ(n^2)
Best case time complexity: Θ(n^2)
Complexity of algorithm in both worst and best case is O(n^2), as expression is iterated
two times simultaneously, firstly for scanning the infix expression and secondly while
poping out of stack.

Space complexity: Θ(n)
For storing Infix expression of n literals the space complexity is O(n) and for stack to
hold atmost n literals the space complexity is O(n)

Page 53 Roll no:160121748021

You might also like