0% found this document useful (0 votes)
9 views10 pages

Ds Exp6

Uploaded by

Madhura Kanse
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)
9 views10 pages

Ds Exp6

Uploaded by

Madhura Kanse
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/ 10

Date:

Experiment No. 6

Aim: To study and Implement Singly Linked List ADT.


Problem Statement: Write a menu driven program to implement operations of
singly linked list

Theory:

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements
are not stored at a contiguous location; the elements are linked using pointers. They
include a series of connected nodes. Here, each node stores the data and the address
of the next node.

Singly Linked List – In this type of linked list, one can move or traverse the linked
list in only one direction

It is the most common. Each node has data and a pointer to the next node.

Algorithm:

Traverse
Step 1: [initialize] set ptr = head
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply process to PTR -> DATA
Step 4: set ptr = ptr->next
[END OF LOOP]
Step 5: EXIT
InsertAtBeginning
Step 1: if avail = null
write overflow
Go to Step 7
[END OF IF]
Step 2: set new_node = avail
Step 3: set avail = avail -> next
Step 4: set new_node -> data = val
Step 5: set new_node -> next = head
Step 6: set head = new_node
Step 7: EXIT

InsertAtEnd
Step 1: if avail = null
write overflow
Go to Step 10
[END OF IF]
Step 2: set new_node = avail
Step 3: set avail = avail -> next
Step 4: set new_node -> data = val
Step 5: set new_node -> next = null
Step 6: set ptr = head
Step 7: repeat step 8
while ptr -> next != null
Step 8: set ptr = ptr -> next
[END OF LOOP]
Step 9: set ptr -> next = new_node
Step 10: EXIT

DeleteFromBeginning
Step 1: if head = null
write underflow
Go to Step 5
[END OF IF]
Step 2: set ptr = head
Step 3: set head = head -> next
Step 4: free ptr
Step 5: EXIT
DeleteFromEnd
Step 1: if head = null
write underflow
Go to Step 8
[END OF IF]
Step 2: set ptr = head
Step 3: Repeat Steps 4 and 5
while ptr -> next != null
Step 4: set preptr = ptr
Step 5: set ptr = ptr -> next
[END OF LOOP]
Step 6: set preptr -> next = null
Step 7: free ptr
Step 8: EXIT

Search
Step 1: [initialize] set ptr = head
Step 2: Repeat Steps 3 and 4
while ptr != null
Step 3: if item = ptr -> data
set pos = ptr
go to step 5
else
set ptr = ptr -> next
[END OF IF]
[END OF LOOP]
Step 4: set pos = null
Step 5: EXIT

Conclusion: We have studied and implemented Singly Linked List ADT


Program Code:

#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node* link;
};
struct node* start = NULL;
void createList()
{
if (start == NULL) {
int n,i;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
if (n != 0) {
int data;
struct node* newnode;
struct node* temp;
newnode = malloc(sizeof(struct node));
start = newnode;
temp = start;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
start->info = data;

for (i= 2; i <= n; i++) {


newnode = malloc(sizeof(struct node));
temp->link = newnode;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
}
}
printf("\nThe list is created\n");
}
else
printf("\nThe list is already created\n");
}
void traverse()
{
struct node* temp;

if (start == NULL)
printf("\nList is empty\n");

else {
temp = start;
while (temp != NULL) {
printf("Data = %d\n", temp->info);
temp = temp->link;
}
}
}
void insertAtFront()
{
int data;
struct node* temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->info = data;

temp->link = start;
start = temp;
}
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));

printf("\nEnter number to"


" be inserted : ");
scanf("%d", &data);

temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {
head = head->link;
}
head->link = temp;
}
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc(sizeof(struct node));
printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);

temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1) {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->link;
free(temp);
}
}
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0) {
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;

if (start == NULL)
printf("\nList is empty\n");
else {
printf("\nEnter index : ");

scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
while (i < pos - 1) {
temp = temp->link;
i++;
}
position = temp->link;
temp->link = position->link;
free(position);
}
}
int main()
{
int choice;
while (1) {
printf("\n\t1 To see list\n");
printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);

switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
Output:

You might also like