0% found this document useful (0 votes)
4 views6 pages

Singly Linked List Operations in C

The document contains C source code for managing a singly linked list with functions for adding, inserting, deleting, counting, traversing, and searching nodes. It includes a user interface for interacting with the list through various options. The code demonstrates how to dynamically manage memory for nodes and perform operations based on user input.
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)
4 views6 pages

Singly Linked List Operations in C

The document contains C source code for managing a singly linked list with functions for adding, inserting, deleting, counting, traversing, and searching nodes. It includes a user interface for interacting with the list through various options. The code demonstrates how to dynamically manage memory for nodes and perform operations based on user input.
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

7/4/22, 6:35 AM [Link]

sNo=20&qId=5d342901e9524009292de70a&bd=AY3RFZHVEQg%3D%3D&lid…
[Link] At Begin
[Link] At End
[Link] At Position
[Link]

Page No:
[Link] List
[Link]
[Link]
Enter your option : 11

ID: B21ME015
Note: Do use the printf() function with a newline character ( \n ) at the end.

Source Code:

sll.c

#include<stdio.h>
#include<stdlib.h>
struct Node{
int Data;
struct Node* Next;
};
struct Node* Head;
void AddNodes(){
int e;
struct Node* temp1;
printf("Enter elements (to stop enter -1): ");
while(1){
scanf("%d", &e);
if(e == -1)
return;
struct Node* temp2 = (struct Node*)malloc(sizeof(struct Node));
if(Head == NULL){
Head = temp2;

2021-2025-MECH-1
temp1 = temp2;
Head->Data = e;
Head->Next = NULL;
}
else{
temp2->Data = e;
temp2->Next = NULL;
Kakatiya Institute of Technology and Science

temp1->Next = temp2;
temp1 = temp2;
}
}
}
void InsertBegin(int e){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->Data = e;
temp->Next = Head;
Head = temp;
}

void InsertEnd(int e){

[Link] 10/24
7/4/22, 6:35 AM [Link]
struct Node* temp2 = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp = Head;
while(temp->Next != NULL){
temp = temp->Next;

Page No:
}
temp2->Data = e;
temp2->Next = NULL;
temp->Next = temp2;

ID: B21ME015
}

void InsertPosition(int e, int pos){


int l = count();
if(pos > l){
printf("No such position in Singly Linked List. So insertion is not possible\n");
return;
}
if(pos == 1){
InsertBegin(e);
return;
}
struct Node* temp2 = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp = Head;
for(int i=0; i<pos-2; i++){
temp = temp->Next;
}
temp2->Data = e;
temp2->Next = temp->Next;
temp->Next = temp2;
}

void DeleteBegin(){
printf("The deleted element from Singly Linked List: %d\n", Head->Data);
struct Node* temp = Head;
Head = temp->Next;
}

2021-2025-MECH-1
void DeleteEnd(){
struct Node* temp = Head;
struct Node* temp2;
while(temp2->Next != NULL){
temp = temp->Next;
temp2 = temp->Next;
}
Kakatiya Institute of Technology and Science

temp->Next = NULL;
printf("The deleted element from Singly Linked List: %d\n", temp2->Data);
}

void DeletePosition(int pos){


if(pos > count() - 1){
printf("No such position in Singly Linked List. So deletion is not possible\n");
return;
}
struct Node* temp = Head;
if(pos == 1){
printf("The deleted element from Singly Linked List: %d\n", Head->Data);
Head = temp->Next;

[Link] 11/24
7/4/22, 6:35 AM [Link]
return;
}
for(int i=0; i<pos-2; i++){
temp = temp->Next;

Page No:
}
struct Node* temp2;
temp2 = temp->Next;
temp->Next = temp2->Next;

ID: B21ME015
printf("The deleted element from Singly Linked List: %d\n", temp2->Data);
}

int count(){
struct Node* temp = Head;
int count = 1;
while(temp != NULL){
count = count + 1;
temp = temp->Next;
}
return count - 1;
}

