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

List and Linked List

Here are code snippets for addition and deletion operations on a singly linked list: // Addition void add(int data) { newNode = (Node*) malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if(head == NULL) { head = newNode; } else { curr = head; while(curr->next != NULL) { curr = curr->next; } curr->next = newNode; } } // Deletion void delete(int data) { curr = head; trail = NULL; if(curr != NULL && curr->data == data) { head = curr->next

Uploaded by

Fathul Amin
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)
44 views

List and Linked List

Here are code snippets for addition and deletion operations on a singly linked list: // Addition void add(int data) { newNode = (Node*) malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if(head == NULL) { head = newNode; } else { curr = head; while(curr->next != NULL) { curr = curr->next; } curr->next = newNode; } } // Deletion void delete(int data) { curr = head; trail = NULL; if(curr != NULL && curr->data == data) { head = curr->next

Uploaded by

Fathul Amin
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/ 32

LIST AND LINKED LIST

TOPIC 2
UNDERSTAND LIST AND LINKED LIST
List
List is a collection of data, element, component or objects with
similar data type.
List always represented as a record. Generally, a collection of
data items that can be selected by indices computed at run-
time, including array data structure, an arrangement of items
at equally spaced addresses in computer memory.
List can be implemented using array that contains sequence
of data/record.
List operations : insertion/add, deletion, retrieval/display,
traversal/search, check list & create list.
Example, if want to create list of 10 number, declared as

#define maxSize 10 // used to declare maximum size of array


int listNumber[maxSize]; // declaration of an array
int NoOfItem; // used to store no. of item in an array

To verify whether an array are empty or not, just check the


NoOfItem variable. If NoOfItem is equal to zero (0), it shows that
the list is empty. Looping statement can be used to traverse
the list of array; that loop accordingly to the NoOfItem in the
list. For insertion and deletion operation, the implementation is
more complicated based on the data type of the list.
Problem with array
Array implementations of lists use a static data structure. Often defined
at compile-time. Cannot be altered while program is running.
This means we usually waste space rather than have program run out.
continue
Problem with array
insert 1 here

2 4 5 8 11 13 16

1 2 4 5 8 11 13 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13
indices

It also means that it is difficult to construct ordered lists. In our implementation, data
must be added to the end. If inserted before the end, all others beneath it must
shuffle down. This is slow and inefficient.
Limitation of list by using array
An array has a limited number of elements
routines inserting a new value have to check that there is room
Can partially solve this problem by reallocating the array
as needed (how much memory to add?)
adding one element at a time could be costly
one approach - double the current size of the array
A better approach: use a Linked List
and dynamically allocate memory
Linked List
A linked list is data structure that consists of a sequence of
data records such that in each record there is a field that
contains a reference (i.e., a link) to the next record in the
sequence.
The principal benefit of a linked list over a conventional
array is that the order of the linked items may be different
from the order that the data items are stored in memory or
on disk. For that reason, linked lists allow insertion and
removal of nodes at any point in the list, with a constant
number of operations.
Diagram of a node
a node
a data a link
Linked List: Basic Ideas
A linked list is an ordered series of connected data /
nodes
Each element of the linked list has
Some data
A link to the next element
The link is used to chain the data
Example : A linked list of integers
The linked list can grow and shrink
Data Link

20 45 75 85

20 45

add(75), add(85)

20 45 75 85

delete(85), delete(45), delete(20)

75
Linked List Structure
Before writing the code to build the above list, we need two data types...
Node The type for the nodes which will make up the body of the list. Each
node contains a single client data element and a pointer to the next node
in the list. Type: struct node

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

Node Pointer The type for pointers to nodes. This will be the type of the
head pointer and the .next fields inside each node. In C and C++, no
separate type declaration is required since the pointer type is just the
node type followed by a '*'. Type: struct node*
Video 1

Introduction to linked list


Effectiveness Of Linked List
Advantages Disadvantages
Dynamically in size Complex programming
processed
Insert and delete operation High in computerizing time
doesnt need to shuffle an and memory management
existing item
Less time used in insert and Not suitable for simple list with
delete operation minimal size

Used for large size of element Complex in updating of


(unknown size) program
Differences Between List And Linked List
List Linked List
Static size of list Dynamic size of list
Add & delete item required Add & delete item required
much steps less steps
Suitable for less of list Suitable for larger of list
Easy programming Complex programming
processes processes
Easy program update Complicated program
update
Low of memory and High of memory and
computerizing time computerizing time
Types Of Linked List

a. Single linked list


b. Double linked list
c. Circular linked list
d. Circular double linked list
Single linked list
It is linear collection of data elements which are called
Nodes.
The elements may or may not be stored in consecutive
memory locations.
So pointers are used maintain linear order. Each node is
divided into two parts. The first part contains the information of
the element and is called INFO Field.
The second part contains the address of the next node and is
called LINK Field or NEXT Pointer Field.
The START contains the starting address of the linked list i.e. it
contains the address of the first node of the linked list.
The LINK Field of last node contains NULL Value which indicates
that it is the end of linked list. It is shown below:
Single linked list
Double linked list
In it each node is divided into three parts:
The first part is PREV part. It is previous pointer field. It contains the address of the
node which is before the current node.
The second part is the INFO part. It contains the information of the element.
The third part is NEXT part. It is next pointer field. It contains the address of the
node which is after the current node.

There are two pointers FIRST and LAST.


FIRST points to the first node in the list.
LAST points to the last node in the list.
The PREV field of first node and the NEXT field of last node contain NULL
value. This shows the end of list on both sides. This list can be traversed in
both directions that is forward and backward.
Double linked list
Circular linked list
In it the last node does not contain NULL pointer. Instead
the last node contains a pointer that has the address of
first node and thus points back to the first node.
It is shown below:
Circular double linked list
In doubly linked list, the next pointer of the last node points
to the first node and the previous pointer of the first node
points to the last node making the circular in both
directions.
Application of linked list
Linked lists are used in variety of applications in computer
Science. Majority, they are used in places where we have to
use dynamic memory or size of input is not known in advance .
Some of the major application :
1. Tree
2. Graph
3. Stack
4. Queue
UNDERSTAND THE OPERATION OF LINEAR
LINKED LIST
Memory Drawing
The best way to design and think about linked list code is to use a drawing to see how
the pointer operations are setting up memory.
Manipulation / Operation of Linked List
Start the first node from scratch - create
Inserting / adding new node
Deleting / removing node
Traversing

Assume, that we have a list with some nodes. Traversal is the very basic
operation, which presents as a part in almost every operation on a singly-
linked list. For instance, algorithm may traverse a singly-linked list to find a
value, find a position for insertion, etc. For a singly-linked list, only forward
direction traversal is possible.
Traversal algorithm
Beginning from the head, check, if the end of a list hasn't been reached
yet;
do some actions with the current node, which is specific for particular
algorithm;
current node becomes previous and next node becomes current. Go to
the step 1.
Example
As for example, let us see an example of summing up values in a singly-
linked list.
Example
As for example, let us see an example of summing up values in a singly-
linked list.
Video 2
Inserting a node
Deleting a node
Exercise
1. State four operations that can performed by list.
2. Draw an empty list constructed using pointer.
3. List two differences between list and linked list.
4. Draw a structure of linked list when the list only consists a
node.
Write sub code for addition and
deletion operations below.
Assume that the node structure is
defined as follows.
20 45

Struct Node
{
add(75), add(85)
int data;
Node *next;
20 45 75 85
} *head, *newNode *trail, *curr;

delete(85), delete(45), delete(20)

75

You might also like