0% found this document useful (0 votes)
8 views24 pages

Linked List DS

The document contains a C program that implements a singly linked list with various operations such as insertion, deletion, finding the first element, finding the position of an element, checking the length, checking if the list is empty, and printing the list. It includes functions to create new nodes, handle memory allocation, and manage the linked list structure. The main function provides a user interface to interact with the linked list through a menu-driven approach.

Uploaded by

kvmail801
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)
8 views24 pages

Linked List DS

The document contains a C program that implements a singly linked list with various operations such as insertion, deletion, finding the first element, finding the position of an element, checking the length, checking if the list is empty, and printing the list. It includes functions to create new nodes, handle memory allocation, and manage the linked list structure. The main function provides a user interface to interact with the linked list through a menu-driven approach.

Uploaded by

kvmail801
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
You are on page 1/ 24

A.

Krishna Vamshi
2024BCY0033

CODE
#include <stdio.h>
#include <stdlib.h>

typedef struct NODE


{
int data;
struct NODE* next;
} node;

int last = 0; // Tracks the number of elements in the list

node* NEW_NODE(int data, node* next)


{
node* newnode = (node*)malloc(sizeof(node));
if (newnode == NULL)
{
printf("Memory allocation failed!\n");
exit(1);
}
newnode->data = data;
newnode->next = next;
return newnode;
}

node* NULL_NODE(int data)


{
return NEW_NODE(data, NULL);
}

node* INSERT(int num, int p, node* L)


{
if (p < 1 || p > last + 1)
{
printf("Invalid position\n");
return L;
}

if (p == 1)
{
node* newnode = NULL_NODE(num);
newnode->next = L;
last++;
return newnode;
}

node* temp = L;
for (int i = 1; i < p - 1; i++)
{
temp = temp->next;
}

node* newnode = NEW_NODE(num, temp->next);


temp->next = newnode;
last++;
return L;
}

node* DELETE(int p, node* L)


{
if (p < 1 || p > last)
{
printf("Invalid position\n");
return L;
}

node* temp = L;
if (p == 1)
{
L = temp->next;
free(temp);
last--;
return L;
}

for (int i = 1; i < p - 1; i++)


{
temp = temp->next;
}

node* toDelete = temp->next;


temp->next = toDelete->next;
free(toDelete);
last--;
return L;
}

int FIRST(node* L)
{
if (L == NULL)
{
printf("The list is empty.\n");
return -1;
}
return L->data;
}

int FIND(int num, node* L)


{
node* temp = L;
int position = 1;
while (temp != NULL)
{
if (temp->data == num)
{
return position;
}
temp = temp->next;
position++;
}
return -1;
}

int LENGTH(node* L)
{
int count = 0;
node* temp = L;
while (temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}

int Isempty(node* L)
{
return (L == NULL);
}

void PRINT(node* HEAD)


{
node* current = HEAD;
while (current != NULL)
{
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

int main() {
node* L = NULL;
int ch;

while (1)
{
printf("Enter the operation you want to perform:\n");
printf("1. Insert\n2. Delete\n3. First\n4. Find position\n5.
Length\n6. Is empty\n7. Print\n8. Exit\n");
scanf("%d", &ch);

if (ch == 1)
{
int num, p;
printf("Enter the number you want to insert: ");
scanf("%d", &num);
printf("Enter the position (1-based index): ");
scanf("%d", &p);
L = INSERT(num, p, L);
}
else if (ch == 2)
{
int p;
printf("Enter the position to delete (1-based index): ");
scanf("%d", &p);
L = DELETE(p, L);
}
else if (ch == 3)
{
int first = FIRST(L);
if (first != -1)
{
printf("First number is: %d\n", first);
}
}
else if (ch == 4)
{
int num;
printf("Enter the number to find: ");
scanf("%d", &num);
int pos = FIND(num, L);
if (pos != -1)
{
printf("Number found at position: %d\n", pos);
}
else
{
printf("Number not found.\n");
}
}
else if (ch == 5)
{
printf("Length of the list: %d\n", LENGTH(L));
}
else if (ch == 6)
{
if (Isempty(L))
{
printf("The list is empty.\n");
}
else
{
printf("The list is not empty.\n");
}
}
else if (ch == 7)
{
if (!Isempty(L))
{
PRINT(L);
}
else
{
printf("The list is empty.\n");
}
}
else if (ch == 8)
{
break;
}
else
{
printf("Invalid choice. Try again.\n");
}

int cho;
printf("Do you want to continue? (1 for Yes / 0 for No): ");
scanf("%d", &cho);
if (!cho)
{
break;
}
}

return 0;
}
OUTPUT
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
1
Enter the number you want to insert: 10
Enter the position (1-based index): 1
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
1
Enter the number you want to insert: 20
Enter the position (1-based index): 1
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
7
20 -> 10 -> NULL
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
1
Enter the number you want to insert: 30
Enter the position (1-based index): 3
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
7
20 -> 10 -> 30 -> NULL
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
2
Enter the position to delete (1-based index): 2
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
7
20 -> 30 -> NULL
Do you want to continue? (1 for Yes / 0 for No): 4
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
4
Enter the number to find: 10
Number not found.
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
5
Length of the list: 2
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
6
The list is not empty.
Do you want to continue? (1 for Yes / 0 for No): 1
Enter the operation you want to perform:
1. Insert
2. Delete
3. First
4. Find position
5. Length
6. Is empty
7. Print
8. Exit
8
ANALYSIS
Not including main function since it is a function with O(1) when not considering the function
calls

You might also like