Anda di halaman 1dari 5

Heavy Industries Taxila Education City (HITEC) University Taxila

Electrical Engineering

Data Structure and Algorithm

Lab # 04
Linked List
What is 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 data and a
reference (in other words, a link) to the next node in the sequence. As linked list consists of
nodes, we need to declare a structure which defines a single node. Our structure should have at
least one variable for data section and a pointer for the next node. In C++, our code would look
like this:

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

Why Linked List is Needed?


Types of Linked Lists:
1. Linear Linked List or One Way List or Singly Linked List:-
In this type of linked list every node has address of just its next node. There is also a
start node termed as Head node. And a Tail node whose next has NULL address.
2. Doubly Linked List or Two-Way Linked List or Two-Way Chain:-
In this type of linked list each node has two structure pointers. One has address of its
next node and other has address of its previous node. The next of last node and
previous of first/head node will be NULL

3. Circular Linked List:-


As the name indicates this type of linked lists is in circular form that is tail is linked with
the head by simply assigning the next of tail with the head address. Both double and
single linked list can be implemented in this manner.

Why Linked list when we have arrays?

Linked lists are preferable over arrays when:

a) You need constant-time insertions/deletions from the list (such as in real-time computing
where time predictability is absolutely critical)

b) You don't know how many items will be in the list. With arrays, you may need to re-declare
and copy memory if the array grows too big
c) You don't need random access to any elements

d) You want to be able to insert items in the middle of the list (such as a priority queue)

Arrays are preferable when:

a) You need indexed/random access to elements

b) You know the number of elements in the array ahead of time so that you can allocate the
correct amount of memory for the array

c) You need speed when iterating through all the elements in sequence. You can use pointer
math on the array to access each element, whereas you need to look up the node based on
the pointer for each element in linked list, which may result in page faults which may result in
performance hits.

d) Memory is a concern. Filled arrays take up less memory than linked lists. Each element in
the array is just the data. Each linked list node requires the data as well as one (or more)
pointers to the other elements in the linked list.
Tasks:

Task 1 Make four functions to initialize, add at end, add at beginning and display a node in the
linked list. Display function will display all the nodes of the list.

Task 2 Make a function that can compute the length of the list that is tell the number of the
nodes in a list. For this u have to first initialize the list with some nodes.

Task 3 Make a function that can search a node by roll number user provides and when found
prints the other attributes of node like age, name etc.

Anda mungkin juga menyukai