Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

21BCE3118

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;

for (int i = 2; i <= n; i++)


{
newnode = (struct node *)malloc(sizeof(struct node));
temp->link = newnode;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
}
}
printf("\nThe list is created\n");
}
else
printf("\nThe list is already created\n");
}

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));

// Enter the number


printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);

// 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));

// Enter the position and data


printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);

// 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;
}

// Function to delete from the front


// of the linked list
void deleteFirst()
{
struct node *temp;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
start = start->link;
free(temp);
}
}
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else
{
temp = start;
while (temp->link != 0)
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}

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;

// Traverse till position


while (i < pos - 1)
{
temp = temp->link;
i++;
}
position = temp->link;
temp->link = position->link;
free(position);
}
}

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;

while (temp != NULL)


{
if (max < temp->info)
max = temp->info;
temp = temp->link;
}
printf("\nMaximum number "
"is : %d ",
max);
}
}
void mean()
{
int a[10];
int i;
struct node *temp;

if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;

int sum = 0, count = 0;


float m;

while (temp != NULL)


{
sum = sum + temp->info;
temp = temp->link;
count++;
}

m = sum / count;

// Print the mean value


printf("\nMean is %f ", m);
}
}

// Function to sort the linked list


// in ascending order
void sort()
{
struct node *current = start;
struct node *index = NULL;
int temp;

if (start == NULL)
{
return;
}

else
{

while (current != NULL)


{
index = current->link;

while (index != NULL)


{

if (current->info > index->info)


{
temp = current->info;
current->info = index->info;
index->info = temp;
}
index = index->link;
}

current = current->link;
}
}
}

void reverseLL()
{
struct node *t1, *t2, *temp;
t1 = t2 = NULL;

if (start == NULL)
printf("List is empty\n");

else
{

while (start != NULL)


{

t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;

temp = start;

printf("Reversed linked "


"list is : ");

// Print the LL
while (temp != NULL)
{
printf("%d ", temp->info);
temp = temp->link;
}
}
}

int main()
{
int choice;
while (1)
{

printf("\n\t1 To see list\n");


printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To find maximum among"
" the elements\n");
printf("\t9 To find mean of "
"the elements\n");
printf("\t10 To sort element\n");
printf("\t11 To reverse the "
"linked list\n");
printf("\t12 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

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>

// Linked List Node


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

// Function to traverse the linked list


void traverse()
{
// List is empty
if (start == NULL) {
printf("\nList is empty\n");
return;
}
// Else print the Data
struct node* temp;
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->info);
temp = temp->next;
}
}

// Function to insert at the front


// of the linked list
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->prev = NULL;

// Pointer of temp will be


// assigned to start
temp->next = start;
start = temp;
}

// Function to insert at the end of


// the linked list
void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;

// 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;
}
}

// Function to insert at any specified


// position in the linked list
void insertAtPosition()
{
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;

// Enter the position and data


printf("\nEnter position : ");
scanf("%d", &pos);

// 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;
}
}

// Function to delete from the front


// of the linked list
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
}
}

// Function to delete from the end


// of the linked list
void deleteEnd()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}

// Function to delete from any specified


// position from the linked list
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;

// If DLL is empty
if (start == NULL)
printf("\nList is empty\n");

// Otherwise
else {
// Position to be deleted
printf("\nEnter position : ");
scanf("%d", &pos);

// If the position is the first node


if (pos == 1) {
deleteFirst(); // im,proved by Jay Ghughriwala on GeeksforGeeks
if (start != NULL) {
start->prev = NULL;
}
free(position);
return;
}

// Traverse till position


while (i < pos - 1) {
temp = temp->next;
i++;
}
// Change Links
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;

// Free memory
free(position);
}
}

// Driver Code
int main()
{
int choice;
while (1) {

printf("\n\t1 To see list\n");


printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

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>

// Structure to create a node with data and the next pointer


struct Node {
int data;
struct Node *next;
};
Node* top = NULL;

// Push() operation on a stack


void push(int value) {
struct Node *newNode;
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value; // assign value to the node
if (top == NULL) {
newNode->next = NULL;
} else {
newNode->next = top; // Make the node as top
}
top = newNode; // top always points to the newly created node
printf("Node is Inserted\n\n");
}

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");
}
}
}

