You are on page 1of 5

Data Structures & Algorithms / Data Structures

Hamdard University

LAB SESSION 01
LINKED LIST
Objectives:
To understand the basic concept of linked list.
To understand the operation performed on linked list.
To implement linked list using arrays and pointers.
Introduction
A linked list is an ordered collection of finite, homogeneous data elements called nodes where the
linear order is maintained by means of links or pointers.
Depending on the requirements the pointer are maintained, and accordingly the linked list can be
classified into three major groups: single linked list, circular linked list and double linked list.
In computer science, a linked list is a data structure consisting of a group of nodes which together
represent a sequence. Under the simplest form, each node is composed of a datum and a reference (in
other words, a link) to the next node in the sequence; more complex variants add additional links. This
structure allows for efficient insertion or removal of elements from any position in the sequence.

The principal benefit of a linked list over a conventional array is that the list elements can easily be
inserted or removed without reallocation or reorganization of the entire structure because the data
items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal
of nodes at any point in the list, and can do so with a constant number of operations if the link
previous to the link being added or removed is maintained during list traversal.
On the other hand, simple linked lists by themselves do not allow random access to the data, or any
form of efficient indexing. Thus, many basic operations such as obtaining the last node of the list
(assuming that the last node is not maintained as separate node reference in the list structure), or

Data Structures & Algorithms / Data Structures
Hamdard University

finding a node that contains a given datum, or locating the place where a new node should be
inserted may require scanning most or all of the list elements.
Definition
A linked list is an ordered collection of finite, homogeneous data elements called nodes where the
linear order is maintained by means of links or pointers.
Anatomy of Linked List
Each node contains a value and a link (pointer or reference) to some other node
The last node contains a null link
The list may (or may not) have a header
A nodes successor is the next node in the sequence
The last node has no successor
A nodes predecessor is the previous node in the sequence
The first node has no predecessor
A lists length is the number of elements in it
A list may be empty (contain no elements)
Type of Linked Lists
Depending on the requirements the pointer are maintained, and accordingly the linked list can be
classified into three major groups: single linked list, circular linked list and double linked list.
Single Linked List
A single linked list is a linear collection of data elements. The elements in a single linked list can
be visited starting from beginning and towards its end i.e. only in one direction. Therefore the
single linked list is also called one way list or one way chain.
Each node in a single linked list consists of at least two fields. The first field contains the data or
value called data field. The second field contains the pointer or link to the next node in the list.
This field is called the linked field or pointer field. The linked field of the last node contains a
NULL value. A NULL value I the pointer field indicated that pointer does not point to any other
node.

Data Structures & Algorithms / Data Structures
Hamdard University


The variable head contains the memory address of the first node of the list. The linked list is
accessed with reference to this pointer variable. If head has a NULL value it means that the list
is empty.
The last node in the list contains a NULL value in tis pointer field. If a new item is to be added at
the end of the linked list, then a memory address is assigned to the pointer field of the last
node and a new item is added.
In a single linked list each node has only the address of the next node in the link. It does not
contain the address of the previous node. Thus it can only be visited in one direction ie starting
from the beginning towards the end of the list.
Representation of a Linked List in Memory
The storage technique of linked list is different from that of linear arrays. The item if linked list are
stored in memory in scattered form but these are linked together through pointers. Each item of the
list is called object.
#include<iostream.h>
struct node
{
int data;
node * link;
};
node * head;
main()
{
head = new node;
head->data=405;
head->link = NULL;
cout<<The value in the starting node = <<head->data;
}


Data Structures & Algorithms / Data Structures
Hamdard University


Deletion in Linked List
A node can be deleted in a linked list at any of these locations:
At the end of the list
At the beginning of the list
At specified location in the list
Deletion at the End of the List
#include<iostream.h>
#include<conio.h>

struct node{
int data;
node *link;
};
node *head,*temp,*current;
void array_to_list(int array[],int size){
inti=0;
while(i<size)
{
if(head==NULL)
{
head=new node;
cout<<"New list created"<<endl;
head->data=array[i];
head->link=NULL;
cout<<"Item inserted: "<<array[i] <<endl;
i++;
//return;
getche();
}
else
{
current=head;
//go to end of list

Data Structures & Algorithms / Data Structures
Hamdard University


{
array[i]=i+1;
}
array_to_list(array,size);
display_list();
}

Lab Exercise:
Instructions:
Make the appropriate functions for the following lab task. Implement the function in main(). Write
the code by hand and take the snapshot of the output screen.
1) Write a program that finds the highest value in linked list.
2) Write a program that deletes alternate values in a linked list.
3) Write a program that writes 20 integers to a text file. Now the program must be able to read that
file and store those integers in a linked list.
4) Write a program that writes the elements of a linked list to a text file.
5) Write a program that swaps the head and the tail value of a linked list.
6) Write a program that stores the linked list in an array. Print the array.

You might also like