void TraverseList(){
struct Node* temp = Head;
if(temp == NULL){
printf("Singly Linked List is empty\n");
return;
}
printf("The elements in Singly Linked List are:");
while(temp->Next != NULL){
printf(" %d -->", temp->Data);
temp = temp->Next;
}
printf(" %d -->", temp->Data);
printf(" NULL\n");
}
void Search(int key){

2021-2025-MECH-1
struct Node* temp = Head;
int At = 0;
while(temp!=NULL){
At++;
if(key == temp->Data){
printf("The given element %d is found at position : %d\n", key, At);
return;
Kakatiya Institute of Technology and Science

}
temp = temp->Next;
}
printf("The given element %d is not found in the given Singly Linked List\n", key);
}
int main(){
Head = NULL;
int option, number, position, key;
while(1){
printf("[Link] Nodes\n");
printf("[Link] At Begin\n");
printf("[Link] At End\n");
printf("[Link] At Position\n");

[Link] 12/24
7/4/22, 6:35 AM [Link]
printf("[Link] At Begin\n");
printf("[Link] At End\n");
printf("[Link] At Position\n");
printf("[Link]\n");

Page No:
printf("[Link] List\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your option : ");

ID: B21ME015
scanf("%d", &option);
switch(option){
case 1:
AddNodes();
break;
case 2:
printf("Enter the number to be inserted at the beginning: ");
scanf("%d", &number);
InsertBegin(number);
break;
case 3:
printf("Enter the number to be inserted at the end: ");
scanf("%d" ,&number);
InsertEnd(number);
break;
case 4:
printf("Enter the position: ");
scanf("%d", &position);
printf("Enter the number to be inserted: ");
scanf("%d", &number);
InsertPosition(number,position);
break;
case 5:
DeleteBegin();
break;
case 6:
DeleteEnd();
break;

2021-2025-MECH-1
case 7:
printf("Enter the position: ");
scanf("%d", &position);
DeletePosition(position);
break;
case 8:
printf("The number of nodes in the Singly Linked List are: %d\n", count());
Kakatiya Institute of Technology and Science

break;
case 9:
TraverseList();
break;
case 10:
printf("Enter the element to be searched: ");
scanf("%d", &key);
Search(key);
break;
case 11:
exit(1);
}
}

[Link] 13/24
7/4/22, 6:35 AM [Link]
return 0;
}

Page No:
ID: B21ME015
Execution Results - All test cases have succeeded!

Test Case - 1

User Output
[Link] Nodes 9
[Link] At Begin 9
[Link] At End 9
[Link] At Position 9
[Link] At Begin 9
[Link] At End 9
[Link] At Position 9
[Link] 9
[Link] List 9

2021-2025-MECH-1
[Link] 9
[Link] 9
Enter your option : 9
Singly Linked List is empty 1
[Link] Nodes 1
[Link] At Begin 1
[Link] At End 1
Kakatiya Institute of Technology and Science

[Link] At Position 1
[Link] At Begin 1
[Link] At End 1
[Link] At Position 1
[Link] 1
[Link] List 1
[Link] 1
[Link] 1
Enter your option : 1
Enter elements (to stop enter -1): 10 20 30 -1
[Link] Nodes 2
[Link] At Begin 2

[Link] 14/24
7/4/22, 6:35 AM [Link]

Test Case - 1
[Link] At End 2
[Link] At Position 2

Page No:
[Link] At Begin 2
[Link] At End 2
[Link] At Position 2
[Link] 2

ID: B21ME015
[Link] List 2
[Link] 2
[Link] 2
Enter your option : 2
Enter the number to be inserted at the beginning: 5
[Link] Nodes 9
[Link] At Begin 9
[Link] At End 9
[Link] At Position 9
[Link] At Begin 9
[Link] At End 9
[Link] At Position 9
[Link] 9
[Link] List 9
[Link] 9
[Link] 9
Enter your option : 9
The elements in Singly Linked List are: 5 --> 10 --> 20 --> 30 --> NULL 3
[Link] Nodes 3
[Link] At Begin 3
[Link] At End 3
[Link] At Position 3
[Link] At Begin 3
[Link] At End 3
[Link] At Position 3
[Link] 3

2021-2025-MECH-1
[Link] List 3
[Link] 3
[Link] 3
Enter your option : 3
Enter the number to be inserted at the end: 35
[Link] Nodes 9
Kakatiya Institute of Technology and Science

[Link] At Begin 9
[Link] At End 9
[Link] At Position 9
[Link] At Begin 9
[Link] At End 9
[Link] At Position 9
[Link] 9
[Link] List 9
[Link] 9
[Link] 9
Enter your option : 9
The elements in Singly Linked List are: 5 --> 10 --> 20 --> 30 --> 35 --> NULL 4

[Link] 15/24

You might also like