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

C++ Unit-5 Link5sraararararraed List

Rfzzzaaaz

Uploaded by

memories.prk.27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Unit-5 Link5sraararararraed List

Rfzzzaaaz

Uploaded by

memories.prk.27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DYNAMIC MEMORY ALLOCATION

Most often we face situations where data is dynamic in nature.


That is number of data items keep on changing during execution of
program.

Example: consider list of customers in a super market. The list


grows when names of customers and details of products they
purchased have added and the list will shrink, when names of some
products available are deleted.

That means, information in list will keep on increasing or


decreasing every time. Therefore we cannot declare maximum size of
array to store all names of customers and products of super market.

For this, what we should do is ; when the list grows more memory
should be automatically allocated during run-time. Such situations
can be effectively handles by using DYNAMIC DATA STRUCTURES. One
such dynamic data structure is LINKED LIST.

The process of allocating and de-allocating memory at run-time is


known as DYNAMIC MEMORY ALLOCATION.

In c++ the operators used for dynamic memory allocation are :

new : allocates memory

delete : de-allocates memory

LINKED LIST
LIST is a set of items organized sequentially.

Ex: array

In array, sequential organization of elements is provided by index.


We use index for accessing and manipulation of array elements.

One major problem with array is that, size of array must be


specified at beginning only. But this may be a difficult task in
many practical applications.( refer dynamic memory allocation )

A complete different way to represent a list is to make each item


in the list as a part of a structure, also containing a LINK to
the structure ( node ) containing the next item. This type of list
is known as LINKED LIST.
Representation of a linked list:

data link data link data link

in above representation: data link


NODE

A NODE CONTAINS 2 FIELDS “DATA” FIELD AND “LINK” FIELD.

Data field contains the value that is inserted and link field
contains the address of next preceding node.

Linked list can be defined as a collection of structures ordered


not by their physical placement in memory but by logical links
that are stored as a part of the data in structure itself. The
link is in the form of a pointer to another structure of same type.

ADVANTAGES OF LINKED LIST:

• Since linked list is a dynamic data structure it’s size can


grow or shrink during execution of program.
• Linked list will not waste memory.
• In linked list it is easy to re-arrange elements by adjusting
the links.

DISADVANTAGES OF LINKED LIST :

• Access to an item is time-consuming.


• Linked list requires more memory space that array because it
has additional link field.

OPERATIONS THAT CAN BE PERFORMED ON LINKED LIST ARE :

1. Create a linked list


2. Insert an element into linked list
3. Deleting an element from linked list
4. Traversing through the list
5. Searching for an element in the list

➢ Elements can be inserted at beginning or end or at specified


position in the list.
➢ Elements can be deleted or erased at beginning or end or at
specified position in the list.
➢ Traversing through the list means the process of going through
all the nodes from one end to another end of a linked list.
In a singly linked list we can visit from left to right,
forward traversing, and nodes only. But in doubly linked list
forward and backward traversing is possible.

TYPES OF LINKED LIST

We can divide the linked list into the following three types based
on the order of arrangement of nodes:

1. Single linked list


2. Double linked list
3. Circular linked list

Single linked list : In this, a node contains 2 fields in which,


one field stores data items or values and link(NEXT) field stores
address of next node.

Representation of single linked list is:

Data next Data next Data next


10 2000 20 3000 30 NULL

1000 2000 3000

1000….2000….3000 are address locations of nodes ( address will


depend on type of compiler )

Say first is the first position in linked list. Let DATA be the element to be inserted in the
new node. POS is the position where the new node is to be inserted. TMP is a temporary
pointer to hold the node address. LEN is the number elements in the list. Len is initialized
to zero and first is initialized to NULL.

You might also like