0% found this document useful (0 votes)
4 views

Linked_List_intro

The document provides an overview of linked lists, a dynamic data structure that allows for efficient insertion and deletion of elements. It discusses various types of linked lists, including singly linked, doubly linked, and circular linked lists, and outlines basic operations such as creation, traversal, and searching. Additionally, it includes example code for creating and manipulating linked lists in a programming context.

Uploaded by

sardhiksai
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)
4 views

Linked_List_intro

The document provides an overview of linked lists, a dynamic data structure that allows for efficient insertion and deletion of elements. It discusses various types of linked lists, including singly linked, doubly linked, and circular linked lists, and outlines basic operations such as creation, traversal, and searching. Additionally, it includes example code for creating and manipulating linked lists in a programming context.

Uploaded by

sardhiksai
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/ 22

Data Structures

LINKED LIST:
(CREATE, TRAVERSE AND SEARCH OPERATION)
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

• 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.

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.
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

– Doubly linked list


• Pointers exist between adjacent nodes in both directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of the
list, head and tail.
head tail

A B C
– Circular linked list
• The pointer from the last element in the list points back
to the first element.
head

A B C

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
List is an Abstract Data Type
• What is an abstract data type?
– It is a data type defined by the user.
– Typically more complex than simple data types like int,
float, etc.
• Why abstract?
– Because details of the implementation are hidden.
– When you do some operation on the list, say insert an
element, you just call a function.
– Details of how the list is implemented or how the
insert function is written is no longer required.
Example: Working with linked list
• Consider the structure of a node as follows:
struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};
/* A user-defined data type called “node” */
typedef struct stud node; node
*head;

Creating a List
How to begin?
• To start with, we have to create a node (the
first node), and make head point to it.
head = (node *)
malloc(sizeof(node));

roll

name
head
age
Contd.
• If there are n number of nodes in the initial
linked list:
– Allocate n records, one by one.
– Read in the fields of the records.
– Modify the links of the records so that the chain is
formed.
head

A B C
node *create_list()
{
int k, n;
node *p, *head;
printf ("\n How many elements to enter?");
scanf ("%d", &n);

for (k=0; k<n; k++)


{
if (k == 0) {
head = (node *) malloc(sizeof(node));
p = head;
}
else {
p->next = (node *) malloc(sizeof(node));
p = p->next;
}

scanf ("%d %s %d", &p->roll, p->name, &p-


>age); }
p->next = NULL;
return (head);
}
Traversing the List
What is to be done?
• Once the linked list has been constructed and
head points to the first node of the list,
– Follow the pointers.
– Display the contents of the nodes as they are
traversed.
– Stop when the next pointer points to NULL.
void display (node *head)
{
int count =
1; node *p;
p = head;
while (p != NULL)
{
printf ("\nNode %d: %d %s %d", count,
p->roll, p->name, p->age); count++;
p = p->next;
}
printf ("\n");
}
Searching in List
What is to be done?
• Once the linked list has been constructed and
head points to the first node of the list,
– Ask user for data to be searched, Here Roll.
– Follow the pointers.
– Compare the contents of the nodes as they are
traversed.
– Stop when data found or traverse till the next
pointer points to NULL.
void Search (node *head, data)
{
int count =
0; node *p;
p = head;
while (p != NULL)
{
if(p->roll==data)
{ printf(“Data found: \n”);
printf ("\nNode %d: %d %s %d", count,
p->roll, p->name, p-
>age); count++; break;
} p
= p->next;
}
if(count==0)
printf(“Data Not Found”);
printf ("\n");
}

You might also like