Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
49 views8 pages

Linked List Full Program

This document contains code for a C program that implements functions to perform operations on a linked list such as insertion, deletion, traversal and searching. The functions include insert, delete and search nodes at different positions in the linked list.

Uploaded by

batturekha513
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)
49 views8 pages

Linked List Full Program

This document contains code for a C program that implements functions to perform operations on a linked list such as insertion, deletion, traversal and searching. The functions include insert, delete and search nodes at different positions in the linked list.

Uploaded by

batturekha513
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/ 8

#include<stdio.

h>

#include<stdlib.h>

struct node

int number;

struct node *next;

};

struct node *head=NULL,*last=NULL;

void delete_at_last();

void delete_at_position();

void delete_at_begin();

void insert_at_begin();

void create_linked_list();

void display_linked_list();

void insert_at_last(int value);

void insert_at_position(int value,int position);

void traverse_linked_list();

void search();

int main()

int n,value,pos;

printf("Enter number of nodes:");

scanf("%d",&n);

printf("\ncreate linked list:");

while(n--)
{

create_linked_list();

display_linked_list();

int c;

printf("\nEnter a number to choose operation\n1_insert_at_begin\n2_insert_at_position\


n3_insert_at last\n4_delete_last\n5_delet_at_begin\n6_delete_at_position\n7_search\n8_exit");

scanf("%d",&c);

while(1)

switch(c)

case 1: insert_at_begin();

display_linked_list();

break;

case 2: printf("Enter position and value");

scanf("%d%d",&pos,&value);

insert_at_position(value,pos);

display_linked_list();

break;

case 3: printf("\nEnter value of new node\n");

scanf("%d",&value);

insert_at_last(value);

display_linked_list();

break;

case 4: delete_at_last();
display_linked_list();

break;

case 5: delete_at_begin();

display_linked_list();

break;

case 6: delete_at_position();

//display_linked_list();

traverse_linked_list();

break;

case 7: search();

display_linked_list();

break;

case 8 : exit(1);

default: printf("Enter num only b/w 1-7");

break;

return 0;

void create_linked_list()

int val;

printf("enter a number:\n");

scanf("%d",&val);

insert_at_last(val);
}

void insert_at_last(int value)

struct node *temp_node;

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

temp_node->number=value;

temp_node->next=NULL;

if(head==NULL)

head=temp_node;

last=temp_node;

else

last->next=temp_node;

last=temp_node;

void display_linked_list()

printf("\n Your linked list is:\n");

struct node*mylist;

mylist=head;

while(mylist!=NULL)

{
printf("%d\t",mylist->number);

mylist=mylist->next;

void insert_at_position(int value,int position)

struct node *ptr1,*ptr2;

ptr1=head;

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

ptr2->number=value;

ptr2->next=NULL;

position--;

while(position!=1)

ptr1=ptr1->next;

position--;

ptr2->next=ptr1->next;

ptr1->next=ptr2;

void insert_at_begin()

struct node *temp;

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

printf("Enter a number\n");
scanf("%d",&temp->number);

temp->next=NULL;

temp->next=head;

head=temp;

void delete_at_begin()

struct node*temp;

temp=head;

head=head->next;

free(temp);

void delete_at_last()

struct node *temp1;

temp1=head;

while(temp1->next!=last)

temp1=temp1->next;

temp1->next=NULL;

last=temp1;

void delete_at_position()

{
struct node *prev,*curr;

int i=1,pos;

prev=head;

printf("\nenter position (2 to n-1)");

scanf("%d",&pos);

while(i<pos-1)

prev=prev->next;

i++;

curr=prev->next;

prev->next=curr->next;

void search()

struct node *ptr;

int key,flag=0;

printf("\nenter a key to search\n");

scanf("%d",&key);

ptr=head;

while(ptr!=NULL)

if(key==ptr->number)

flag++;
break;

ptr=ptr->next;

if(flag==1)

printf("\n Key element found");

void traverse_linked_list()

printf("\n your linked is\n");

struct node*mylist;

mylist=head;

while(mylist!=NULL)

printf("%d\t",mylist->number);

mylist=mylist->next;

You might also like