0% found this document useful (0 votes)
10 views

010-bds record (1)

Uploaded by

010 Monitha Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

010-bds record (1)

Uploaded by

010 Monitha Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

BASIC DATA STRUCTURES

LAB RECORD
CYCLE 1 & 2

NAME: Chinnala Monitha Sai


BRANCH: EEE
CLASS: D1
ROLL NUMBER:160120734010

160120734010
WEEK-1 DATE : 29-09-2021
1) Write a C program to print elements in array and calculate sum of all the
elements in an array
AIM: To print elements in an array and calculate sum.
Description: To insert the elements in to Array and transverse (display) them and
then calculate sum.
ALGORITHM :
Transversal:
Step:1 intilaize the array elements ,I,and sum=0
Step:2 reading the array elements using loop
for(i=0;i<n;i++)
step:3 To transverse the array elements
step:4 Addition of the array elements
sum=sum+a[i]
step:5 printing the sum of array elements
CODE:
#include<stdio.h>
int main()
{
int a[5],sum=0,i,count;
printf("enter array elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("array elements");
for(i=0;i<5;i++)
160120734010
{
printf("\n%d ",a[i]);
}
for(i=0;i<5;i++)
{
sum=sum+a[i];
}
printf(" sum %d",sum”); }
INPUT : 1 2 3 4 5
OUTPUT : Sum = 15

2)Write a program to Insert elements in to the array.


AIM: To insert elements in to Array
Description: Input the Array elements , the position of the new element is to be
given and insert the new element at the position and shift rest of the elements to
right by one position.
ALGORITHM :
Insertion :
Step:1 intialize the array elements ,I,position,element.
Step 2: reading the array elements
Step 3: for(i=4;i>=position;i--)
array[i+1]=array[i]
step 4: then the position of the element
array[position]=element
step 5: then print the inserted elements
CODE:
#include<stdio.h>
160120734010
int main()
{
int a[5],i,position,element;
printf("enter position");
scanf("%d",&position);
printf("enter element");
scanf("%d",&element);
printf("enter array elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
a[position]=element;
printf("array element");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]); } }
INPUT : enter position : 3
Enter element: 7
1 2 3 4 5
OUTPUT : 1 2 3 7 4 5

3)Write A Program to delete an element from the Array


AIM : To delete an Element from Array
DESCRIPTION: Input the array elements , give the position of the element to be
deleted and the element will be deleted , shift the rest of the elements to left by one
position.
160120734010
ALGORITHM :
Deletion :
Step:1 initialize the array elements and the position
Step 2:read the array elements and scan
Step 3: for deleting
for(i=position ;i<5;i++)
array[i]=array[i+1]
step:4 printing the deleted array
CODE :
#include<stdio.h>
int main()
{
int a[5],i,position,element;
printf("enter position");
scanf("%d",&position);
printf("enter element");
scanf("%d",&element);
printf("enter array elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
a[position]=element;
printf("array element");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
160120734010
}
}
INPUT : enter the position : 3
enter array elements 1 2 5 6 7
OUTPUT : 1 2 5 7

4)Write a program to Search elements in an Array


Aim : To Search an element in an Array
DESCRIPTION : Searching: Input the array elements, give the element to be
searched . Traverse the array and check if the element is present in the array Display
“YES” if it is present in the array else display “NO”
ALGORITHM :
searching :
step 1: initialize and declare the array elements ,sum=0(sum is a counting variable).
step 2: read the position
step 3: for searching the element
if(x==a[i])
sum++
break
step 4: if(sum==1)
then print element is found
else
element is not found .
CODE:
#include<stdio.h>
int main()
{int a[5]={1, 50, 6 ,7 ,8},i,x,sum=0;
printf("enter value of x"); 160120734010
scanf("%d",&x);
for(i=0;i<5;i++)
if(x==a[i])
{
sum++;
break;
}
if(sum==1)
printf("elemenet found %d",i);
else
printf("element not found",i);
}
INPUT : enter value of x 50
OUTPUT : element found at 2

5)Write a program to Update Elements Available at different positions in an Array.


