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

Linked List Operations in C Code

The document contains C code for a linked list implementation, including functions for inserting, deleting, and displaying nodes. It allows users to perform operations at the beginning, end, or specific positions in the list. The main function provides a menu-driven interface for users to interact with the linked list.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Linked List Operations in C Code

The document contains C code for a linked list implementation, including functions for inserting, deleting, and displaying nodes. It allows users to perform operations at the beginning, end, or specific positions in the list. The main function provides a menu-driven interface for users to interact with the linked list.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include<stdio.

h>

#include<stdlib.h>

struct node

int num;

struct node *next;

}; typedef struct node node;

node *start;

void insertatbegin(int num)

node *p;

p=(node*)malloc(sizeof(node));

p->num=num;

if(start==NULL)

p->next=NULL;

else

{ p->next=start;}

start=p;

void insertatend(int item)

node *p,*loc;

p=(node*)malloc(sizeof(node));

p->num=item;

p->next=NULL;

if(start==NULL)

start=p;

else
{

loc=start;

while(loc->next!=NULL)

loc=loc->next;

loc->next=p;

void insertionatspepos(int item)

int pos;

printf("enter the position to insert node :: ");

scanf("%d",&pos);

node *p,*loc;

p=(node*)malloc(sizeof(node));

p->num=item;

int i=2;

loc=start;

while(i!=pos)

i++;

loc=loc->next;

if(loc->next==NULL&& i!=pos)

break;

if(i==pos){p->next=loc->next;
loc->next=p;}

else { printf("linked list is smaller than expected and its size is %d ",i-1);}

void display(node *start)

node *ptr;

//ptr=(node*)malloc(sizeof(node));

ptr=start;

int i=1;

while(ptr!=NULL)

printf("value at node %d is :: %d\n", i++, ptr->num);

ptr=ptr->next;

void deletefrombegin()

node *p;

p=start;

start=start->next;

printf("deleted element is %d ",p->num);

p->next=NULL;

free(p);

void deletefromend()

node *p,*q;

// p=(node*)malloc(sizeof(node));
// q=(node*)malloc(sizeof(node));

p=start;

if(start==NULL)

printf("linked list is empty: ");

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

start=NULL;

printf("Element deleted id %d ",p->num);

free(p);

else

{ q=start;

p=p->next;

while(p->next!=NULL)

q=p;

p=p->next;

q->next=NULL;

printf("deleted element is %d ", p->num);

free(p);

void deletefromspecpos()

int pos;

printf("enter the location of node to delete: ");

scanf("%d",&pos);

node *p,*q;

int i=1;
if(start==NULL)

{printf("linked list is empty: ");}

else {

p=start;

while(i++!=pos)

q=p;

p=p->next;

if(p->next==NULL&& i!=pos)

{break;}

if(i-1==pos)

q->next=p->next;

printf("deleted element is %d ", p->num);

free(p);

else {

printf("length of linked list is %d and is shorter ",i);

int main()

int num,choice,choice1;

while (1)

printf("\nenter the operation you want to perform\n");

printf("1: insertion: \n2: deletion: \n3: display: \n4: exit: \n");


scanf("%d",&choice1);

switch (choice1)

case 1:

printf("\nenter the number to store in node : \n");

scanf("%d",&num);

printf("enter the choice to select the operation \n ");

printf("1: insert at begin: \n 2: insert at specific position: \n 3: insert at end: \n");

scanf("%d",&choice);

switch (choice)

case 1:

insertatbegin(num);

break;

case 2:

insertionatspepos(num);

break;

case 3:

insertatend(num);

break;

default:

printf("wrong choice !");

break;

break;

case 2:

{
printf("enter the choice to select the operation \n");

printf("1: delete first node: \n2: delete a specific node: \n3: delete last node: \n");

scanf("%d",&choice);

switch (choice)

case 1:

deletefrombegin();

break;

case 2:

deletefromspecpos();

break;

case 3:

deletefromend();

break;

default:

printf("wrong choice !");

break;

break;}

case 3:

display(start);

break;

case 4:

return 0;

default:
printf("wrong choice !");

break;

return 0;

You might also like