0% found this document useful (0 votes)
12 views

Linked List Try

Uploaded by

mousumi banerjee
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)
12 views

Linked List Try

Uploaded by

mousumi banerjee
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/ 6

#include<stdio.

h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

};

struct node *head = NULL;

struct node * getnode()

struct node *temp;

int val;

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

if(temp != NULL)

printf("Enter the value to insert\n");

scanf("%d",&val);

temp->data = val;

temp->next = NULL;

return temp;

struct node *insert_first()

struct node *temp;

temp = getnode();

if(head == NULL)

head = temp;
else

temp->next = head;

head = temp;

return head;

struct node *insert_last()

struct node *temp,*ptr;

ptr = head;

temp = getnode();

if(head == NULL)

head = temp;

else

while(ptr->next != NULL)

ptr = ptr->next;

ptr->next = temp;

return head;

struct node *delete_first()

struct node *temp;

if(head == NULL)

{
printf("The list is empty\n");

return head;

else if(head->next == NULL)

temp = head;

head = NULL;

free(temp);

else

head = head->next;

return head;

struct node *delete_last()

struct node *temp,*ptr;

if(head == NULL)

printf("The list is empty\n");

return head;

else if(head->next == NULL)

temp = head;

head = NULL;

free(temp);

}
else

temp = head;

while(temp->next != NULL)

ptr = temp;

temp = temp->next;

ptr->next = NULL;

free(temp);

return head;

void display()

struct node *temp;

printf("\n Display\n");

temp = head;

while(temp != NULL)

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

temp = temp->next;

printf("\n");

void search()

struct node *temp = head;


int item,f=0,c=0;

printf("Enter the number to be seacched\n");

scanf("%d",&item);

while (temp != NULL)

if(temp->data == item)

f = 1;

break;

temp = temp->next;

c++;

if ( f == 1)

printf("Found in position %d\n",c+1);

else

printf("Not Found\n");

int main()

struct node *temp;

int ch,pos;

do {

printf("1. Insert First\n2. Insert Last\n3. Display\n4. Exit\n");

printf("5. Delete First\n6. Delete Last\n7. Search\n");

scanf("%d",&ch);

switch(ch)

case 1:
head = insert_first();

break;

case 2:

head = insert_last();

break;

case 3:

display();

break;

case 4:

return 0;

case 5:

head = delete_first();

break;

case 6:

head = delete_last();

break;

case 7:

search();

//printf("Item found in %d position\n");

break;

default:

printf("Wrong Choice\n");

}while(ch != 4);

return 0;

You might also like