AIM : To Update elements in an Array
DESCRIPTION : Input the array elements , give the position where new element to be
updated and the new element will be updated at the given position in the array
ALGORITHM : updation:
step 1: initialize the array elements ,element,position.
step 2: read the array elements and position and element
step 3: for updation
a[position-1]=element
step 4: print the updated elements
CODE :
#include <stdio.h>
Int main()
160120734010
{
Int array[5], element , position
Printf(“enter element”);
Scanf(“%d”,&element);
Printf(“enter position”);
Scanf(“%d”,&position);
for(i=0; i<5 ; i++)
{ scanf(“%d”,&array[i]) ; }
a[Position-1]=element ;
printf(“After updation Array elements are”);
for(i=0 ; i<5 ; i++ );
{
Printf(“%d”,a[i]); }
INPUT : enter elements : 5
Enter position : 3
Enter array elements 9 8 7 1 2
OUTPUT : array elements
9 8 7 5 2
160120734010
WEEK – 2 DATE : 13-10-2021
1) Implement all the Opertaions of stack using Array
AIM : To implement push ; pop ; peek; isfull; isempty operations in stack using array
DESCRIPTION :This program is used to implement the all the operations of the stack ,
pop()-to remove the element from the stack.
push()-to insert the elements in the stack.
peek()-to print the top most element.
isempty()-to whether the stack is empty or not.
isfull() -to check whether the stack is full or not.
ALGORITHM : intilization of the stack
Step:1Top=-1 if top is -1 then
Return 1 other wise return 0.
Step:2 checking whether stack is full
If top is equal to max size -1 then return 0
Else return 0;
Step:3 To return the top most element (peek)
Using stack[top].
Step 4: To delete the elements from the stack
If stack is not empty then check for under flow condition.
Step 5: To insert the element in the stack
then check for condition whether the stack is not full (over flow)
else inseretion is possible.
Step 6:Then inserting the elements using push()
To print the return the element using peek()
Checking whether the stack is full or empty.
CODE :
#include<stdio.h>
Int size=8; 160120734010
Int stack[8];
Int top=-1;
Int isempty()
{if(top==-1)
Return 1;
Else
Return 0;
}
Int isfull()
{if(top==size)
return 1;
else
return 0;}
int peek()
{return stack(top);}
Int pop()
{int data;
If (!isempty())
{data=stack[top];
top=top-1;
return data;
}
Else
{printf(“underflow”);
}
Int push(int data)
{ if(!isfull())
{top=top+1; 160120734010
Stack[top]=data;}
else
{printf(“overflow”);}
Int main()
{push(1);
Push(2);
Push(3);
Push(4);
Push(5);
Push(6);
Push(7);
Push(8);
Printf(“elements on top %d”,peek);
Printf(“element \n”);
While(!isemtpy())
{int data=pop();
Printf(“%d \n”, data);}
Printf(“stack full: %s \n”,isfull?”true” ,”false”);
Printf(“stack empty %s \n, isempty ? “true “,”false”);}
}
Output:
Element at the top 8
Elements
1 5
2 6
3 7 Stack full : false
4 8 Stack empty : true
160120734010
WEEK---- 3 Date : 27-10-2021
1) Implementation of single Linked List using Array
AIM : Implementation of Linkedlist
DESCRIPTION : creating a new node and inserting the elements into the node.
ALGORITHM :
Step-1 : Declare the user defined function
Struct node
Step-2 : Define node structure with two elements data and next
Step-3 : Define *head *newnode and *temp (pointers)
Step-4 : Initialize head==0 and Allocate elements In to linked list (using Dynamic
memory allocation)
(structnode*)malloc(size of (structnode))
Step-5 : printf(“enter data”)
Scanf(“%d”,&newnode->data)
Step-6 : Assign newnode-> next =0
When head==0 head=temp=newnode
tempnext value =newnode

Step-7 : If you want to continue enter 1 else 0


