Professional Documents
Culture Documents
New Microsoft Word Document
New Microsoft Word Document
ARYAN CHUGH
DA 3
Q1. Write C programs to implement all operations (create, insert, delete, search, traverse, and
anything else you may like to code) of a) singly linked list b) doubly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *start = NULL;
void createList()
{
if (start == NULL)
{
int n;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
if (n != 0)
{
int data;
struct node *newnode;
struct node *temp;
newnode = (struct node *)malloc(sizeof(struct node));
start = newnode;
temp = start;
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
start->info = data;
void traverse()
{
struct node *temp;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
while (temp != NULL)
{
printf("Data = %d\n", temp->info);
temp = temp->link;
}
}
}
void insertAtFront()
{
int data;
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->info = data;
temp->link = start;
start = temp;
}
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = (struct node *)malloc(sizeof(struct node));
// Changes links
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL)
{
head = head->link;
}
head->link = temp;
}
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = (struct node *)malloc(sizeof(struct node));
// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else
{
printf("\nEnter index : ");
// Position to be deleted
scanf("%d", &pos);
position = (struct node *)malloc(sizeof(struct node));
temp = start;
void maximum()
{
int a[10];
int i;
struct node *temp;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
int max = temp->info;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
m = sum / count;
if (start == NULL)
{
return;
}
else
{
current = current->link;
}
}
}
void reverseLL()
{
struct node *t1, *t2, *temp;
t1 = t2 = NULL;
if (start == NULL)
printf("List is empty\n");
else
{
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;
temp = start;
// Print the LL
while (temp != NULL)
{
printf("%d ", temp->info);
temp = temp->link;
}
}
}
int main()
{
int choice;
while (1)
{
switch (choice)
{
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
reverseLL();
break;
case 12:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// If start is NULL
if (start == NULL) {
start = temp;
}
// Changes Links
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
// If start==NULL,
if (start == NULL) {
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}
// If position==1,
else if (pos == 1) {
// this is author method its correct but we can simply call
insertAtfront() function for this special case
/* newnode->next = start;
newnode->next->prev = newnode;
newnode->prev = NULL;
start = newnode; */
// now this is improved by Jay Ghughriwala on geeksforgeeks
insertAtFront();
}
// Change links
else {
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
newnode->info = data;
temp = start;
while (i < pos - 1) {
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
// If DLL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
// Position to be deleted
printf("\nEnter position : ");
scanf("%d", &pos);
// Free memory
free(position);
}
}
// Driver Code
int main()
{
int choice;
while (1) {
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
printf("Incorrect Choice. Try Again \n");
continue;
}
}
return 0;
}
Q2. Write C programs to implement the following using linked list. a) stack, b) queue c) circular
queue
#include <stdio.h>
#include <stdlib.h>
int pop() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
struct Node *temp = top;
int temp_data = top->data;
top = top->next;
free(temp);
return temp_data;
}
}
void display() {
// Display the elements of the stack
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("The stack is \n");
struct Node *temp = top;
while (temp->next != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
}
}
int main() {
int choice, value;
printf("\nImplementation of Stack using Linked List\n");
while (1) {
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped element is :%d\n", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
int main() {
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", & value);
enqueue(value);
break;
case 2:
printf("Popped element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d) //Insert elements in Queue
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n;
r = n;
n->next = f;
}
}
void dequeue() // Delete an element from Queue
{
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else if(f == r){
f = r = NULL;
free(t);
}
else{
f = f->next;
r->next = f;
free(t);
}
}
void print(){ // Print the elements of Queue
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}
}
int main()
{
int opt,n,i,data;
printf("Enter Your Choice:-");
do{
printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in
Queue \n3 for Delete the data from the Queue\n0 for Exit");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the number of data");
scanf("%d",&n);
printf("\nEnter your data");
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");
}
}while(opt!=0);
return 0;
}
Q3. Find out which of the following algorithms can be efficiently implemented using either singly
linked list or doubly linked list. State the reasons for each of the following. Write C programs to sort
the data using, a) bubble sort, b) insertion sort, c) selection sort, d) merge sort e) quick sort.
A. BUBBLE SORT
For bubble sort as we only need to compare the next element with the current element so no
need to transverse that much so in this case, we just use singly linked list as it saves space by not
storing the address of the previous elements and runs much faster.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
} Node;
h = head;
swapped = 0;
*h = swap(p1, p2);
swapped = 1;
}
h = &(*h)->next;
}
if (swapped == 0)
break;
}
}
ptr1->data = data;
ptr1->next = *start_ref;
*start_ref = ptr1;
}
// Driver Code
int main()
{
int arr[] = { 78, 20, 10, 32, 1, 5 };
int list_size, i;
return 0;
}
INSERTION SORT
A double-linked list is a linked data structure made up of nodes, which sequentially link records.
There are three fields in each node: two link fields and one data field. So it make it quicker to
access the previous data as the current struct stores the address of previous node
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
void insertionsort()
{
) {
sortedInsert(current);
current = next;
}
head = sorted;
}
int main()
{
push(5);
push(20);
push(4);
push(3);
push(30);
insertionsort(head);
Singly linked list will work best for the selection sort as we have to traverse the whole set of data and
find the minimum element there is no point in storing the address of the previous data which will
not be used and will waste space.
#include <stdio.h>
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
// Driver program
int main(void)
{
struct Node *head = NULL;
insert(&head,5);
insert(&head,20);
insert(&head,4);
insert(&head,3);
insert(&head,30);
insert(&head,10);
head = mergeSort(head);
printf("\n\nLinked List after sorting\n");
print(head);
return 0;
}
QUICK SORT USING DOUBLY LINKED LIST.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
i = (i == NULL) ? l : i->next;
swap(&(i->data), &(j->data));
}
}
i = (i == NULL) ? l : i->next;
return i;
}
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL) (*head_ref)->prev = new_node ;
(*head_ref) = new_node;
}
// Driver Code
int main(int argc, char **argv)
{
struct Node *a = NULL;
push(&a, 5);
push(&a, 20);
push(&a, 4);
push(&a, 3);
push(&a, 30);
quickSort(a);
return 0;
}
Q4. Write C programs to search elements using linear and binary search techniques.