0% found this document useful (0 votes)
2 views78 pages

C Programs for Search and Sort Algorithms

The document contains multiple C programs demonstrating various algorithms, including linear search (both iterative and recursive), bubble sort, selection sort, and insertion sort. It also provides a complete implementation of a singly linked list with functionalities for creation, insertion, deletion, and traversal. Each section includes code snippets and user interaction for input and output.

Uploaded by

Sirela Meena
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)
2 views78 pages

C Programs for Search and Sort Algorithms

The document contains multiple C programs demonstrating various algorithms, including linear search (both iterative and recursive), bubble sort, selection sort, and insertion sort. It also provides a complete implementation of a singly linked list with functionalities for creation, insertion, deletion, and traversal. Each section includes code snippets and user interaction for input and output.

Uploaded by

Sirela Meena
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

LINEAR SEARCH

#include<stdio.h>

int linearsearch(int a[],int n,int key)

int i;

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

if(a[i]==key)

return i;

return -1;

int main()

int a[100],n,key,pos,i;

printf("enter n value");

scanf("%d",&n);

printf("enter %d elements into array",n);

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

scanf("%d",&a[i]);

printf("enter search key elements");


scanf("%d",&key);

pos=linearsearch(a,n,key);

if(pos>=0)

printf("element found at %d position",pos+1);

else

printf("element not found");

return 1;

RECURSIVE LINEAR SEARCH

#include <stdio.h>

int LinearSearch(int arr[], int search,int index,int n);

int main()

int n, search, result, m = 0, arr[100];

printf("Program on Linear Search\n");

printf("-------\n");

printf("Enter the total elements in the array\n");

scanf("%d", &n);

printf("Enter the array elements\n");

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

scanf("%d", &arr[i]);

printf("Check whether these are the elements which you have entered\n");
for (int i = 0; i < n; i++)

printf("%d\n", arr[i]);

printf("Enter the element to search \n");

scanf("%d", &search);

result = LinearSearch(arr, search,0,n);

if (result != 0)

printf("Element found at pos %d\n ", result);

else

printf("Element not found");

return 0;

int LinearSearch(int arr[], int search,int index,int n)

int arrpos=0;

if(index>=n)

return 0;