Step-8 : when temp!=0
temp=head
temp= temp->next
to print all the entered elements
END
CODE :
#include<stdio.h>
#include<string.h>
#include<studlib.> 160120734010
Void main()
{ struct node
{ int data;
Struct node *next;
};
Struct node *head, *newnode,*temp;
head=0;
int choice;
While(choice)
newnode=(structnode*)malloc(sizeof(structnode));
Printf(“enter data”);
scanf(“%d”&newnode->data);
newnode->next=0;
if(head==0)
{head=temp=newnode;}
else
{temp->next=newnode;
temp=newnode;}
printf(“enter your choice”(0,1)?);
scanf(“%d”,choice);
} INPUT : Enter your choice -1
temp=head; Enter the value : 1
while(temp!=0) Enter your choice 1
{printf(“%d”,temp->data); Enter the value : 2
temp=temp->next; Enter your choice 1
} Enter the value : 3
} OUTPUT : 1 2 3
160120734010
WEEK- 4 Date : 03-11-2021
Insertion of Elements in Linked list
1) AT BEGINNING
AIM : To insert an element at the beginning of the linked list
DESCRIPTION: while inserting element in linked list at the beginning then the head
must points to the inserting elements .
Insertion at the beginning of linked list

Step 1: IF PTR = NULL

WriteOVERFLOW
GotoStep7
[END OF IF]

Step 2: SET NEW_NODE = PTR

Step 3: SET PTR = PTR → NEXT

Step 4: SET NEW_NODE → DATA = VAL

Step 5: SET NEW_NODE → NEXT = HEAD

Step 6: SET HEAD = NEW_NODE

Step 7: EXIT

CODE :

#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
160120734010
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beginsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void beginsert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *)
);
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
OUTPUT : Enter the item which you want to insert 12
Node Inserted
Press 0 to insert more
0
Enter the item which you want to insert 23
Node Inserted
Press 0 to insert more
1 .

160
120734010
2)AT THE END
AIM : To Insert an element at the End of the list
DESCRIPTION :Here directly we cannot insert the elements at the
end . We need to create a link using temp variable to the all
elements and insert .
INSERTION At the END : Algorithm

Step1: IFPTR=NULLWriteOVERFLOW
GotoStep1
[END OF IF]

Step 2: SET NEW_NODE = PTR

Step 3: SET PTR = PTR - > NEXT

Step 4: SET NEW_NODE - > DATA = VAL

Step 5: SET NEW_NODE - > NEXT = NULL

Step 6: SET PTR = HEAD

Step 7: Repeat Step 8 while PTR - > NEXT != NULL

Step8: SETPTR=PTR->NEXT
[END OF LOOP]

Step 9: SET PTR - > NEXT = NEW_NODE

Step 10: EXIT

CODE :
#include<stdio.h>
#include<stdlib.h>
void lastinsert(int);
struct node {
int data;
struct node *next;
}; struct node *head;
void main () { int choice,item;
do {
160120734010
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
lastinsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void lastinsert(int item)
{
struct node *ptr = (struct node*)malloc(sizeof(struct node
));
struct node *temp;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");

}
} }
160120734010
OUTPUT : Enter the item which you want to insert 12
Node Inserted
Press 0 to insert more?
0
Enter the item which you want to insert 23
Node inserted
Press 0 to insert more
1

3)AT PARTICULAR POSITION


AIM : To insert element in linkedlist at a specified position
DESCRIPTION: Here inserting a element in between then the position must greater
then count then inserting the elements is not possible . Else it is possible.o
Insertion At particular position : Algorithm

STEP 1: IF PTR = NULL

WRITEOVERFLOW
GOTOSTEP12
END OF IF

STEP 2: SET NEW_NODE = PTR

STEP 3: NEW_NODE → DATA = VAL

STEP 4: SET TEMP = HEAD

STEP 5: SET I = 0

STEP 6: REPEAT STEP 5 AND 6 UNTIL I<loc< li=""></loc<>


STEP 7: TEMP = TEMP → NEXT

STEP 8: IF TEMP = NULL

WRITE"DESIREDNODENOTPRESENT"
GOTOSTEP12
ENDOFIF
END OF LOOP

STEP 9: PTR → NEXT = TEMP → NEXT


160120734010

STEP 10: TEMP → NEXT = PTR

STEP 11: SET PTR = NEW_NODE

o STEP 12: EXIT

CODE :

