0% found this document useful (0 votes)
48 views6 pages

Linkedlistnext

A linked list is a linear data structure where each element (called nodes) points to the next node in the list using a pointer. Each node contains data and a pointer to the next node. Algorithms are presented for creating a linked list, traversing the list to display elements, and inserting, deleting nodes from different positions in the list. Linked lists offer advantages over arrays like dynamic memory allocation and efficient insertion/deletion by adjusting pointers rather than shifting elements.

Uploaded by

Rojan Adhikari
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)
48 views6 pages

Linkedlistnext

A linked list is a linear data structure where each element (called nodes) points to the next node in the list using a pointer. Each node contains data and a pointer to the next node. Algorithms are presented for creating a linked list, traversing the list to display elements, and inserting, deleting nodes from different positions in the list. Linked lists offer advantages over arrays like dynamic memory allocation and efficient insertion/deletion by adjusting pointers rather than shifting elements.

Uploaded by

Rojan Adhikari
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/ 6

Linked List

• A linked list data structure includes a series of connected nodes. Here, each node stores the
data and the address of the next node. A linked list is a linear data structure, in which the
elements are not stored at contiguous memory locations. The elements in a linked list are linked
using pointers. A linked list consists of nodes where each node contains a data field and a
reference (link) to the next node in the list.

Algorithm for creating a linked list


1. Start
2. newptr=getnode()
3. if(head=NULL) head=newptr
Otherwise,
last->next=newptr
4. last=newptr
5. Repeat from step 2 if user wants to add another node.
6. Stop
Algorithm to display (Traversal) linked list
1. Start
2. ptr=head
3. while(ptr≠NULL) print “ptr->info” ptr=ptr->next
4. Stop
Algorithm for Insertion in Linked List
I. Insertion at front
1. Start
2. newptr=getnode()
3. newptr->next=head
4. head=newptr
II. Insertion at last
1. Start
2. newptr=getnode()
3. ptr=head
4. while(ptr->next≠NULL) ptr=ptr->next
5. ptr->next=newptr
6. Stop
Insertion at anywhere
Let key be the data of the node after which we are going to add new item
1. Start
2. newptr=getnode()
3. ptr=head
4. while(ptr->info≠key AND ptr≠NULL) ptr=ptr->next
5. if(ptr=NULL) print “Node with key does not exists”
Otherwise,
i. newptr->next=ptr->next
ii. ptr->next=newpt
r 6. Stop
Algorithm for Deletion in Linked List
Let free(ptr) be a function which releases memory occupied by a node pointed by ptr.
I. Deletion at front
1. Start
2. if(head=NULL) Print “Linked list is empty”
Otherwise,
i. ptr=head
ii. head=head->nex
t iii. free(ptr)
3. Stop
II. Deletion at last
1. Start
2. if(head=NULL) Print “Linked list is empty”
Otherwise,
i. ptr=head
ii. while(ptr->next≠NULL)
prevptr=ptr ptr=ptr->next
iii. prevptr->next=NULL
iv. free(ptr)
3. Stop
III. Deletion at anywhere
Let key be the data of the node which we are going to delete.
1. Start
2. if(head=NULL) Print “Linked list is empty”
Otherwise,
i. ptr=head
ii. while(ptr->info≠key AND ptr≠NULL) prevptr=ptr ptr=ptr->next
iii. if(ptr=NULL) print “Node with key does not exists”
Otherwise,
prevptr->next=ptr->next free(ptr)
3. Stop

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

struct node
{
int info;
struct node *next;
};

struct node *head = NULL, *ptr, *newptr;

struct node *getnode()


{
struct node *np;
np = (struct node *)malloc(sizeof(struct node));
printf("Enter the data:");
scanf("%d", &np->info);
np->next = NULL;
return np;
}

void create()
{
struct node *last;
char ch;
do
{
newptr = getnode();
if (head == NULL)
head = newptr;
else
last->next = newptr;
last = newptr;
printf("Do you want to add more(y/n) : ");
scanf(" %c", &ch);
} while (ch == 'y' || ch == 'Y');
printf("\nLinked List Created\n");
getch();
}

void insert()
{
int position;
printf("Enter the position to insert: ");
scanf("%d", &position);

newptr = getnode();
if (position == 1)
{
newptr->next = head;
head = newptr;
}
else
{
ptr = head;
for (int i = 1; i < position - 1 && ptr != NULL; i++)
{
ptr = ptr->next;
}

if (ptr == NULL)
{
printf("Invalid position\n");
}
else
{
newptr->next = ptr->next;
ptr->next = newptr;
}
}

printf("Node inserted successfully\n");


}

void delete()
{
int position;
if (head == NULL)
{
printf("List is empty\n");
return;
}

printf("Enter the position to delete: ");


scanf("%d", &position);
if (position == 1)
{
ptr = head;
head = head->next;
free(ptr);
}
else
{
struct node *prev = NULL;
ptr = head;

for (int i = 1; i < position && ptr != NULL; i++)


{
prev = ptr;
ptr = ptr->next;
}

if (ptr == NULL)
{
printf("Invalid position\n");
}
else
{
prev->next = ptr->next;
free(ptr);
}
}

printf("Node deleted successfully\n");


}

void display()
{
ptr = head;
printf("The elements are\n");
while (ptr != NULL)
{
printf("%d ", ptr->info);
ptr = ptr->next;
}
}

int main()
{
int choice;
create();
while (1)
{
system("cls");
printf("Linked List\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
}
getch();
}
return 0;
}

Discussion and conclusion


In conclusion, linked lists offer several advantages in data structure implementations. They
provide dynamic memory allocation, allowing for efficient memory usage as elements can be
dynamically added or removed. Linked lists also offer ease of insertion and deletion, as these
operations only require adjustments to pointers, avoiding the need for shifting elements as in
arrays. Additionally, linked lists can easily accommodate variable-sized data. While they may
have some drawbacks, such as increased memory overhead and slower random access
compared to arrays, their flexibility and efficiency in certain scenarios make them a valuable
choice in various applications.

You might also like