Linked List DS
Linked List DS
Krishna Vamshi
2024BCY0033
CODE
#include <stdio.h>
#include <stdlib.h>
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* temp = L;
if (p == 1)
{
L = temp->next;
free(temp);
last--;
return L;
}
int FIRST(node* L)
{
if (L == NULL)
{
printf("The list is empty.\n");
return -1;
}
return L->data;
}
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);
}
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