#include<stdio.h>
#include<stdlib.h>
void randominsert(int);
void create(int)
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item,loc;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
if(head == NULL)
{
create(item);
}
else
{
randominsert(item);
}
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
} void create(int item)
160120734010
{
struct node *ptr = (struct node *)malloc(sizeof(struct nod
e *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
} }
void randominsert(int item)
{
struct node *ptr = (struct node *) malloc (sizeof(struct no
de));
struct node *temp;
int i,loc;
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else {
printf("Enter the location");
scanf("%d",&loc);
ptr->data = item;
temp=head;
for(i=0;i<loc;i++) {
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
} }
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
160120734010
} }

OUTPUT :

Enter the item which you want to insert 12


Node inserted
Press 0 to insert more
1
160120734010

WEEK----5 Date : 03-11-2021


DELETION
1)AT BEGINNING
AIM : To delete an element from the beginning of the linked list
DESCRIPTION : For deletion from the beginning we must have a head value to
temp . And head must equal to head next or temp next.
DELETION : At Beginning
Algorithm :

Step 1: IF HEAD = NULL

WriteUNDERFLOW
GotoStep5
[END OF IF]

Step 2: SET PTR = HEAD

Step 3: SET HEAD = HEAD -> NEXT

Step 4: FREE PTR

Step 5: EXIT
CODE :

#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;

160120734010
/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);

printf("\nData in the list \n");


displayList();

printf("\nPress 1 to delete first node: ");


scanf("%d", &choice);

/* Delete first node from list */


if(choice == 1)
deleteFirstNode();

printf("\nData in the list \n");


displayList();

return 0;
}

/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));

/*
* If unable to allocate memory for head node
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
Else
{
/*
* In data of node from the user
*/
printf("Enter the data of node 1: ");
160120734010
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data


newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode


temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
/*
* Deletes the first node of the linked list
*/
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{

printf("List is already empty.");


}
else
{
toDelete = head;
160120734010
head = head->next;

printf("\nData deleted = %d\n", toDelete->data);

/* Clears the memory occupied by first node*/


free(toDelete);

printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");


}
}

/*
* Displays the entire list
*/
void displayList()
{
struct node *temp;
/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
}

OUTPUT :
Enter the total number of nodes : 5
Enter the data of node 1 : 3
Enter the data of node 2 : 4
Enter the data of node 3 : 2
Enter the data of node 4 : 1
Enter the data of node 5 : 6
Singly LinkedList created Successfully
Data in the list
Data=3
160120734010
Data=4
Data=2
Data=1
Data=6
Press 1 to delete first node : 1
Data deleted = 3
Successfully Deleted first node from the list
Data in the list
Data= 4
Data= 2
Data= 1
Data= 6

2)AT THE END


AIM : To delete an element at the end of the linked list
DESCRIPTION : 2 scenarios in which a node is deleted from the end of linked list
Only one node in the linked list and more than one node in the linked list and last node
is deleted.
AT THE END : Algorithm

STEP 1 START

STEP 2 Store the element to be deleted


STEP 3 Check if header->link == NULL then Underflow cannot
delete element

STEP 4 Traverse to end of the list and create pointer to point


at end position

STEP 5 Use function free () i.e. free (ptr)

CODE :

#include <stdio.h>

#include <stdlib.h>

/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address 160120734010
}*head;

void createList(int n);


void deleteLastNode();
void displayList();

int main()
{
int n, choice;

/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);

printf("\nData in the list \n");


displayList();

printf("\nPress 1 to delete last node: ");


scanf("%d", &choice);

/* Delete last node from list */


if(choice == 1)
deleteLastNode();
printf("\nData in the list \n");
displayList();

return 0;
}

/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*
* If unable to allocate memory for head node
160120734010
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
/*
* Input data of node from the user
*/
printf("Enter the data of node 1: ");
scanf("%d", &data);

head->data = data; // Link the data field with data


head->next = NULL; // Link the address field to NULL

temp = head;

/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else {
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data


newNode->next = NULL; // Link the address field of newNode with NULL
temp->next = newNode; // Link previous node i.e. temp to the newNode
temp = temp->next;
}
}

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}
160120734010

/*
* Delete last node of the linked list
*/
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;

if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;

/* Traverse to the last node of the list */


while(toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}

