Chapter 1 Part Two
Chapter 1 Part Two
8
Cont..
Here are some disadvantage of arrays
The size of the array is fixed. Most often this size is specified
at compile time. This makes the programmers to allocate
arrays, which seems "large enough" than required.
Inserting new elements at the front is potentially expensive
because existing elements need to be shifted over to make
room.
9
Array Vs Linked list
10
Linked List concepts
• A linked list is a non-sequential collection of data items. It is a
dynamic data structure. For every data item in a linked list, there is an
associated pointer that would give the memory location of the next
data item in the linked list.
• The data items in the linked list are not in consecutive memory
locations. They may be anywhere, but the accessing of these data
items is easier as each data item contains the address of the next data
item.
11
Advantage of Linked List over array
Linked lists are dynamic data structures. i.e., they can grow or shrink
during the execution of a program.
Linked lists have efficient memory utilization. Here, memory is not
pre allocated. Memory is allocated whenever it is required and it is
de-allocated (removed) when it is no longer needed.
Insertion and Deletions are easier and efficient. Linked lists provide
flexibility in inserting a data item at a specified position and deletion
of the data item from the given position.
Many complex applications can be easily carried out with linked lists.
12
Disadvantage of Linked List
It consumes more space because every node requires a additional
pointer to store address of the next node.
Searching a particular element in list is difficult and also time
consuming.
13
Structure
• Structures are aggregate data types built using elements of primitive
data types.
int hour;
int minute;
int second;
};
• The struct keyword creates a new user defined data type that is used
14
Structure
• Structure variables are declared like variables of other types.
Syntax: struct <structure tag> <variable name>;
E.g. Time timeObject,
Time *timeptr;
• Accessing Members of Structure Variables
• The Dot operator(.): to access data members of structure variables.
• The Arrow operator(->): to access data members of pointer variables pointing
to the structure.
E.g. Print member hour of timeObject and timeptr.
cout<< timeObject.hour; or
cout<<timeptr->hour;
• TIP: timeptr->hour is the same as (*timeptr).hour.
15
Single Linked List
• A linked list allocates space for each element separately in its own
block of memory called a "node".
• The list gets an overall structure by using pointers to connect all its
nodes together like the links in a chain.
• Each node contains two fields; a "data" field to store whatever
element, and a "next" field which is a pointer used to link to the next
node.
• Each node is allocated in the using new(), so the node memory
continues to exist until it is explicitly de-allocated using free().
• The front of the list is a pointer to the “start” node
16
Cont..
17
Cont.…
• The beginning of the linked list is stored in a "start" pointer which
points to the first node.
• The first node contains a pointer to the second node. The second
node contains a pointer to the third node, ... and so on.
• The last node in the list has its next field set to NULL to mark the end
of the list.
• Code can access any node in the list by starting at the
start and following the next pointers.
• The start pointer is an ordinary local pointer variable, so it is drawn
separately on the left top to show that it is in the stack.
• The list nodes are drawn on the right to show that they are allocated
in the heap. 18
Implementation of Single linked lists
Before writing the code to build the above list, we need to create a
start node, used to create and access other nodes in the linked list.
Creating a structure with one data item and a next pointer, which will
be pointing to next node of the list. This is called as self-referential
structure.
Initialize the start pointer to be NULL.
struct slinklist
{ data next
int data; node:
start
struct slinklist node *start = NULL; Empty list:
NULL
19
The basic operation in single linked list
The basic operations in a single linked list are:
Creation.
Insertion.
Deletion.
Traversing.
20
Creating a node for single linked list
• Creating a singly linked list starts with creating a node.
• Sufficient memory has to be allocated for creating a node.
• The information is stored in the memory, allocated by
using the new() function.
• The function getnode(), is used for creating a node, after
allocating memory for the structure of type node, the
information for the item (i.e.,data) has to be read from the
user, set next field to NULL and finally returns the address
of the node.
21
Cont..
22
Insertion Into Linked Lists
23
Insertion of nodes in to linked list
• One of the most primitive operations that can be done in a singly
linked list is the insertion of a node.
• Memory is to be allocated for the new node (in a similar way that is
done while creating a list) before reading the data.
• The new node will contain empty data field and empty next field. The
data field of the new node is then stored with the information read
from the user.
• The next field of the new node is assigned to NULL. The new node can
then be inserted at three different places namely:
• The front
• The end
• Somewhere in the middle 24
Inserting at the beginning
28
Inserting to the Middle
void insert_right_y(int x, int y) while(temp2->data!=y)
{ {
node *temp=new node; temp2 = temp2->next;
temp->data=x; }
temp->next=NULL; temp->next = temp2->next;
if(head==NULL) temp2->next = temp;
head = temp; }
else }
{
node *temp2 = head;
29
Inserting node at the end
The following steps are followed to insert a new node at the end of the
list:
Get the new node
If the list is empty then start = newnode.
If the list is not empty follow the steps given below:
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
30
Cont.
Figure below shows inserting a node into the single linked list at the end
31
Inserting node at the end
void insert_end(int x) node *temp2 = head;
{ while(temp2->next!=NULL)
node *temp=new node; {
temp->data=x; temp2 = temp2->next;
temp->next=NULL; }
if(head==NULL) temp2->next = temp;
head = temp; }
else }
{
32
Deletion of nodes
Another primitive operation that can be done in a singly linked list is
the deletion of a node. Memory is to be released for the node to be
deleted. A node can be deleted from the list from three different places
namely.
Deleting a node at the beginning.
Deleting a node at the end.
Deleting a node at intermediate position.
33
Delete node at the beginning
The following steps are followed, to delete a node at the beginning of
the list:
If the list is empty display “Empty List”
If the list is not empty, follow the steps given below:
temp = start;
start = start -> next;
free(temp);
34
Deleting a node from the front of a singly linked list:
void delete_front()
{
node *temp;
if(head==NULL)
cout <<"No data inside\n";
else
{
temp = head;
head = head->next;
delete temp;
}
}
35
Delete node at the end
void delete_end()
The following steps are followed, to delete a {
node at the beginning of the list: node *temp, *temp3;
If the list is empty display “Empty List” if(head==NULL)
cout <<"No data inside\n";
If the list is not empty, follow the steps else {
given below: temp = head;
while(temp->next!=NULL) {
temp = start; temp3 = temp;
start = start -> next; temp = temp->next;
}
free(temp); temp3->next = NULL;
delete temp;
}
}
36
Delete node at intermediate position
The following steps are followed, to delete a node at the beginning of
the list:
If the list is empty display “Empty List”
If the list is not empty, follow the steps given below:
temp = start;
start = start -> next;
free(temp);
37
Doubly linked list
• A double linked list is a two-way list in which all nodes will have two links. This helps in accessing
both successor node and predecessor node from the given node position. It provides bi-
directional traversing. Each node contains three fields:
Left link.
Data.
Right link.
The left link points to the predecessor node and the right link points to the successor node. The
data field stores the required data.
38
Cont.
The beginning of the double linked list is stored in a "start" pointer which points
to the first node. The first nodes left link and the last node right link is set to be
NULL;
39
Creating node for double linked list
Creating a double linked list starts with creating a node. Sufficient memory has to
be allocated for creating a node. The information is stored in the memory, allocated
by using the new node() function. The temp, is used for creating a node, after
allocating memory for the structure of type node, the information for the item (i.e.,
data) has to be read from the user and set left field to NULL and right field also set
to NULL.
void double_linked_list(int data)
{
struct dlinklist *temp;
temp=new dlinklist();
temp->data=data
temp->left=NULL;
temp->right=NULL;
}
40