0% found this document useful (0 votes)
12 views12 pages

Singly Linked List

This document describes the implementation of a singly linked list in C including functions to insert nodes at the beginning, end, or a specific position of the list. It also includes functions to delete nodes from the beginning, end, or after a specific location. Additional functions allow searching for a node by value and displaying all nodes. The main function implements a menu driven program to test the linked list functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

Singly Linked List

This document describes the implementation of a singly linked list in C including functions to insert nodes at the beginning, end, or a specific position of the list. It also includes functions to delete nodes from the beginning, end, or after a specific location. Additional functions allow searching for a node by value and displaying all nodes. The main function implements a menu driven program to test the linked list functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Singly Linked list

Singly Linked list


• #include<stdio.h>
• #include<stdlib.h>
• struct node
• {
• int data;
• struct node *next;
• };
• struct node *head;

• void insertatfirst ();


• void insertatlast ();
• void insertatsp();
• void deletefirst();
• void deletelast();
• void deletesp();
• void display();
• void search();
• void main ()
• {
• int choice =0;
• while(choice != 9)
• {
• printf("\n\n*********Main Menu*********\n");
• printf("\nChoose one option from the following list ...\n");
• printf("\n===============================================\n");
• printf("\n1.Insert at start\n2.Insert at last\n3.Insert at any specific Position\n4.Delete from
starting\n 5.Delete from last\n6.Delete node after specified location\n7.Search\n8.Display\n9.Exit\
n");
• printf("\nEnter your choice?\n");
• scanf("\n%d",&choice);
• switch(choice) • case 6: deletesp();
• { case 1: insertatstrat(); • break;
• break; • case 7: search();
• case 2: insertatlast(); • break;
• break; • case 8: display();
• case 3: insertatsp(); • break;
• break; • case 9: exit(0);
• case 4: deletefirst(); • break;
• break; • default: printf("Please enter valid
• case 5: deletelast(); choice..");

• Break;
• } }}
• void insertatfirst()
• {
• struct node *ptr;
• int value;
• ptr = (struct node *) malloc(sizeof(struct node *));
• if(ptr == NULL) {
• printf(“\n Insert not allowed"); }
• else {
• printf("\nEnter value\n");
• scanf("%d",&value);
• ptr->data = value;
• ptr->next = head;
• head = ptr;
• printf("\nNode inserted");
• } }
• void insertatlast() { • ptr -> next = NULL;
• struct node *ptr,*temp; • head = ptr;
• int value;
• printf("\nNode inserted"); }
• ptr = (struct node*)malloc(sizeof(struct node));
• else {
• if(ptr == NULL) {
• printf(“\nInsert not allowed");
• temp = head;

• } • while (temp -> next != NULL) {


• else { • temp = temp -> next; }
• printf("\nEnter value\n"); • temp->next = ptr;
• scanf("%d",&value);
• ptr->next = NULL;
• ptr->data = item;
• printf("\nNode inserted");
• if(head == NULL) {
• } } }
• void insertatsp()
• { • scanf("\n%d",&loc);
• int i,loc,value; • temp=head;
• struct node *ptr, *temp;
• for(i=0;i<loc;i++) {
• ptr = (struct node *) malloc (sizeof(struct node));
• temp = temp->next;
• if(ptr == NULL) {
• printf(“\nInsert not allowed"); } • if(temp == NULL) {
• else { • printf("\ncan't insert\n");
• printf("\nEnter element value");
• return; } }
• scanf("%d",&value);
• ptr->data = item;
• ptr ->next = temp ->next;
• printf("\nEnter the node number after which • temp ->next = ptr;
• you want to insert ");
• printf("\nNode inserted");

• } }
• void deletefirst()
• {
• struct node *ptr;
• if(head == NULL)
• {
• printf("\nList is empty\n");
• }
• else
• {
• ptr = head;
• head = ptr->next;
• free(ptr);
• printf("\nNode deleted from the begining ...\n");
• }
• }
• void last_delete() • else {
• {
• ptr = head;
• struct node *ptr,*ptr1;
• while(ptr->next != NULL)
• if(head == NULL) {
• printf("\nlist is empty");
• {

• } • ptr1 = ptr;
• else if(head -> next == NULL) { • ptr = ptr ->next;
• head = NULL; • }
• free(head);
• ptr1->next = NULL;
• printf("\nOnly node of the list deleted ...\n");
• free(ptr);
• }
• • printf("\nDeleted Node from the last ...\n");
• } }
• void deletesp()
• {
• struct node *ptr,*ptr1;
• int loc,i;
• printf("\n Enter the location of the node after which you want to perform deletion \n");
• scanf("%d",&loc);
• ptr=head;
• for(i=0;i<loc;i++) {
• ptr1 = ptr;
• ptr = ptr->next;
• if(ptr == NULL)
• {
• printf("\nCan't delete");
• return;
• } }
• ptr1 ->next = ptr ->next;
• free(ptr);
• printf("\nDeleted node %d ",loc+1);
• }
• void search()
• {
• struct node *ptr;
• int item,i=0,flag;
• ptr = head;
• if(ptr == NULL) {
• printf("\nEmpty List\n");
• }
• else {
• printf("\nEnter item which you want to search?\n");
• scanf("%d",&item);
• while (ptr!=NULL)
• { if(ptr->data == item)
• {
• printf("item found at location %d ",i+1);
• flag=0; }
• else {
• flag=1; }
• i++;
• ptr = ptr -> next;
• }
• if(flag==1) {
• printf("Item not found\n");
• } } }
• void display()
• {
• struct node *ptr;
• ptr = head;
• if(ptr == NULL)
• {
• printf("Nothing to print");
• }
• else
• {
• printf("\nprinting values . . . . .\n");
• while (ptr!=NULL)
• {
• printf("\n%d",ptr->data);
• ptr = ptr -> next;
• } }}

You might also like