if(toDelete == head)
{
head = NULL;
}
else
{
/* Disconnect link of second last node with last node */
secondLastNode->next = NULL;
}

/* Delete the last node */


free(toDelete);

printf("SUCCESSFULLY DELETED LAST NODE OF LIST\n");


}
}

/*
* Display entire list
*/
void displayList()
160120734010
{
struct node *temp;

/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print the data of current node
temp = temp->next; // Move to next node
}
}
}
OUTPUT :

Enter the total number of nodes : 5


Enter the data of node 1 : 6
Enter the data of node 2 : 7
Enter the data of node 3 : 8
Enter the data of node 4 : 9
Enter the data of node 5 : 4
Singly LinkedList created Successfully
Data in the list
Data=6
Data=7
Data=8
Data=9
Data=4
Press 1 to delete last node : 1
Data deleted = 3
Successfully Deleted last node from the list
Data in the list
Data= 6
Data= 7
Data= 8
Data= 9

160120734010

3)AT PARTICULAR POSITION :


AIM: To delete element from linked list at the particular position
DESCRIPTION :For deletion from middle node , we must have a pointer to the
previous to the node to be deleted .and need one more pointer called temp .
Algorithm :

STEP 1: IF HEAD = NULL

WRITEUNDERFLOW
GOTOSTEP10
END OF IF

STEP 2: SET TEMP = HEAD

STEP 3: SET I = 0

STEP 4: REPEAT STEP 5 TO 8 UNTIL I<loc< li=""></loc<>

STEP 5: TEMP1 = TEMP

STEP 6: TEMP = TEMP → NEXT


STEP 7: IF TEMP = NULL

WRITE"DESIREDNODENOTPRESENT"
GOTOSTEP12
END OF IF

STEP 8: I = I+1

END OF LOOP

STEP 9: TEMP1 → NEXT = TEMP → NEXT

STEP 10: FREE TEMP

STEP 11: EXIT

CODE :
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
160120734010
// Data struct node *next; // Address
} *head;
/* Functions used in program */
void createList(int n);
void deleteMiddleNode(int position);
void displayList();
int main()
{
int n, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter the node position you want to delete: ");
scanf("%d", &position);
/* Delete middle node from list */
deleteMiddleNode(position);
printf("\nData in the list \n");
displayList();

return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
160120734010
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data


newNode->next = NULL; // Link the address field of newNode with NULL
temp->next = newNode; // Link previous node i.e. temp to the newNode
temp = temp->next;
}
}

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}
void deleteMiddleNode(int position)
{
int i;
struct node *toDelete, *prevNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
prevNode = head;

for(i=2; i<=position; i++)


{
prevNode = toDelete;
toDelete = toDelete->next;

if(toDelete == NULL)
break;
}

if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;

160120734010
prevNode->next = toDelete->next;
toDelete->next = NULL;

/* Delete nth node */


free(toDelete);

printf("SUCCESSFULLY DELETED NODE FROM MIDDLE OF LIST\n");


}
else
{
printf("Invalid position unable to delete.");
}
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print the data of current node
temp = temp->next; // Move to next node
}}}
OUTPUT :
Enter the total number of nodes : 4
Enter the data of node 1 : 5
Enter the data of node 2 : 6
Enter the data of node 3 : 7
Enter the data of node 4 : 8
Singly LinkedList created Successfully
Data in the list Data in the list
Data=5 Data = 5
Data=6 Data = 6
Data=7 Data = 8
Data=8
Enter the node position you want to delete : 3
Successfully Deleted node from middle of the list

160120734010

WEEK----6 Date : 11-11-2021


1)AIM: To find factorial of a given number using Recursion
DESCRIPTION : Using the recursion we are finding the factorial of the given number
so that the code size is less , and using the stack recursion is done .
Factorial of a number -using RECURSIVE function :
Algorithm :
Step-1 : Start
Step-2: Read number n
Step-3 : Call function(n)
Step-4 : print factorial f
Step-5 : stop
factorial(n)
Step-6 : if n==1 then return 1
Else
F=n*factorial(n-1)
Step-7 : return f
CODE:
#include<stdio.h>
int main()
{
int factorial(int n);
int n,f;
printf("Enter the number:");
scanf("%d",&n);
f=factorial(n);
printf("factorial of number is %d",f);
}
int factorial (int n) 160120734010
{
int f;
if(n==1)
return 1 ;
else
f=n*factorial(n-1);
return f;
}
OUTPUT : Enter the number : 6
Factorial of number is 720

