0% found this document useful (0 votes)
7 views19 pages

Singly Linked Lists

The document discusses linked lists as a flexible data structure that allows dynamic memory allocation and efficient insertion and deletion operations. It compares linked lists with arrays, highlighting their advantages and different types such as singly linked lists, circular linked lists, and doubly linked lists. Additionally, it provides code examples for basic operations like creating, inserting, and deleting elements in a singly linked list.

Uploaded by

akul.kumar
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)
7 views19 pages

Singly Linked Lists

The document discusses linked lists as a flexible data structure that allows dynamic memory allocation and efficient insertion and deletion operations. It compares linked lists with arrays, highlighting their advantages and different types such as singly linked lists, circular linked lists, and doubly linked lists. Additionally, it provides code examples for basic operations like creating, inserting, and deleting elements in a singly linked list.

Uploaded by

akul.kumar
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/ 19

Linked list

9-May-24 Dept. of I&CT 1


Why Linked Lists?
• Advantages of Arrays:
❖ Data access is faster
❖ Simple
• Disadvantages:
❖Size of the array is fixed.
❖Array items are stored contiguously.
❖Insertion and deletion operations involve tedious job of shifting the elements
with respect to the index of the array.

9-May-24 Dept. of I&CT 2


Introduction
• A linked list is a data structure which can change during execution.
• Successive elements are connected by pointers.
• Last element points to NULL.
• It can grow or shrink in size during execution of a program.
• It can be made just as long as required.
• It does not waste memory space.

head

A B C

9-May-24 Dept. of I&CT 3


Introduction
• Keeping track of a linked list:
• Must know the pointer to the first element of the list (called start, head, etc.).

• Linked lists provide flexibility in allowing the items to be rearranged


efficiently.
• Insert an element.
• Delete an element.

9-May-24 Dept. of I&CT 4


Illustration: Insertion

A B C

Item to be
tmp X inserted

A B C

curr
X

9-May-24 Dept. of I&CT 5


Illustration: Deletion
Item to be deleted
A B C

tmp
curr

A B C

9-May-24 Dept. of I&CT 6


Array versus Linked Lists
• Arrays are suitable for:
• Inserting/deleting an element at the end.
• Randomly accessing any element.
• Searching the list for a particular value.
• Linked lists are suitable for:
• Inserting an element.
• Deleting an element.
• Applications where sequential access is required.
• In situations where the number of elements cannot be predicted beforehand.

9-May-24 Dept. of I&CT 7


Types of Lists
• Depending on the way in which the links are used to maintain adjacency,
several different types of linked lists are possible.

• Linear singly-linked list (or simply linear list)


• One we have discussed so far.

head

A B C

9-May-24 Dept. of I&CT 8


• Circular linked list
• The pointer from the last element in the list points back to the first element.

head

A B C

9-May-24 Dept. of I&CT 9


• Doubly linked list
• Pointers exist between adjacent nodes in both directions.
• The list can be traversed either forward or backward.
.

head

A B C

9-May-24 Dept. of I&CT 10


Basic Operations on a List
• Creating a list
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Concatenating two lists into one

9-May-24 Dept. of I&CT 11


Implementing Linked Lists: Singly Linked List(SLL)
struct node
{
int data;
struct node *link;
}*head; //keep the head pointer as global or define in main and pass it to the functions

9-May-24 Dept. of I&CT 12


void create_list()
{
struct node *cur;
cur=head;
struct node *temp=(struct node *) malloc(sizeof(struct node));
printf("Enter the value:“);
scanf(“%d”, temp->data);
temp->link=NULL;
if(head==NULL)
{
head=temp;
}
else
{
while(cur->link!=NULL){
cur=cur->link;}
cur->link=temp;
}}
9-May-24 Dept. of I&CT 13
void print()
{
struct node *h=head;
if(h==NULL)
printf("List is empty\n“);
while(h!=NULL)
{
printf(“%d->”,h->data);
h=h->link;
}
}

9-May-24 Dept. of I&CT 14


void ins_beg()
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
printf("Enter the value:“);
scanf(“%d”, temp->data);
temp->link=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->link=head;
head=temp;
}
}9-May-24 Dept. of I&CT 15
void ins_at_pos() if(head==NULL)
{ {
int pos; head=temp;
struct node *temp=(struct node }
*)malloc(sizeof(struct node)); else
struct node *t,*t1; {
printf("Enter the value to be inserted \n“); t=head;
scanf((“%d”,&temp->data); for(int i=1;i<pos-1;i++)
temp->link=NULL; t=t->link;
printf("Enter the position:\n“); t1=t->link;
scanf(“%d”,&pos); t->link=temp;
temp->link=t1;
}
}

9-May-24 Dept. of I&CT 16


void del_beg()
{
struct node *temp;
temp=head;
head=head->link;
printf("\nDeleted element is:%d“,temp->data);
free(temp);
}

9-May-24 Dept. of I&CT 17


void del_item() if(cur==head)
{ {
struct node *cur, *prev; head=head->link;
int item; printf("Data Deleted: %d“, item);
if(head==NULL) }
printf("No records to delete\n“); if(cur==NULL)
else {
{ printf("Record not found\n“);
printf("Enter the data to be deleted: “); return;
scanf(“%d”,&item); }
cur=head; else
while((cur!=NULL)&&(cur->data!=item)) {
{ prev->link=cur->link;
prev=cur; printf("Data deleted is:%d “, item);
cur=cur->link; }
} free(cur);
}
}

9-May-24 Dept. of I&CT 18


int main()
{
while(1)
{
printf(“1. Create list 2.Insert Beginning \t 3. Insert at a position\t 4. Print\t 5. Delete Beg \t 6. Delete given a position \t 7. Exit\n“);
int ch;
scanf(“%d”, &ch);
switch(ch)
{
case 1: create_list();
break;
case 2: ins_beg(); break;
case 3: ins_pos();break;
case 4:.print(); break;
case 5: del_beg();break;
case 6: del_pos(); break;
case 7: exit(0);
}
}
return 0;
}
9-May-24 Dept. of I&CT 19

You might also like