DS PRAC

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Name :Mohammed Noman Roll no : 270

Experiment no 7: Write a C program to Implementation of operations on singly linked list


Code:
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};

struct Node *createNode(int value)//to create a new node


{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));//allocate memony
address to new node
newNode->data = value;//set info part
newNode->next = NULL;//set next =null
return newNode;//return the address of new node
}

void insertAtBeginning(struct Node **head, int value)//insert at begining


{
struct Node *newNode = createNode(value);//create an ew node and get its adress
newNode->next = *head;//set the next of new node to head
*head = newNode;//set head pointer to new node
}

void insertAtEnd(struct Node **head, int value)


{
struct Node *newNode = createNode(value);//create a new node get address of new node
if (*head == NULL)//when LL is empty
{
*head = newNode;//set head = new node
return;
}
struct Node *temp = *head;//temporary pointer
while (temp->next != NULL)
{
temp = temp->next;//traverse to last node
}
temp->next = newNode;//set next of lastnode =address of new node
}

void deleteAtBeginning(struct Node **head)//delete from begining


{
if (*head == NULL)//when LL is empty
{
printf("List is empty! Cannot delete.\n");
return;
}
struct Node *temp = *head;//tem pointer
*head = (*head)->next;//set head = next of first node
free(temp);//delete the node}

void deleteAtEnd(struct Node **head)//delete from end


{ if (*head == NULL)//when empty
{
printf("List is empty! Cannot delete.\n");
return;
}
if ((*head)->next == NULL)//when LL having only one element
{
free(*head);//deallocate the memory of node
*head = NULL;//set head = null
return;
}
struct Node *temp = *head;//temp pointer
while (temp->next != NULL)//traverse to last node
{
temp = temp->next;
}
free(temp->next);//deallocate the memoryof last node
temp->next = NULL;//set next== null of last node
}

void insertAtNthPosition(struct Node **head, int value, int n)//to insert at specific position
{
struct Node *newNode = createNode(value);
if (n == 0)//when pos=0 insert at begining
{
insertAtBeginning(head, value);
return;
}
struct Node *temp = *head;//temp pointer
for (int i = 0; i< n - 1 &&temp != NULL; i++)//until position
{
temp = temp->next;//set temp = nextnode
}
if (temp == NULL)//position is out of bound
{
printf("Position is out of bounds. Node not inserted.\n");
free(newNode);//deallocate the newnode
return;
}
//else
newNode->next = temp->next; //set next of new node = next of temp
temp->next = newNode;//set the next of temp = new node
}
void deleteAtNthPosition(struct Node **head, int n)//to delete from specific position
{ if (*head == NULL)//LL is empty
{
printf("List is empty! Cannot delete.\n");
return;
}
struct Node *temp = *head;//temp pointer= head
if (n == 0)//when pos==0 delete from begining
{
*head = temp->next;
free(temp);//deallocate the memory of temp pointing node
return;
}
for (int i = 0; i< n - 1 &&temp != NULL; i++)//traverse to position node
{
temp = temp->next;
}
if (temp == NULL || temp->next == NULL)//position is out of bound
{
printf("Position is out of bounds. Node not deleted.\n");
return;
}
struct Node *nodeToDelete = temp->next;//store the node to be deleted
temp->next = nodeToDelete->next;//link the n-1 node to n+1 node
free(nodeToDelete);//free the node
}
void display(struct Node *head)//to display
{
if (head == NULL)//empty
{
printf("List is empty!\n");
return;
}
struct Node *temp = head;//temp pointer
printf("Linked List: ");
while (temp != NULL)//until last
{
printf("%d -> ", temp->data);//print the node
temp = temp->next;//increment the node pointer
}
printf("NULL\n");//indicates the end of list
}

void search(struct Node *head, int value)


{ if (head == NULL) // empty
{
printf("List is empty!\n");
return;
}
struct Node *current = head; // Start from the head
int index = 0; // Initialize index to 0
while (current != NULL) // Traverse the list
{ if (current->data == value) // Check if the current node's data matches the value
{
printf("Element %d found at posi %d ",value,index);
return; // display index if value is found
}
current = current->next; // Move to the next node
index++; // Increment the index
}
printf("Elment %d not found in LL",value);
}
int main()
{ struct Node *head = NULL;//set head == null
int choice, value, position;
do{
printf("\nSingly Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete at Beginning\n");
printf("4. Delete at End\n");
printf("5. Insert at nth Position\n");
printf("6. Delete at nth Position\n");
printf("7. Display\n");
printf("8. Search\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1://calls insertatBegining
printf("Enter a value to insert at beginning: ");
scanf("%d", &value);//input value to be insertedd
insertAtBeginning(&head, value);//insert at begining
break;
case 2://insert at end
printf("Enter a value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3://delete the first node
deleteAtBeginning(&head);
break;
case 4:
deleteAtEnd(&head);//deleted the last element
break;
case 5://insert at specific position
printf("Enter a value to insert and the position: ");
scanf("%d %d", &value, &position);
insertAtNthPosition(&head, value, position);
break;
case 6:
printf("Enter the position to delete: ");
scanf("%d", &position);//input he position
deleteAtNthPosition(&head, position);
break;
case 7: display(head);
break;
case 8:
printf("\nEnter the value to search:");
scanf("%d",&value);
search(head,value);
break;
case 9:
printf("\nExiting...\n");//it will exit the execution
break;
default:
printf("Invalid choice! Please try again.\n");//when entered envalid choice
}
} while (choice != 9);//exit when ch==9
return 0;
}
Output:

PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract7>gccSinglyLL.c
PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract7> ./a.exe

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 1
Enter a value to insert at beginning: 20

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 2
Enter a value to insert at end: 30

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 5
Enter a value to insert and the position: 40
1

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 7
Linked List: 20 -> 40 -> 30 -> NULL

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 8
Enter the value to search:40
Element 40 found at posi 1
Singly Linked List Operations:
1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 8

Enter the value to search:99


Elment 99 not found in LL
Singly Linked List Operations:
1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 3

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 4

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 7
Linked List: 40 -> 30 -> NULL

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 6
Enter the position to delete: 1

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 3

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 8

Enter the value to search:40


List is empty!

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete at Beginning
4. Delete at End
5. Insert at nth Position
6. Delete at nth Position
7. Display
8. Search
9. Exit
Enter your choice: 9

Exiting...
PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract7>

You might also like