//queue using linked list


#include < stdio.h >
#include < stdlib.h >

// Structure to create a node with data and the next pointer


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

struct node * front = NULL;


struct node * rear = NULL;

// Enqueue() operation on a queue


void enqueue(int value) {
struct node * ptr;
ptr = (struct node * ) malloc(sizeof(struct node));
ptr - > data = value;
ptr - > next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear - > next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}

// Dequeue() operation on a queue


int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node * temp = front;
int temp_data = front - > data;
front = front - > next;
free(temp);
return temp_data;
}
}

// Display all elements of the queue


void display() {
struct node * temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d--->", temp - > data);
temp = temp - > next;
}
printf("NULL\n\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;
}

//circular queue using linked list

#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;

struct Node* swap(struct Node* ptr1, struct Node* ptr2)


{
struct Node* tmp = ptr2->next;
ptr2->next = ptr1;
ptr1->next = tmp;
return ptr2;
}
int bubbleSort(struct Node** head, int count)
{
struct Node** h;
int i, j, swapped;

for (i = 0; i <= count; i++) {

h = head;
swapped = 0;

for (j = 0; j < count - i - 1; j++) {

struct Node* p1 = *h;


struct Node* p2 = p1->next;

if (p1->data > p2->data) {

*h = swap(p1, p2);
swapped = 1;
}

h = &(*h)->next;
}

if (swapped == 0)
break;
}
}

void printList(struct Node* n)


{
while (n != NULL) {
printf("%d -> ", n->data);
n = n->next;
}
printf("\n");
}
void insertAtTheBegin(struct Node** start_ref, int data)
{
struct Node* ptr1
= (struct Node*)malloc(sizeof(struct Node));

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;

/* start with empty linked list */


struct Node* start = NULL;
list_size = sizeof(arr) / sizeof(arr[0]);

/* Create linked list from the array arr[] */


for (i = 0; i < list_size; i++)
insertAtTheBegin(&start, arr[i]);

/* print list before sorting */


printf("Linked list before sorting\n");
printList(start);

/* sort the linked list */


bubbleSort(&start, list_size);

/* print list after sorting */


printf("Linked list after sorting\n");
printList(start);

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;
};

struct node* head = NULL;


struct node* sorted = NULL;

void push(int val)


{
struct node* newnode
= (struct node*)malloc(sizeof(struct node));
newnode->data = val;
newnode->next = head;
head = newnode;
}

(struct node* newnode)


{
if (sorted == NULL || sorted->data >= newnode->data) {
newnode->next = sorted;
sorted = newnode;
}
else {
struct node* current = sorted;

while (current->next != NULL


&& current->next->data < newnode->data) {
current = current->next;
}
newnode->next = current->next;
current->next = newnode;
}
}

void insertionsort()
{

struct node* current = head;

) {

struct node* next = current->next;

sortedInsert(current);

current = next;
}
head = sorted;
}

void printlist(struct node* head)


{
while (head != NULL) {
printf("%d->", head->data);
head = head->next;
}
printf("NULL");
}

int main()
{

push(5);
push(20);
push(4);
push(3);
push(30);

printf("Linked List before sorting:\n");


printlist(head);
printf("\n");

insertionsort(head);

printf("Linked List after sorting:\n");


printlist(head);
}
SELECTION SORT

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>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx;

for (i = 0; i < n-1; i++)


{

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]);
}
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

MERGE SORT USING DOUBLY LINKED LIST

// C program for merge sort on doubly linked list


#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next, *prev;
};

struct Node *split(struct Node *head);

// Function to merge two linked lists


struct Node *merge(struct Node *first, struct Node *second)
{
// If first linked list is empty
if (!first)
return second;

// If second linked list is empty


if (!second)
return first;

// Pick the smaller value


if (first->data < second->data)
{
first->next = merge(first->next,second);
first->next->prev = first;
first->prev = NULL;
return first;
}
else
{
second->next = merge(first,second->next);
second->next->prev = second;
second->prev = NULL;
return second;
}
}

// Function to do merge sort