2)AIM: To find Fibonacci series of a given number using Recursion


DESCRIPTION : Fibonacci series means a series of numbers in which each number is
the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.
ALGORITHM :
FIbionacci series using RECURSION :
Algorithm :
Step-1 : Start
Step-2: initialize n,i (number which you need series up to is n)
Step-3 : Enter the number
Step-4 : Fibonacci series = Fibonacci(i-1)+Fibonacci(i-2)
Step-5 : print the series till n
Step-6 : Stop
CODE:
#include<stdio.h>
int fibonacci (int);
int main ()
{ 16012073401
0
int n,i;
printf("Enter the number: \n");
scanf("%d", &n);
printf("fibonacci series is : \n");
for(i=0; i<n; i++)
{
printf("%d",fibonacci (i));
}}
int fibonacci(int i) {
if(i==0) return 0;
else
if(i==1) return 1;
else return(fibonacci(i-1)+fibonacci(i-2));
}
OUTPUT: Enter the number: 6
Fibonacci series is : 0 1 1 2 3 5

160120734010
WEEK-7 Date : 24-11-2021
1)IMPLEMENTATION OF QUEUE
AIM : To perform Insertion, Deletion and Display operation in QUEUE
DESCRIPTION : Implementation of Queue operations in C The queue is
implemented without any functions and directly written with switch case.
Algorithm :
insert( )
1. If rear ≥ size-1
then write (‘overflow’)
2 Read item
3. rear← rear + 1
4. queue[rear]← item
5. If(front==-1)
6. front++;
7. stop

CODE :

#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
160120734010
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
OUTPUT : Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1

Enter no 1:10

Enter the Choice:1

Enter no 2:54

160120734010
Enter the Choice:1

Enter no 3:98

Enter the Choice:1

Enter no 4:234

Enter the Choice:3

Queue Elements are:


10
54
98
234

Enter the Choice:2


Deleted Element is 10
Enter the Choice:3

Queue Elements are:


54
98
234

Enter the Choice:4

Exit.

160120734010
WEEK 8 DATE: 15/12/2021
(a)LINEAR SEARCH
AIM: To Design, Develop and Implement a program in C for the Search operations
on Given Array of Elements.
DESCRIPTION: Search is a process of finding a value in a list of values. In other
words, searching is the process of locating given value position in a list of values.
Linear search algorithm finds a given element in a list of elements with O(n) time
complexity where n is total number of elements in the list. This search process starts
comparing search element with the first element in the list. If both are matched then
result is element found otherwise search element is compared with the next element in
the list. Repeat the same until search element is compared with the last element in the
list, if that last element also doesn't match, then the result is "Element not found in the
list". That means, the search element is compared with element by element in the list.
ALGORITHM: Linear search is implemented using following steps.
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!" and terminate
the function.
Step 4 - If both are not matched, then compare search element with the next element
in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in
the list.
Step 6 - If last element in the list also doesn't match, then display "Element is not
found!!!" and terminate the function
CODE
#include<stdio.h>
int main()
{
int i,data,n,k=0;
int found =0;
printf("enter the number of elements in array");
160120734010
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("enter the elements of array");
scanf("%d",&a[i]);
}
printf(" enter the element need to found");
scanf("%d",&data);
for(i=0;i<n;i++)
{
if(a[i]==data)
{
k=1;
i++;
}
if(a[i] != data)
{

k=0;
}
} Output :
if(k=1) Enter the series of the elements :
{ 5 10 15 19 20 22
printf("element is found"); Enter the element : 15
} Element found .
if(k=0)
{
printf("element is not found");
}
}
160120734010
(b) Binary Search
DESCRIPTION: Binary search algorithm finds a given element in a list of elements
with O(log n) time complexity where n is total number of elements in the list. The
binary search algorithm can be used with only a sorted list of elements. That means
the binary search is used only with a list of elements that are already arranged in an
order. This search process starts comparing the search element with the middle
element in the list. If both are matched, then the result is "element found. Otherwise,
we check whether the search element is smaller or larger than the middle element in
the list. If the search element is smaller, then we repeat the same process for the left
sub list of the middle element. If the search element is larger, then we repeat the same
process for the right sub list of the middle element. We repeat this process until we
find the search element in the list or until we left with a sublist of only one element.
And if that element also doesn't match with the search element, then the result is
"Element not found in the list".
ALGORITHM:
Binary search is implemented using following steps.
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list. Step 4
- If both are matched, then display "Given element is found!!!" and terminate func’n
Step 5 - If both are not matched, then check whether the search element is smaller or
larger than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and
5 for the left sublist of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5
for the right sublist of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until
sublist contains only one element.
Step 9 - If that element also doesn't match with the search element, then display
"Element is not found in the list!!!" and terminate the function.
CODE :
160120734010
#include <stdio.h>
int main(){
int c,first,last,middle,n,search,array[100];
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break; Output :
} Enter the elements : 2 5 16 18
25
Else Enter the element you want to
search:16
last = middle - 1; 16 is present at the index 2.
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
160120734010
WEEK 9 DATE: 22/12/2021

