Source Code in C:Single Linked List Operations
// SLL Operations
#include<stdio.h>
#include<stdlib.h>
// Definition of the node structure
struct Node {
int data;
struct Node* next;
}*head=NULL,*last=NULL;
// Function to create a new node
void createNode()
{
struct Node *p;
int i,n;
head=last=NULL;
printf("Enter no. of node data, you want to create :");
scanf("%d",&n);
printf("Enter the %d Node(s) data:",n);
for(i=0;i<n;i++)
{
if(head==NULL)
p=head=(struct Node*)malloc(sizeof(struct Node));
else
{
p->next=(struct Node*)malloc(sizeof(struct Node));
p=p->next;
}
p->next=NULL;
scanf("%d",&(p->data));
}
last=p;
//return(head);
}
// Function to insert a node at the beginning of the list
void insertAtBeginning(int val) {
struct Node *p;
if(head==NULL)
printf("Create the List first,after that u perform Insert Operation\n");
else
{
p=(struct Node*)malloc(sizeof(struct Node));
p->data=val;
p->next=head;
head=p;
printf("Node inserted at the beginning\n");
}
}
// Function to insert a node at the end of the list
void insertAtEnd(int val)
{
struct Node* p;
if(head==NULL)
printf("Create the List first,after that u perform Insert Operation\n");
else
{
p=(struct Node*)malloc(sizeof(struct Node));
p->data=val;
p->next=NULL;
last->next=p;
last=p;
printf("Node inserted at the end\n");
}
}
// Function to insert a node after a given node
void insertInBetween(int val)
{
struct Node *p,*preptr,*postptr;
int n=1,pos;
postptr=head;
if(head==NULL)
printf("Create the List first,after that u perform Insert Operation\n");
else
{
printf("Enter position of the newnode has to be inserted:");
scanf("%d",&pos);
if(pos==1)
insertAtBeginning(val);
else
{
for(n=1;n!=pos;n++)
{
preptr=postptr;
postptr=postptr->next;
}
p=(struct Node*)malloc(sizeof(struct Node));
p->data=val;
p->next=postptr;
preptr->next=p;
printf("Node inserted in between\n");
}
}
}
// Function to delete a node from the beginning of the list
void deleteAtBeginning() {
struct Node *p=head;
if (head == NULL) {
printf("List is empty\n");
return;
}
else
{
printf("Node data %d deleted from the beginning\n",p->data);
head=head->next;
free(p);
}
}
void deleteAtEnd()
{
struct Node *preptr=NULL,*postptr=head;
if(head==NULL)
{
printf("List is empty. Nothing to delete.\n");
return;
}
// If there's only one node in the list
if(postptr->next==NULL)
{
printf("Node data %d deleted at the end\n",postptr->data);
free(postptr);
head=last=NULL;
return;
}
// Traverse to the end of the list
while(postptr->next!=NULL)
{
preptr=postptr;
postptr=postptr->next;
}
// Update the last node
preptr->next=NULL;
last=preptr;
printf("Node data %d is deleted at the end\n",postptr->data);
free(postptr);
}
void deleteInBetween()
{
struct Node *preptr,*postptr;
int n=1,pos;
if(head==NULL)
{
printf("List is empty\n");
return;
}
else
{
postptr=head;
printf("Enter the position of the node has to be deleted:");
scanf("%d",&pos);
if(pos==1)
deleteAtBeginning();
else
{
while(n<pos && postptr!=NULL) // while(n!=pos)
{
preptr=postptr;
postptr=postptr->next;
n++;
}
printf("Node data %d is deleted from the list\n",postptr->data);
preptr->next = postptr->next;
free(postptr);
}
}
}
void searchNode(int data)
{
struct Node* temp = head;
int loc=1;
if(head==NULL)
{
printf("List is empty, Search is not performed\n");
return;
}
else
{
while(temp!=NULL && temp->data!=data)
{
temp=temp->next;
loc++;
}
if(temp==NULL)
printf("The given data %d not found in the List",data);
else
printf("The given data %d found in the List at location %d",data,loc);
}
}
// Function to reverse the linked list
void reverseList()
{
struct Node *prev, *current, *next;
prev = NULL;
current = head;
last = head; // last should now point to the current head as it will become the last node
while(current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
printf("List reversed\n");
}
// Function to sort the list using Bubble Sort to sort the singly linked list in ascending order
void sortList() {
struct Node *current, *next;
int temp;
if (head == NULL) {
printf("List is empty. Nothing to sort.\n");
return;
}
int swapped;
do {
swapped = 0;
current = head;
while (current->next != NULL) {
next = current->next;
if (current->data > next->data) {
// Swap the data
temp = current->data;
current->data = next->data;
next->data = temp;
swapped = 1;
}
current = current->next;
}
} while (swapped);
printf("List sorted\n");
}
// Function to print the linked list
void printList()
{
struct Node* p;
if(head==NULL)
printf("Underflow....Empty Linked List\n");
else
{
for(p=head;p!= NULL;p=p->next)
{
printf("%d -> ",p->data);
}
printf("NULL\n");
}
}
// Main function to perform operations
int main()
{
int choice,subChoice,data;
while(1)
{
printf("********SLL Operations*******\n");
printf("1. Create Node(s)\n");
printf("2. Insert Node\n");
printf("3. Delete Node\n");
printf("4. Search Node\n");
printf("5. Reverse List\n");
printf("6. Print List\n");
printf("7. Sort List\n");
printf("8. Quit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch (choice)
{
case 1:
createNode();
printf("Node(s) created\n");
break;
case 2:
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert in Between\n");
printf("Enter your choice: ");
scanf("%d",&subChoice);
printf("Enter data to insert: ");
scanf("%d", &data);
switch (subChoice)
{
case 1:
insertAtBeginning(data);
break;
case 2:
insertAtEnd(data);
break;
case 3:
insertInBetween(data);
break;
default:
printf("Invalid choice. Please try again.\n");
}
break;
case 3:
printf("1. Delete at Beginning\n");
printf("2. Delete at End\n");
printf("3. Delete in Between\n");
printf("Enter your choice: ");
scanf("%d", &subChoice);
switch (subChoice)
{
case 1:
deleteAtBeginning();
break;
case 2:
deleteAtEnd();
break;
case 3:
deleteInBetween();
break;
default:
printf("Invalid choice. Please try again.\n");
}
break;
case 4:
printf("Enter data to search: ");
scanf("%d",&data);
searchNode(data);
break;
case 5:
reverseList();
break;
case 6:
printList();
break;
case 7:
sortList();
break;
case 8:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
INPUT/OUTPUT:
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 5
List is empty.
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 6
Underflow....Empty Linked List
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice:
7
List is empty. Nothing to sort.
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 4
Enter data to search: 6
List is empty, Search is not performed
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 3
1. Delete at Beginning
2. Delete at End
3. Delete in Between
Enter your choice: 1
List is empty
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 2
1. Insert at Beginning
2. Insert at End
3. Insert in Between
Enter your choice: 3
Enter data to insert: 4
Create the List first,after that u perform Insert Operation
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 1
Enter no. of node data, you want to create :6
Enter the 6 Node(s) data:10 20 30 40 50 60
Node(s) created
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 6
10 -> 20 -> 30 -> 40 -> 50 -> 60 -> NULL
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 2
1. Insert at Beginning
2. Insert at End
3. Insert in Between
Enter your choice: 1
Enter data to insert: -5
Node inserted at the beginning
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 6
-5 -> 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> NULL
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 2
1. Insert at Beginning
2. Insert at End
3. Insert in Between
Enter your choice: 2
Enter data to insert: 70
Node inserted at the end
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 6
-5 -> 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70 -> NULL
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 2
1. Insert at Beginning
2. Insert at End
3. Insert in Between
Enter your choice: 3
Enter data to insert: 45
Enter position of the newnode has to be inserted:6
Node inserted in between
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 6
-5 -> 10 -> 20 -> 30 -> 40 -> 45 -> 50 -> 60 -> 70 -> NULL
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 4
Enter data to search: 55
The given data 55 not found in the List
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 4
Enter data to search: 60
The given data 60 found in the List at location 8
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 5
List reversed
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 6
70 -> 60 -> 50 -> 45 -> 40 -> 30 -> 20 -> 10 -> -5 -> NULL
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice:
7
List sorted
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 6
-5 -> 10 -> 20 -> 30 -> 40 -> 45 -> 50 -> 60 -> 70 -> NULL
********SLL Operations*******
1. Create Node(s)
2. Insert Node
3. Delete Node
4. Search Node
5. Reverse List
6. Print List
7. Sort List
8. Quit
Enter your choice: 8
Exiting...
--------------------------------
Process exited after 274.5 seconds with return value 0
Press any key to continue . . .
No comments:
Post a Comment