0% found this document useful (0 votes)
19 views5 pages

Dsa Lab 06

This document discusses linked lists as a data structure for storing indefinite amounts of data by connecting nodes sequentially through pointers, compares linked lists to arrays, and describes how to implement a singly linked list in C++ including inserting and deleting nodes at the start, end, or middle of the list. It also briefly describes different types of linked lists like singly linked, doubly linked, and circular linked lists.

Uploaded by

Hanzla Zafar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Dsa Lab 06

This document discusses linked lists as a data structure for storing indefinite amounts of data by connecting nodes sequentially through pointers, compares linked lists to arrays, and describes how to implement a singly linked list in C++ including inserting and deleting nodes at the start, end, or middle of the list. It also briefly describes different types of linked lists like singly linked, doubly linked, and circular linked lists.

Uploaded by

Hanzla Zafar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

University of Engineering and Technology, Taxila

Data Structures and Algorithms

Experiment 6
Arrays: Implementation of Linked List(part 2)

CLO-1: Construct the experiments/projects of varying complexities.

CLO-2: Use modern tools and languages.

CLO-3: Demonstrate an original solution of problem under discussion.

CLO-4: Work individually as well as in teams


1
Linked List:
Basically, a linked list is a data structure that can store an indefinite amount of items. These
items don’t have to be the same data type. As long as pointers connect these items in a
sequential manner, it technically counts as a linked list.

Arrays vs. Linked Lists

Usually, programmers learn about arrays before they learn about linked lists, so it’s useful to
compare the two. Although arrays and linked lists are both data structures for storing multiple
values, there are some major differences between the two. There are pros and cons to using
each.

Array Linked Lists

 Physically Contiguous  Logically Contiguous Only


 Fixed Length  Changeable Length
 Access Elements by Index  Access Elements by Traversal
 Insertion/Removal is Costly  Insertion/Removal is Efficient

Memory Allocation

Linked list is one of the fundamental data structures and can be used to implement other data
structures. In a linked list there are different numbers of nodes. Each node consists of two
fields. The first field holds the value or data and the second field holds the reference to the
next node or null if the linked list is empty.
2

Unlike a linked list, an array is both physically and logically contiguous. A good way to think
about an array is to imagine a single chunk of memory cells. The elements of an array are
actually located next to each other as consecutive memory addresses in RAM (Random
Access Memory).

The elements of a linked list, by contrast, are logically contiguous in their implementation,
but not physically contiguous in memory. A linked list doesn’t occupy a single block of
memory, allowing it to use whatever memory “scraps” are available. Because of this
flexibility, node elements can be different sizes as well as different data types.

Accessing Elements

One advantage an array has over a linked list is indexing. Instead of having to traverse down
every element in the array, you can access an element directly by its index number. This costs
only one instruction cycle. To get the fifth element of an array, you only need to perform one
lookup operation. The same operation in a linked list would cost you five instruction cycles
since you have to “inchworm” your way down the list.

In technical terms, retrieving an element by its index number in an array is always an O(1)
operation. By contrast, the same logically equivalent operation in a linked list would take a
variable amount of runtime depending on the number of elements preceding the one you’re
trying to access. Thus, retrieving a particular element in a linked list would be an O(n)
operation at worst and an O(1) operation at best (if the sought-after element happens to be at
the front of the list).
3
Inserting/Removing Elements

One advantage a linked list has over an array is the ease of inserting and removing elements.
In order to insert an element into the middle of an array, you have to shift each element over
to make space for insertion. This can be quite costly. In a linked list, however all you need to
do is reassign the pointer of one node to make it point to the new element and have that new
element point to the next logical node in the sequence.

Types of Linked Lists

There are several kinds of linked lists. In a singly linked list, each node has only one pointer,
commonly called “next” since it points to the next node in the sequence. The first node is
called the “head” of the list. The last node in the list points to NULL.

A singly linked list can become a circular node by assigning the address of the head node to
the tail node instead of NULL. This isn’t a particularly useful linked list, but it illustrates the
flexibility of this data structure.

A doubly linked list has two pointer variables, one pointing forward to the next node and one
pointing back to the previous node.
4

Lab Tasks:
Implement singly link list in C ++ and perform following operation on it

 insertion and deletion at the start


 insertion and deletion at end
 insertion and deletion somewhere in the middle of link list.

You might also like