Link-List

Link-List  Singly

#include<stdio.h>
#include<malloc.h>
#include<process.h>

void create();
void display();
void insert_front();
void insert_last();
void insert_after();
void del_front();
void del_last();
void del_any();
void count();
void search();

struct linklist
{
int data;
struct linklist *next;
}*head;
typedef struct linklist node;
void main()
{
head=NULL;
int choice;
while(1)
{
printf(“1.Create\n2.Front\n3.Last\n4.Add After\n5.Del_First\n6.Del_Last\n7.Del_Any\n8.Count\n9.Search\n10.Display\n11.Exit\n”);
printf(“Enter your Choice:”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert_front();
break;
case 3:
insert_last();
break;
case 4:
insert_after();
break;
case 5:
del_front();
break;
case 6:
del_last();
break;
case 7:
del_any();
break;
case 8:
count();
break;
case 9:
search();
break;
case 10:
display();
break;
case 11:
abort();
default:
printf(“Invalid Selection\n”);
}
}
}

void create()
{
int n;
printf(“Enter no. of nodes to be created:”);
scanf(“%d”,&n);
for(int i=0;i<n;i++)
{
node *newnode,*temp;
newnode=(node *)malloc(sizeof(node));
newnode->next=NULL;
printf(“Enter the value %d:”,i+1);
scanf(“%d”,&newnode->data);
if(head==NULL)
head=newnode;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
}
}

void display()
{
if(head==NULL)
printf(“List is Empty\n”);
else
{
printf(“\n_________\n”);
node *temp;
temp=head;
while(temp!=NULL)
{
printf(” %d\n”,temp->data);
temp=temp->next;
}
printf(“_________\n\n”);
}
}

void insert_front()
{
node *newnode;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the value to be inserted:”);
scanf(“%d”,&newnode->data);
newnode->next=head;
head=newnode;
}

void insert_last()
{
node *temp,*newnode;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the value to be inserted:”);
scanf(“%d”,&newnode->data);
newnode->next=NULL;
temp=head;
while(temp->next != NULL)
temp=temp->next;
temp->next=newnode;
}

void insert_after()
{
node *temp,*newnode;
int pos;
newnode=(node *)malloc(sizeof(node));
printf(“Enter the position:”);
scanf(“%d”,&pos);
temp=head;
for(int i=0;i<pos-1;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf(“There are less than %d elements in the list\n”,pos);
return;
}
}
printf(“Enter the value to be inserted:”);
scanf(“%d”,&newnode->data);
newnode->next=temp->next;
temp->next=newnode;
}

void del_front()
{
node *temp;
if(head==NULL)
{
printf(“The list is empty\n”);
return;
}
temp=head;
head=head->next;
free(temp);
}

void del_last()
{
node *temp,*q=NULL;
temp=head;
if(head==NULL)
{
printf(“The list is empty\n”);
return;
}
while(temp->next!= NULL)
{
q=temp;
temp=temp->next;
}
q->next=NULL;
free(temp);
}

void del_any()
{
if(head==NULL)
{
printf(“The list is empty\n”);
return;
}

else
{
node *current,*previous;
previous=head;
current=head->next;
int key,flag=0;
printf(“Enter the node value to be deleted:”);
scanf(“%d”,&key);
if(previous->data==key)
{
del_front();
return;
}
while(current != NULL)
{
if(current->data==key)
{
flag=1;
break;
}
previous=current;
current=current->next;

}
if(flag==1)
{
previous->next=current->next;
free(current);
}
else
printf(“The key is not found in the list\n”);
}
}

void count()
{
node *temp;
temp=head;
if(head==NULL)
{
printf(“List is Empty\n”);
return;
}
int cnt=0;
while(temp != NULL)
{
cnt++;
temp=temp->next;
}
printf(“\nNo. of nodes in the list are %d\n”,cnt);
}

void search()
{
if(head==NULL)
{
printf(“List is Empty\n”);
return;
}
node *temp;
temp=head;
int key,location=1;
printf(“Enter the value that have to search:”);
scanf(“%d”,&key);
while(temp != NULL)
{
if(temp->data==key)
{
printf(“Key is on the location %d\n”,location);
return;
}
temp=temp->next;
location++;
}
if(temp==NULL)
printf(“Key is not found\n”);
}

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.