if (arr[index] == search)
{

arrpos = index + 1;

return arrpos;

else

return LinearSearch(arr, search,index+1,n);

return arrpos;

// C program for implementation of Bubble sort

#include <stdio.h>

void swap(int* arr, int i, int j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

void bubbleSort(int arr[], int n) {

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

// Last i elements are already in place, so the loop

// will only num n - i - 1 times

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

if (arr[j] > arr[j + 1])

swap(arr, j, j + 1);

}
}

int main() {

int arr[] = { 6, 0, 3, 5 };

int n = sizeof(arr) / sizeof(arr[0]);

// Calling bubble sort on array arr

bubbleSort(arr, n);

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

printf("%d ", arr[i]);

return 0;

// C program for implementation of selection sort


#include <stdio.h>

void selectionSort(int arr[], int N) {

// Start with the whole array as unsored and one by

// one move boundary of unsorted subarray towards right

for (int i = 0; i < N - 1; i++) {

// Find the minimum element in unsorted array

int min_idx = i;

for (int j = i + 1; j < N; j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

// Swap the found minimum element with the first


// element in the unsorted part

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

int main() {

int arr[] = {64, 25, 12, 22, 11};

int N = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: \n");

for (int i = 0; i < N; i++) {

printf("%d ", arr[i]);

printf("\n");

// Calling selection sort

selectionSort(arr, N);

printf("Sorted array: \n");

for (int i = 0; i < N; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

// C program to implement insertion sort


#include <math.h>

#include <stdio.h>

void insertionSort(int arr[], int N) {

// Starting from the second element

for (int i = 1; i < N; i++) {

int key = arr[i];

int j = i - 1;

// Move elements of arr[0..i-1], that are

// greater than key, to one position to

// the right of their current position

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

// Move the key to its correct position

arr[j + 1] = key;

int main() {

int arr[] = { 12, 11, 13, 5, 6 };

int N = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: ");

for (int i = 0; i < N; i++) {

printf("%d ", arr[i]);

}
printf("\n");

// Calling insertion sort on array arr

insertionSort(arr, N);

printf("Sorted array: ");

for (int i = 0; i < N; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

Aim:
Write a C program that uses functions to perform the following on Singly Linked List: i)
Creation ii) Insertion iii) Deletion iv) Traversal

Source Code:
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int count=0;

struct node
{

int data;

struct node *next;

}*head,*newn,*trav;

//----------------------------------------------------------

void create_list()

int value;

struct node *temp;

temp=head;

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

printf("\nenter the value to be inserted");

scanf("%d",&value);

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;
}

temp->next=newn;

newn->next=NULL;

count++;

//----------------------------------------------------

void insert_at_begning(int value)

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

newn->next=head;

head=newn;

count++;

//----------------------------------------------------------
void insert_at_end(int value)

struct node *temp;

temp=head;

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

//------------------------------------------------------

int insert_at_middle()
{

if(count>=2)

struct node *var1,*temp;

int loc,value;

printf("\n after which value you want to insert : ");

scanf("%d",&loc);

printf("\nenter the value to be inserted");

scanf("%d",&value);

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

newn->data=value;

temp=head;

/* if(head==NULL)

head=newn;

head->next=NULL;

count++;

return 0;

else

{*/

while(temp->data!=loc)

temp=temp->next;
if(temp==NULL)

printf("\nSORRY...there is no %d element",loc);

return 0;

//var1=temp->next;

newn->next=temp->next;//var1;

temp->next=newn;

count++;

//}

else

printf("\nthe no of nodes must be >=2");

//----------------------------------------------------------------

int delete_from_middle()

if(count==0)

printf("\n List is Empty!!!! you can't delete elements\n");

else if(count>2)

{
struct node *temp,*var;

int value;

temp=head;

printf("\nenter the data that you want to delete from the list shown above");

scanf("%d",&value);

while(temp->data!=value)

var=temp;

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",value);

return 0;

if(temp==head)

head=temp->next;

else{

var->next=temp->next;

temp->next=NULL;

}
count--;

if(temp==NULL)

printf("Element is not avilable in the list \n**enter only middle elements..**");

else

printf("\ndata deleted from list is %d",value);

free(temp);

else

printf("\nthere no middle elemts..only %d elemts is avilable\n",count);

//------------------------------------------------------------------

int delete_from_front()

struct node *temp;

temp=head;

if(head==NULL)

printf("\nno elements for deletion in the list\n");

return 0;

else

printf("\ndeleted element is :%d",head->data);


if(temp->next==NULL)

head=NULL;

else{

head=temp->next;

temp->next=NULL;

count--;

free(temp);

//--------------------------------------------------------

int delete_from_end()

struct node *temp,*var;

temp=head;

if(head==NULL)

printf("\nno elemts in the list");

return 0;

else{
if(temp->next==NULL )

head=NULL;//temp->next;

else{

while(temp->next != NULL)

var=temp;

temp=temp->next;

var->next=NULL;

printf("\ndata deleted from list is %d",temp->data);

free(temp);

count--;

return 0;

//------------------------------------------------------

int display()

trav=head;

if(trav==NULL)

printf("\nList is Empty\n");
return 0;

else

printf("\n\nElements in the Single Linked List is %d:\n",count);

while(trav!=NULL)

printf(" -> %d ",trav->data);

trav=trav->next;

printf("\n");

//---------------------------------------------------------------

int main()

int ch=0;

char ch1;

head=NULL;

while(1)

printf("\[Link] linked list");

printf("\[Link] at begning of linked list");

printf("\[Link] at the end of linked list");


printf("\[Link] at the middle where you want");

printf("\[Link] from the front of linked list");

printf("\[Link] from the end of linked list ");

printf("\[Link] of the middle data that you want");

printf("\[Link] the linked list");

printf("\[Link]\n");

printf("\nenter the choice of operation to perform on linked list");

scanf("%d",&ch);

switch(ch)

case 1:

do{

create_list();

display();

printf("do you want to create list ,y / n");

getchar();

scanf("%c",&ch1);

}while(ch1=='y');

break;

case 2:

int value;
printf("\nenter the value to be inserted");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 3:

int value;

printf("\nenter value to be inserted");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 4:

insert_at_middle();

display();

break;

case 5:

delete_from_front();

display();
}break;

case 6:

delete_from_end();

display();

break;

case 7:

display();

delete_from_middle();

display();

break;

case 8:

display();

break;

case 9:

exit(1);

default:printf("\n****Please enter correct choice****\n");

}
}

getch();

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int count=0;

struct node

int data;

struct node *next;

}*head,*newn,*trav;

//----------------------------------------------------------

void create_list()

int value;

struct node *temp;

temp=head;

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

printf("\nenter the value to be inserted");

scanf("%d",&value);

newn->data=value;

if(head==NULL)

head=newn;
head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

//----------------------------------------------------

void insert_at_begning(int value)

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

}
else

newn->next=head;

head=newn;

count++;

//----------------------------------------------------------

void insert_at_end(int value)

struct node *temp;

temp=head;

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;
}

temp->next=newn;

newn->next=NULL;

count++;

//------------------------------------------------------

int insert_at_middle()

if(count>=2)

struct node *var1,*temp;

int loc,value;

printf("\n after which value you want to insert : ");

scanf("%d",&loc);

printf("\nenter the value to be inserted");

scanf("%d",&value);

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

newn->data=value;

temp=head;

/* if(head==NULL)

head=newn;

head->next=NULL;
count++;

return 0;

else

{*/

while(temp->data!=loc)

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",loc);

return 0;

//var1=temp->next;

newn->next=temp->next;//var1;

temp->next=newn;

count++;

//}

else

printf("\nthe no of nodes must be >=2");

}
}

//----------------------------------------------------------------

int delete_from_middle()

if(count==0)

printf("\n List is Empty!!!! you can't delete elements\n");

else if(count>2)

struct node *temp,*var;

int value;

temp=head;

printf("\nenter the data that you want to delete from the list shown above");

scanf("%d",&value);

while(temp->data!=value)

var=temp;

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",value);

return 0;

if(temp==head)

{
head=temp->next;

else{

var->next=temp->next;

temp->next=NULL;

count--;

if(temp==NULL)

printf("Element is not avilable in the list \n**enter only middle elements..**");

else

printf("\ndata deleted from list is %d",value);

free(temp);

else

printf("\nthere no middle elemts..only %d elemts is avilable\n",count);

//------------------------------------------------------------------

int delete_from_front()

struct node *temp;

temp=head;
if(head==NULL)

printf("\nno elements for deletion in the list\n");

return 0;

else

printf("\ndeleted element is :%d",head->data);

if(temp->next==NULL)

head=NULL;

else{

head=temp->next;

temp->next=NULL;

count--;

free(temp);

//--------------------------------------------------------

int delete_from_end()

{
struct node *temp,*var;

temp=head;

if(head==NULL)

printf("\nno elemts in the list");

return 0;

else{

if(temp->next==NULL )

head=NULL;//temp->next;

else{

while(temp->next != NULL)

var=temp;

temp=temp->next;

var->next=NULL;

printf("\ndata deleted from list is %d",temp->data);

free(temp);

count--;

return 0;
}

//------------------------------------------------------

int display()

trav=head;

if(trav==NULL)

printf("\nList is Empty\n");

return 0;

else

printf("\n\nElements in the Single Linked List is %d:\n",count);

while(trav!=NULL)

printf(" -> %d ",trav->data);

trav=trav->next;

printf("\n");

//---------------------------------------------------------------

int main()

int ch=0;
char ch1;

head=NULL;

while(1)

printf("\[Link] linked list");

printf("\[Link] at begning of linked list");

printf("\[Link] at the end of linked list");

printf("\[Link] at the middle where you want");

printf("\[Link] from the front of linked list");

printf("\[Link] from the end of linked list ");

printf("\[Link] of the middle data that you want");

printf("\[Link] the linked list");

printf("\[Link]\n");

printf("\nenter the choice of operation to perform on linked list");

scanf("%d",&ch);

switch(ch)

case 1:

do{

create_list();

display();

printf("do you want to create list ,y / n");

getchar();
scanf("%c",&ch1);

}while(ch1=='y');

break;

case 2:

int value;

printf("\nenter the value to be inserted");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 3:

int value;

printf("\nenter value to be inserted");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 4:

{
insert_at_middle();

display();

break;

case 5:

delete_from_front();

display();

}break;

case 6:

delete_from_end();

display();

break;

case 7:

display();

delete_from_middle();

display();

break;

case 8:

display();
break;

case 9:

exit(1);

default:printf("\n****Please enter correct choice****\n");

getch();

case 1:

do{

create_list();

display();

printf("do you want to create list ,y / n");

getchar();

scanf("%c",&ch1);

}while(ch1=='y');

break;
}

case 2:

int value;

printf("\nenter the value to be inserted");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 3:

int value;

printf("\nenter value to be inserted");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 4:

insert_at_middle();

display();

break;

}
case 5:

delete_from_front();

display();

}break;

case 6:

delete_from_end();

display();

break;

case 7:

display();

delete_from_middle();

display();

break;

case 8:

display();

break;

case 9:

{
exit(1);

default:printf("\n****Please enter correct choice****\n");

getch();

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int count=0;

struct node

int data;

struct node *next;

}*head,*newn,*trav;

//----------------------------------------------------------

void create_list()

int value;

struct node *temp;

temp=head;

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

printf("\nenter the value to be inserted");

scanf("%d",&value);
newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

//----------------------------------------------------

void insert_at_begning(int value)

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

newn->data=value;

if(head==NULL)

{
head=newn;

head->next=NULL;

count++;

else

newn->next=head;

head=newn;

count++;

//----------------------------------------------------------

void insert_at_end(int value)

struct node *temp;

temp=head;

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else
{

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

//------------------------------------------------------

int insert_at_middle()

if(count>=2)

struct node *var1,*temp;

int loc,value;

printf("\n after which value you want to insert : ");

scanf("%d",&loc);

printf("\nenter the value to be inserted");

scanf("%d",&value);

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

newn->data=value;

temp=head;
/* if(head==NULL)

head=newn;

head->next=NULL;

count++;

return 0;

else

{*/

while(temp->data!=loc)

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",loc);

return 0;

//var1=temp->next;

newn->next=temp->next;//var1;

temp->next=newn;

count++;

//}

}
else

printf("\nthe no of nodes must be >=2");

//----------------------------------------------------------------

int delete_from_middle()

if(count==0)

printf("\n List is Empty!!!! you can't delete elements\n");

else if(count>2)

struct node *temp,*var;

int value;

temp=head;

printf("\nenter the data that you want to delete from the list shown above");

scanf("%d",&value);

while(temp->data!=value)

var=temp;

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",value);

return 0;
}

if(temp==head)

head=temp->next;

else{

var->next=temp->next;

temp->next=NULL;

count--;

if(temp==NULL)

printf("Element is not avilable in the list \n**enter only middle elements..**");

else

printf("\ndata deleted from list is %d",value);

free(temp);

else

printf("\nthere no middle elemts..only %d elemts is avilable\n",count);

//------------------------------------------------------------------
int delete_from_front()

struct node *temp;

temp=head;

if(head==NULL)

printf("\nno elements for deletion in the list\n");

return 0;

else

printf("\ndeleted element is :%d",head->data);

if(temp->next==NULL)

head=NULL;

else{

head=temp->next;

temp->next=NULL;

count--;

free(temp);

}
}

//--------------------------------------------------------

int delete_from_end()

struct node *temp,*var;

temp=head;

if(head==NULL)

printf("\nno elemts in the list");

return 0;

else{

if(temp->next==NULL )

head=NULL;//temp->next;

else{

while(temp->next != NULL)

var=temp;

temp=temp->next;

var->next=NULL;

printf("\ndata deleted from list is %d",temp->data);


free(temp);

count--;

return 0;

//------------------------------------------------------

int display()

trav=head;

if(trav==NULL)

printf("\nList is Empty\n");

return 0;

else

printf("\n\nElements in the Single Linked List is %d:\n",count);

while(trav!=NULL)

printf(" -> %d ",trav->data);

trav=trav->next;

printf("\n");

}
//---------------------------------------------------------------

int main()

int ch=0;

char ch1;

head=NULL;

while(1)

printf("\[Link] linked list");

printf("\[Link] at begning of linked list");

printf("\[Link] at the end of linked list");

printf("\[Link] at the middle where you want");

printf("\[Link] from the front of linked list");

printf("\[Link] from the end of linked list ");

printf("\[Link] of the middle data that you want");

printf("\[Link] the linked list");

printf("\[Link]\n");

printf("\nenter the choice of operation to perform on linked list");

scanf("%d",&ch);

switch(ch)

case 1:

do{
create_list();

display();

printf("do you want to create list ,y / n");

getchar();

scanf("%c",&ch1);

}while(ch1=='y');

break;

case 2:

int value;

printf("\nenter the value to be inserted");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 3:

int value;

printf("\nenter value to be inserted");

scanf("%d",&value);

insert_at_end(value);

display();
break;

case 4:

insert_at_middle();

display();

break;

case 5:

delete_from_front();

display();

}break;

case 6:

delete_from_end();

display();

break;

case 7:

display();

delete_from_middle();

display();

break;
}

case 8:

display();

break;

case 9:

exit(1);

default:printf("\n****Please enter correct choice****\n");

getch();

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int count=0;

struct node

int data;

struct node *next;


}*head,*newn,*trav;

//----------------------------------------------------------

void create_list()

int value;

struct node *temp;

temp=head;

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

printf("\nenter the value to be inserted");

scanf("%d",&value);

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;
count++;

//----------------------------------------------------

void insert_at_begning(int value)

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

newn->next=head;

head=newn;

count++;

//----------------------------------------------------------

void insert_at_end(int value)

struct node *temp;


temp=head;

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

//------------------------------------------------------

int insert_at_middle()

if(count>=2)

{
struct node *var1,*temp;

int loc,value;

printf("\n after which value you want to insert : ");

scanf("%d",&loc);

printf("\nenter the value to be inserted");

scanf("%d",&value);

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

newn->data=value;

temp=head;

/* if(head==NULL)

head=newn;

head->next=NULL;

count++;

return 0;

else

{*/

while(temp->data!=loc)

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",loc);
return 0;

//var1=temp->next;

newn->next=temp->next;//var1;

temp->next=newn;

count++;

//}

else

printf("\nthe no of nodes must be >=2");

//----------------------------------------------------------------

int delete_from_middle()

if(count==0)

printf("\n List is Empty!!!! you can't delete elements\n");

else if(count>2)

struct node *temp,*var;

int value;

temp=head;
printf("\nenter the data that you want to delete from the list shown above");

scanf("%d",&value);

while(temp->data!=value)

var=temp;

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",value);

return 0;

if(temp==head)

head=temp->next;

else{

var->next=temp->next;

temp->next=NULL;

count--;

if(temp==NULL)

printf("Element is not avilable in the list \n**enter only middle elements..**");


else

printf("\ndata deleted from list is %d",value);

free(temp);

else

printf("\nthere no middle elemts..only %d elemts is avilable\n",count);

//------------------------------------------------------------------

int delete_from_front()

struct node *temp;

temp=head;

if(head==NULL)

printf("\nno elements for deletion in the list\n");

return 0;

else

printf("\ndeleted element is :%d",head->data);

if(temp->next==NULL)

head=NULL;
}

else{

head=temp->next;

temp->next=NULL;

count--;

free(temp);

//--------------------------------------------------------

int delete_from_end()

struct node *temp,*var;

temp=head;

if(head==NULL)

printf("\nno elemts in the list");

return 0;

else{

if(temp->next==NULL )

head=NULL;//temp->next;
}

else{

while(temp->next != NULL)

var=temp;

temp=temp->next;

var->next=NULL;

printf("\ndata deleted from list is %d",temp->data);

free(temp);

count--;

return 0;

//------------------------------------------------------

int display()

trav=head;

if(trav==NULL)

printf("\nList is Empty\n");

return 0;

else
{

printf("\n\nElements in the Single Linked List is %d:\n",count);

while(trav!=NULL)

printf(" -> %d ",trav->data);

trav=trav->next;

printf("\n");

//---------------------------------------------------------------

int main()

int ch=0;

char ch1;

head=NULL;

while(1)

printf("\[Link] linked list");

printf("\[Link] at begning of linked list");

printf("\[Link] at the end of linked list");

printf("\[Link] at the middle where you want");

printf("\[Link] from the front of linked list");

printf("\[Link] from the end of linked list ");


printf("\[Link] of the middle data that you want");

printf("\[Link] the linked list");

printf("\[Link]\n");

printf("\nenter the choice of operation to perform on linked list");

scanf("%d",&ch);

switch(ch)

case 1:

do{

create_list();

display();

printf("do you want to create list ,y / n");

getchar();

scanf("%c",&ch1);

}while(ch1=='y');

break;

case 2:

int value;

printf("\nenter the value to be inserted");

scanf("%d",&value);

insert_at_begning(value);
display();

break;

case 3:

int value;

printf("\nenter value to be inserted");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 4:

insert_at_middle();

display();

break;

case 5:

delete_from_front();

display();

}break;

case 6:

{
delete_from_end();

display();

break;

case 7:

display();

delete_from_middle();

display();

break;

case 8:

display();

break;

case 9:

exit(1);

default:printf("\n****Please enter correct choice****\n");

getch();

}
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int count=0;

struct node

int data;

struct node *next;

}*head,*newn,*trav;

//----------------------------------------------------------

void create_list()

int value;

struct node *temp;

temp=head;

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

printf("\nenter the value to be inserted");

scanf("%d",&value);

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

}
else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;

count++;

//----------------------------------------------------

void insert_at_begning(int value)

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

newn->next=head;
head=newn;

count++;

//----------------------------------------------------------

void insert_at_end(int value)

struct node *temp;

temp=head;

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

newn->data=value;

if(head==NULL)

head=newn;

head->next=NULL;

count++;

else

while(temp->next!=NULL)

temp=temp->next;

temp->next=newn;

newn->next=NULL;
count++;

//------------------------------------------------------

int insert_at_middle()

if(count>=2)

struct node *var1,*temp;

int loc,value;

printf("\n after which value you want to insert : ");

scanf("%d",&loc);

printf("\nenter the value to be inserted");

scanf("%d",&value);

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

newn->data=value;

temp=head;

/* if(head==NULL)

head=newn;

head->next=NULL;

count++;

return 0;

}
else

{*/

while(temp->data!=loc)

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",loc);

return 0;

//var1=temp->next;

newn->next=temp->next;//var1;

temp->next=newn;

count++;

//}

else

printf("\nthe no of nodes must be >=2");

//----------------------------------------------------------------

int delete_from_middle()
{

if(count==0)

printf("\n List is Empty!!!! you can't delete elements\n");

else if(count>2)

struct node *temp,*var;

int value;

temp=head;

printf("\nenter the data that you want to delete from the list shown above");

scanf("%d",&value);

while(temp->data!=value)

var=temp;

temp=temp->next;

if(temp==NULL)

printf("\nSORRY...there is no %d element",value);

return 0;

if(temp==head)

head=temp->next;

}
else{

var->next=temp->next;

temp->next=NULL;

count--;

if(temp==NULL)

printf("Element is not avilable in the list \n**enter only middle elements..**");

else

printf("\ndata deleted from list is %d",value);

free(temp);

else

printf("\nthere no middle elemts..only %d elemts is avilable\n",count);

//------------------------------------------------------------------

int delete_from_front()

struct node *temp;

temp=head;

if(head==NULL)

printf("\nno elements for deletion in the list\n");


return 0;

else

printf("\ndeleted element is :%d",head->data);

if(temp->next==NULL)

head=NULL;

else{

head=temp->next;

temp->next=NULL;

count--;

free(temp);

//--------------------------------------------------------

int delete_from_end()

struct node *temp,*var;

temp=head;

if(head==NULL)
{

printf("\nno elemts in the list");

return 0;

else{

if(temp->next==NULL )

head=NULL;//temp->next;

else{

while(temp->next != NULL)

var=temp;

temp=temp->next;

var->next=NULL;

printf("\ndata deleted from list is %d",temp->data);

free(temp);

count--;

return 0;

//------------------------------------------------------

int display()
{

trav=head;

if(trav==NULL)

printf("\nList is Empty\n");

return 0;

else

printf("\n\nElements in the Single Linked List is %d:\n",count);

while(trav!=NULL)

printf(" -> %d ",trav->data);

trav=trav->next;

printf("\n");

//---------------------------------------------------------------

int main()

int ch=0;

char ch1;

head=NULL;
while(1)

printf("\[Link] linked list");

printf("\[Link] at begning of linked list");

printf("\[Link] at the end of linked list");

printf("\[Link] at the middle where you want");

printf("\[Link] from the front of linked list");

printf("\[Link] from the end of linked list ");

printf("\[Link] of the middle data that you want");

printf("\[Link] the linked list");

printf("\[Link]\n");

printf("\nenter the choice of operation to perform on linked list");

scanf("%d",&ch);

switch(ch)

case 1:

do{

create_list();

display();

printf("do you want to create list ,y / n");

getchar();

scanf("%c",&ch1);

}while(ch1=='y');
break;

case 2:

int value;

printf("\nenter the value to be inserted");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 3:

int value;

printf("\nenter value to be inserted");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 4:

insert_at_middle();

display();

break;
}

case 5:

delete_from_front();

display();

}break;

case 6:

delete_from_end();

display();

break;

case 7:

display();

delete_from_middle();

display();

break;

case 8:

display();

break;

case 9:
{

exit(1);

default:printf("\n****Please enter correct choice****\n");

getch();

You might also like