struct Node *mergeSort(struct Node *head)
{
if (!head || !head->next)
return head;
struct Node *second = split(head);

// Recur for left and right halves


head = mergeSort(head);
second = mergeSort(second);

// Merge the two sorted halves


return merge(head,second);
}

// A utility function to insert a new node at the


// beginning of doubly linked list
void insert(struct Node **head, int data)
{
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = temp->prev = NULL;
if (!(*head))
(*head) = temp;
else
{
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}

// A utility function to print a doubly linked list in


// both forward and backward directions
void print(struct Node *head)
{
struct Node *temp = head;
printf("Forward Traversal using next pointer\n");
while (head)
{
printf("%d ",head->data);
temp = head;
head = head->next;
}
printf("\nBackward Traversal using prev pointer\n");
while (temp)
{
printf("%d ", temp->data);
temp = temp->prev;
}
}

// Utility function to swap two integers


void swap(int *A, int *B)
{
int temp = *A;
*A = *B;
*B = temp;
}

// Split a doubly linked list (DLL) into 2 DLLs of


// half sizes
struct Node *split(struct Node *head)
{
struct Node *fast = head,*slow = head;
while (fast->next && fast->next->next)
{
fast = fast->next->next;
slow = slow->next;
}
struct Node *temp = slow->next;
slow->next = NULL;
return temp;
}

// 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;
};

void swap ( int* a, int* b )


{ int t = *a; *a = *b; *b = t; }

struct Node *lastNode(struct Node *root)


{
while (root && root->next)
root = root->next;
return root;
}

struct Node* partition(struct Node *l, struct Node *h)


{
int x = h->data;

struct Node *i = l->prev;

for (struct Node *j = l; j != h; j = j->next)


{
if (j->data <= x)
{

i = (i == NULL) ? l : i->next;

swap(&(i->data), &(j->data));
}
}
i = (i == NULL) ? l : i->next;
return i;
}

void _quickSort(struct Node* l, struct Node *h)


{
if (h != NULL && l != h && l != h->next)
{
struct Node *p = partition(l, h);
_quickSort(l, p->prev);
_quickSort(p->next, h);
}
}

void quickSort(struct Node *head)


{

struct Node *h = lastNode(head);


_quickSort(head, h);
}

void printList(struct Node *head)


{
while (head)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

/* Function to insert a node at the


beginning of the Doubly Linked List */
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->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);

printf("Linked List before sorting \n");


printList(a);

quickSort(a);

printf("Linked List after sorting \n");


printList(a);

return 0;
}

Q4. Write C programs to search elements using linear and binary search techniques.

// linear search using linked list in c


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
void insert()
{
struct node *temp,*ptr;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty");
}
else
{
ptr=start;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr=start;
if(ptr==NULL)
{
printf("Empty List");
}
else
{
printf("Enter the item which you want to search:");
scanf("%d",&item);
while(ptr!=NULL)
{
if(ptr->data==item)
{
printf("item %d found at location %d",item,i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr=ptr->next;
}
if(flag==1)
{
printf("Item not found");
}
}
}
int main()
{
int choice;
while(1)
{
printf("\n1.Insert \n2.Display \n3.Linear Search \n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
exit(0);
break;
default:
printf("Wrong choice");
}
}
return 0;
}

//binary search using linked list in c


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
void insert()
{
struct node *temp,*ptr;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty");
}
else
{
ptr=start;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
}
}
void binary_search()
{
struct node *ptr;
int item,i=0,flag;
ptr=start;
if(ptr==NULL)
{
printf("Empty List");
}
else
{
printf("Enter the item which you want to search:");
scanf("%d",&item);
while(ptr!=NULL)
{
if(ptr->data==item)
{
flag=0;
break;
}
else
{
flag=1;
}
ptr=ptr->next;
}
if(flag==0)
{
printf("Item found");
}
else
{
printf("Item not found");
}
}
}
int main()
{
int choice;
while(1)
{
printf("\n1.Insert \n2.Display \n3.Binary Search \n4.Exit");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:display();
break;
case 3:binary_search();
break;
case 4:exit(0);
break;
default:printf("Invalid choice");
}
}
return 0;
}

You might also like