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

Singly Linked List

This document describes the implementation of a singly linked list in C. It includes algorithms for inserting a new node, deleting a node, and traversing the list. The C program defines functions for insertion, deletion, finding a node, finding the previous node, and displaying the list. It allows the user to choose between inserting a new element, deleting an element, or displaying the list. The output shows sample runs of inserting multiple elements at different positions and deleting a node from the list.

Uploaded by

vinolinev
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Singly Linked List

This document describes the implementation of a singly linked list in C. It includes algorithms for inserting a new node, deleting a node, and traversing the list. The C program defines functions for insertion, deletion, finding a node, finding the previous node, and displaying the list. It allows the user to choose between inserting a new element, deleting an element, or displaying the list. The output shows sample runs of inserting multiple elements at different positions and deleting a node from the list.

Uploaded by

vinolinev
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

IMPLEMENTATION OF SINGLY LINKED LIST

Ex. No: 01 Aim To write a program in C to implement pointer implementation of LIST ADT. Algorithm 1. Inserting a New node into SLL: 1.1 Get a new node from the memory. 1.2 Traverse the Linked List, if necessary. 1.3 Identify & locate the previous node 1.4 Insert the new node by rearranging the pointer concerned. 2. Deleting a node from SLL: 1.1 Traverse the Linked List, if necessary. 1.2 Identify & locate the previous node & target node. 1.3 Rearrange t he pointers and detach a target node from the list. 1.4 Return the target node back to the memory. 3. Traversing a SLL: 1.1 Duplicate the HEAD node 1.2 Access the nodes in SLL through NEXT pointers.

Program //Ex. No: 1a. Implementation of Singly Linked List in C. #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct node *find(int); struct node *findprevious(int); struct node { int element; struct node *next; } *List=NULL,*p; void insert(int X); void deletion(int X); void display(); void main() { int data, ch; clrscr(); printf("1.Insert\n2.Deletion\n3.Display\n4.Exit\n"); do { printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the element to insert: "); scanf("%d",&data); insert(data); break; case 2: printf("Enter the element to delete"); scanf("%d",&data); deletion(data); break; case 3: display(); break; case 4: exit(0); }

}while(ch<4); getch(); } void insert(int X) { struct node *newnode; int pos; newnode=malloc(sizeof(struct node)); newnode->element=X; if(List->next==NULL) { List->next=newnode; newnode->next=NULL; } else { printf("\nEnter the value after which the element to be inserted: "); scanf("%d",&pos); p=find(pos); newnode->next=p->next; p->next=newnode; } } struct node *find(int s) { p=List->next; while(p!=NULL && p->element!=s) p=p->next; return p; } void deletion (int X) { struct node *temp; temp=malloc(sizeof(struct node)); p=findprevious(X); if(p->next!=NULL) { temp=p->next; p->next=temp->next; printf("\nThe deleted element is %d",temp->element); free(temp); } else printf("Element not found in list"); }

struct node *findprevious(int s) { p=List; while(p->next!=NULL && p->next->element!=s) p=p->next; return p; } void display() { if(List->next==NULL) printf("List is empty"); else { p=List->next; printf("The contents of list are: \n"); while(p!=NULL) { printf("%d-->",p->element); p=p->next; } printf("NULL"); } }

Output 1.Insert 2.Deletion 3.Display 4.Exit Enter your choice: 1 Enter the element to insert: 10 Enter your choice: 1 Enter the element to insert: 20 Enter the value after which the element to be inserted: 10 Enter your choice: 1 Enter the element to insert: 15 Enter the value after which the element to be inserted: 20 Enter your choice: 3 The contents of list are: 10-->20-->15-->NULL Enter your choice: 2 Enter the element to delete15 The deleted element is 15 Enter your choice: 3 The contents of list are: 10-->20-->NULL Enter your choice:

RESULT Thus the operations such as insertion,deletion and display are implemented for singly linked list This concept is used in implementing polynomial operations.

You might also like