0% found this document useful (0 votes)
10 views21 pages

Week 3

The document provides source code for implementing a singly linked list data structure in both C and Python. It includes functions for inserting, deleting, displaying, and counting nodes in the list. The code is structured to allow user interaction through a menu-driven interface for performing various operations on the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Week 3

The document provides source code for implementing a singly linked list data structure in both C and Python. It includes functions for inserting, deleting, displaying, and counting nodes in the list. The code is structured to allow user interaction through a menu-driven interface for performing various operations on the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Week 3:

Write a C & Python program to implement Linked list data structure and perform the
following operations. i) Insert an element into a list. ii) Delete an element from list iii) Search
for a key element in list. iv) count number of nodes in list.
Source code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *start = NULL, *q, *t;
void insert_beg();
void insert_end();
void insert_pos();
void display();
void delete_beg();
void delete_end();
void delete_pos();
void length();
int main()
{
int ch;
while (1)
{
printf("\n\n ---- Singly Linked List (SLL) Menu ----");
printf("\n1. Insert\n2. Display\n3. Delete\n4. Count\n5. Exit");
printf("\n\nEnter your choice (1-5): ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n ---- Insert Menu ----");
printf("\n1. Insert at beginning\n2. Insert at end\n3. Insert at specified position\n4.
Exit");
printf("\n\nEnter your choice (1-4): ");
scanf("%d", &ch);
switch (ch)
{
case 1: insert_beg(); break;
case 2: insert_end(); break;
case 3: insert_pos(); break;
case 4: exit(0);
default: printf("Wrong Choice!!\n");
}
break;
case 2: display(); break;
case 3:printf("\n ---- Delete Menu ----");
printf("\n1. Delete from beginning\n2. Delete from end\n3. Delete from specified
position\n4. Exit");
printf("\n\nEnter your choice (1-4): ");
scanf("%d", &ch);
switch (ch)
{
case 1: delete_beg(); break;
case 2: delete_end(); break;
case 3: delete_pos(); break;
case 4: exit(0);
default: printf("Wrong Choice!!\n");
}
break;
case 4: length(); break;
case 5: exit(0);
default: printf("Wrong Choice!!\n");
}
}
return 0;
}
void insert_beg()
{
int num;
t = (struct node *)malloc(sizeof(struct node));
if (!t)
{
printf("Memory allocation failed\n");
return;
}
printf("Enter data: ");
scanf("%d", &num);
t->data = num;
t->next = start;
start = t;
}
void insert_end()
{
int num;
t = (struct node *)malloc(sizeof(struct node));
if (!t)
{
printf("Memory allocation failed\n");
return;
}
printf("Enter data: ");
scanf("%d", &num);
t->data = num;
t->next = NULL;
if (start == NULL)
{
start = t;
}
else
{
q = start;
while (q->next != NULL)
q = q->next;
q->next = t;
}
}
void insert_pos()
{
int pos, i, num;
printf("Enter data: ");
scanf("%d", &num);
printf("Enter position to insert: ");
scanf("%d", &pos);
t = (struct node *)malloc(sizeof(struct node));
if (!t)
{
printf("Memory allocation failed\n");
return;
}
t->data = num;
if (pos == 1)
{
t->next = start;
start = t;
return;
}
q = start;
for (i = 1; i < pos - 1 && q != NULL; i++)
q = q->next;
if (q == NULL)
{
printf("Invalid position!\n");
free(t);
return;
}
t->next = q->next;
q->next = t;
}
void display()
{
if (start == NULL)
{
printf("List is empty!!\n");
return;
}

q = start;
printf("The linked list is:\n");
while (q != NULL)
{
printf("%d -> ", q->data);
q = q->next;
}
printf("NULL\n");
}
void delete_beg()
{
if (start == NULL)
{
printf("The list is empty!!\n");
return;
}
q = start;
start = start->next;
printf("Deleted element is %d\n", q->data);
free(q);
}
void delete_end()
{
if (start == NULL)
{
printf("The list is empty!!\n");
return;
}
if (start->next == NULL)
{
printf("Deleted element is %d\n", start->data);
free(start);
start = NULL;
return;
}
q = start;
while (q->next->next != NULL)
q = q->next;
t = q->next;
q->next = NULL;
printf("Deleted element is %d\n", t->data);
free(t);
}
void delete_pos()
{
int pos, i;
if (start == NULL)
{
printf("List is empty!!\n");
return;
}
printf("Enter position to delete: ");
scanf("%d", &pos);
if (pos == 1)
{
q = start;
start = start->next;
printf("Deleted element is %d\n", q->data);
free(q);
return;
}
q = start;
for (i = 1; i < pos - 1 && q->next != NULL; i++)
q = q->next;
if (q->next == NULL)
{
printf("Invalid position!\n");
return;
}
t = q->next;
q->next = t->next;
printf("Deleted element is %d\n", t->data);
free(t);
}
void length()
{
int num = 0;
q = start;
while (q != NULL)
{
num++;
q = q->next;
}
printf("Number of nodes in the list: %d\n", num);
}
Output:
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at beginning choice:
Enter your choice (1-4): 1
Enter data: 11
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at beginning choice:
Enter your choice (1-4): 1
Enter data: 12
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Display after inserting 12 before 11 node:
Enter your choice (1-5): 2
The linked list is:
12 -> 11 -> NULL
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at end choice:
Enter your choice (1-4): 2
Enter data: 15
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
12 -> 11 -> 15 -> NULL
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Insert at specific position i.e at 2nd location 14 is inserted:

Enter your choice (1-5): 1


---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Enter your choice (1-4): 3
Enter data: 17
Enter position to insert: 2
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
12 -> 17 -> 11 -> 15 -> NULL
Count no of nodes:
Enter your choice (1-5): 4
Number of nodes in the list: 4
Deletion of nodes:
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 3
---- Delete Menu ----
1. Delete from beginning
2. Delete from end
3. Delete from specified position
4. Exit
delete from end position-15 gets deleted
Enter your choice (1-4): 2
Deleted element is 15
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Display after deletion:
Enter your choice (1-5): 2
The linked list is:
12 -> 17 -> 11 -> NULL
Deletion from beginning: 12 gets deleted
Enter your choice (1-4): 1
Deleted element is 12
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
17 -> 11 -> NULL
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Inserting new node as we have only two nodes to delete specific to show difference we again
inserted 20 at 2nd location and deleted the same .
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at specified position
Enter your choice (1-4): 3

Enter data: 20

Enter position to insert: 2


---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
17 -> 20 -> 11 -> NULL
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 3
---- Delete Menu ----
1. Delete from beginning
2. Delete from end
3. Delete from specified position
4. Exit
Delete from specific position:20 deleted from 2nd position:
Enter your choice (1-4): 3
Enter position to delete: 2
Deleted element is 20
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
17 -> 11 -> NULL

 Python code for linked list :


class Node:
def __init__(self, data):
[Link] = data
[Link] = None
class SinglyLinkedList:
def __init__(self):
[Link] = None
def insert_beg(self, data):
t = Node(data)
[Link] = [Link]
[Link] = t
def insert_end(self, data):
t = Node(data)
if [Link] is None:
[Link] = t
else:
q = [Link]
while [Link]:
q = [Link]
[Link] = t
def insert_pos(self, pos, data):
t = Node(data)
if pos == 1:
[Link] = [Link]
[Link] = t
return
q = [Link]
for _ in range(pos - 2):
if q is None:
print("Invalid position!")
return
q = [Link]
if q is None:
print("Invalid position!")
else:
[Link] = [Link]
[Link] = t
def delete_beg(self):
if [Link] is None:
print("List is empty!")
else:
print("Deleted element is", [Link])
[Link] = [Link]
def delete_end(self):
if [Link] is None:
print("List is empty!")
elif [Link] is None:
print("Deleted element is", [Link])
[Link] = None
else:
q = [Link]
while [Link]:
q = [Link]
print("Deleted element is", [Link])
[Link] = None
def delete_pos(self, pos):
if [Link] is None:
print("List is empty!")
return
if pos == 1:
print("Deleted element is", [Link])
[Link] = [Link]
return
q = [Link]
for _ in range(pos - 2):
if [Link] is None:
print("Invalid position!")
return
q = [Link]
if [Link] is None:
print("Invalid position!")
else:
print("Deleted element is", [Link])
[Link] = [Link]
def display(self):
if [Link] is None:
print("List is empty!")
else:
q = [Link]
print("The linked list is:")
while q:
print([Link], end=" -> ")
q = [Link]
print("NULL")
def length(self):
count = 0
q = [Link]
while q:
count += 1
q = [Link]
print("Number of nodes in the list:", count)
if __name__ == "__main__":
sll = SinglyLinkedList()
while True:
print("\n\n---- Singly Linked List (SLL) Menu ----")
print("1. Insert\n2. Display\n3. Delete\n4. Count\n5. Exit")
ch = int(input("Enter your choice (1-5): "))
if ch == 1:
print("\n---- Insert Menu ----")
print("1. Insert at beginning\n2. Insert at end\n3. Insert at specified position\n4. Exit")
ch2 = int(input("Enter your choice (1-4): "))
if ch2 == 1:
data = int(input("Enter data: "))
sll.insert_beg(data)
elif ch2 == 2:
data = int(input("Enter data: "))
sll.insert_end(data)
elif ch2 == 3:
data = int(input("Enter data: "))
pos = int(input("Enter position to insert: "))
sll.insert_pos(pos, data)
elif ch2 == 4:
break
else:
print("Wrong Choice!!")
elif ch == 2:
[Link]()
elif ch == 3:
print("\n---- Delete Menu ----")
print("1. Delete from beginning\n2. Delete from end\n3. Delete from specified position\n4.
Exit")
ch2 = int(input("Enter your choice (1-4): "))
if ch2 == 1:
sll.delete_beg()
elif ch2 == 2:
sll.delete_end()
elif ch2 == 3:
pos = int(input("Enter position to delete: "))
sll.delete_pos(pos)
elif ch2 == 4:
break
else:
print("Wrong Choice!!")
elif ch == 4:
[Link]()
elif ch == 5:
break
else:
print("Wrong Choice!!")
Output:
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at beginning choice:
Enter your choice (1-4): 1
Enter data: 11
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at beginning choice:
Enter your choice (1-4): 1
Enter data: 12
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Display after inserting 12 before 11 node:
Enter your choice (1-5): 2
The linked list is:
12 -> 11 -> NULL
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at end choice:
Enter your choice (1-4): 2
Enter data: 15
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
12 -> 11 -> 15 -> NULL
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Insert at specific position i.e at 2nd location 14 is inserted:

Enter your choice (1-5): 1


---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Enter your choice (1-4): 3
Enter data: 17
Enter position to insert: 2
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
12 -> 17 -> 11 -> 15 -> NULL
Count no of nodes:
Enter your choice (1-5): 4
Number of nodes in the list: 4
Deletion of nodes:
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 3
---- Delete Menu ----
1. Delete from beginning
2. Delete from end
3. Delete from specified position
4. Exit
delete from end position-15 gets deleted
Enter your choice (1-4): 2
Deleted element is 15
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Display after deletion:
Enter your choice (1-5): 2
The linked list is:
12 -> 17 -> 11 -> NULL
Deletion from beginning: 12 gets deleted
Enter your choice (1-4): 1
Deleted element is 12
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
17 -> 11 -> NULL
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Inserting new node as we have only two nodes to delete specific to show difference we again
inserted 20 at 2nd location and deleted the same .
Enter your choice (1-5): 1
---- Insert Menu ----
1. Insert at beginning
2. Insert at end
3. Insert at specified position
4. Exit
Insert at specified position
Enter your choice (1-4): 3

Enter data: 20

Enter position to insert: 2


---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
17 -> 20 -> 11 -> NULL
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 3
---- Delete Menu ----
1. Delete from beginning
2. Delete from end
3. Delete from specified position
4. Exit
Delete from specific position:20 deleted from 2nd position:
Enter your choice (1-4): 3
Enter position to delete: 2
Deleted element is 20
---- Singly Linked List (SLL) Menu ----
1. Insert
2. Display
3. Delete
4. Count
5. Exit
Enter your choice (1-5): 2
The linked list is:
17 -> 11 -> NULL

 Week 4
Write a C & Python program to implement the following using a singly linked list.
i) Stack ii) Queue

You might also like