AIM: To Implement following sorting techniques in C over Given Array of Elements

a. Bubble Sort b. Selection Sort

(a)BUBBLE SORT DESCRIPTION:


Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in wrong order.

CODE

#include<stdio.h>
int main()
{
int a[10],i,j,n,flag,temp;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data");
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
160120734010
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if(flag==0)
break;
}
printf("The sorted array :");
for(i=0;i<n;i++)
{
printf("\t%d\t",a[i]);
}

}
Input : enter n value
Enter the array elements 5 4 3 2 1
Output : The sorted list is 1 2 3 4 5
(b) SELECTION SORT

DESCRIPTION: Selection sort is a simple sorting algorithm. This sorting algorithm


is an in-place comparison-based algorithm in which the list is divided into two parts,
the sorted part at the left end and the unsorted part at the right end. Initially, the sorted
part is empty and the unsorted part is the entire list. The smallest element is selected
from the unsorted array and swapped with the leftmost element, and that element
becomes a part of the sorted array. This process continues moving unsorted array
boundary by one element to the right. This algorithm is not suitable for large data sets
as its average and worst case complexities are of Ο(n2), where n is the number of
items.

160120734010

ALGORITHM:

Step 1 Set MIN to location 0

Step 2 Search the minimum element in the list


Step 3 Swap with value at location MIN

Step 4 Increment MIN to point to next element

Step 5 Repeat until list is sorted

CODE

#include<stdio.h>
int main()
{
int a[10],i,n,j,min,temp;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data");
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
160120734010
}
}
if(min!=0)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("The sorted array :");
for(i=0;i<n;i++)
{
printf("\t%d\t",a[i]);
}
}
Input : Enter n value : 5
Enter array elements : 5 6 9 8 7
Output: The sorted list is 5 6 7 8 9

16012073401
0
WEEK 10 DATE:
29/12/2021
QUICK SORT

DESCRIPTION: Quick sort is a fast sorting algorithm used to sort a list of elements.
Quick sort algorithm is invented by C. A. R. Hoare. The quick sort algorithm attempts
to separate the list of elements into two parts and then sort each part recursively. That
means it use divide and conquer strategy. In quick sort, the partition of the list is
performed based on the element called pivot. Here pivot element is one of the
elements in the list. The list is divided into two partitions such that "all elements to the
left of pivot are smaller than the pivot and all elements to the right of pivot are greater
than or equal to the pivot". Complexity of the Quick Sort Algorithm 68 | P a g e To
sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n- 2)+(n-
3)+......+1) = (n (n-1))/2 number of comparisons in the worst case. If the list is already
sorted, then it requires 'n' number of comparisons.

Worst Case : O(n2)

Best Case : O (n log n)

Average Case : O (n log n)

ALGORITHM:

In Quick sort algorithm, partitioning of the list is performed using following steps

. Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in
the list)

. Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.

Step 3 - Increment i until list[i] > pivot then stop.

Step 4 - Decrement j until list[j] < pivot then stop.

Step 5 - If i < j then exchange list[i] and list[j].

Step 6 - Repeat steps 3,4 & 5 until i > j.


160120734010
Step 7 - Exchange the pivot element with list[j] element

CODE

#include<stdio.h>
void quicksort(int number[25],int first,int last)
{
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}

temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
160120734010
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main(){
int i, count, number[25];

printf("How many elements are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
Output : Enter the array size : 7
Enter the array elements : 5 2 6 8 1 9 7
The sorted list is : 1 2 5 6 7 8 9
160120734010
WEEK 11 DATE: 24/01/2022
HEAP SORT

DESCRIPTION: Heap sort is one of the sorting algorithms used to arrange a list of
elements in order. Heap sort algorithm uses one of the tree concepts called Heap Tree.
In this sorting algorithm, we use Max Heap to arrange list of elements in Descending
order and Min Heap to arrange list elements in ascending order.

ALGORITHM:

The Heap sort algorithm to arrange a list of elements in ascending order is performed
using following steps.

Step 1 - Construct a Binary Tree with given list of Elements.

Step 2 - Transform the Binary Tree into Min Heap.

Step 3 - Delete the root element from Min Heap using Heapify method.

Step 4 - Put the deleted element into the Sorted list.

Step 5 - Repeat the same until Min Heap becomes empty.

Step 6 - Display the sorted list. Complexity of the Heap Sort Algorithm To sort
an unsorted list with 'n' number of elements, following are the complexities. Worst
Case : O(n log n)

Best Case : O(n log n)

Average Case : O(n log n)

CODE:

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
160120734010
}

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
160120734010
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100] = {15,5,20,1,7,10,30};
int n= size of(a), i;
printf("\n Before sorting the array elements are :");
PrintfArr(“a,n”);
HeapSort(a,n);
Printf(“\n After Sorting the array elements are:” )
printArray(arr, n);
return 0;
}
Output :
Before Sorting the array elements are :
15 5 20 1 7 10 30
After Sorting the array elements are :
1 5 7 10 15 20 30
1601207340
10
Assignment 2
Library Management system in C
Aim : To implement Library Management System in C :
Algorithm :
Step - 1: Declare a structure which holds data members
Step - 2: Declare variables which are used for loop
Step – 3: Use switch case to work on each module
Step – 4: Case 1 : For adding book information
Case 2 : For displaying book information
Case 3 : For finding no.of books in the library
Case 4 : Exit
CODE :
include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct library
{
char book_name[50];
char author[50];
int no.of pages;
float price;
};
int main()
{
struct library lib[100];
char authorname[30], bookname[30];
int i, j, count;
160120734010

i = j= count = 0;
while (j != 5) {
printf("\n\n1. Add book information\n2. Display book information\n");
printf("3. No.of books in the library\n");
printf("4. Exit");
printf("\n\nEnter one of the above: ");
scanf("%d", &j);
switch (j) {
// Add book
case 1:
printf("Enter book name: ");
scanf("%s", lib[i].book_name);
printf("Enter author name = ");
scanf("%s", lib[i].author);
printf("Enter pages = ");
scanf("%d", &lib[i].pages);
printf("Enter price = ");
scanf("%f", &lib[i].price);
count++;
i++;
break;
case 2:
printf("you have entered the following information\n");
for (i = 0; i < count; i++) {
printf("book name = %s “,lib[i].book_name);
printf("\t author name = %s", lib[i].author);
printf("\t pages = %d",lib[i].pages);
printf("\t price = %f", lib[i].price);
160120734010
}
break;
case 3:
printf(”no,of books in the library:%d”);
}
break;
case 4:
exit(0);
}
}
return 0;
}
Output:
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 1
Book name : Harry Potter
Author name : hp
Pages : 300
Price: 355.88
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 2
You have entered the following information
160120734010
Book name : Harry Potter
Author name : hp
Pages : 300
Price: 355
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 3
No. of books in the library : 10
1.Add book information
2. Display book information
3.No. of books in the library
4.Exit
Enter one of the above : 4
Exit
160120734010

You might also like