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

Linked Lists

The document discusses pointers and linked lists. It covers basics of pointers like pointer variables and data types. It then discusses concepts of linked lists like nodes, inserting and deleting nodes, and navigating through the list. Linked lists allow dynamic memory allocation and are built using nodes containing data and pointers.

Uploaded by

ytbarekuk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Linked Lists

The document discusses pointers and linked lists. It covers basics of pointers like pointer variables and data types. It then discusses concepts of linked lists like nodes, inserting and deleting nodes, and navigating through the list. Linked lists allow dynamic memory allocation and are built using nodes containing data and pointers.

Uploaded by

ytbarekuk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Basics of Pointers

• Every byte in the computer’s memory has an address


• A variable that holds an address value is called a pointer
variable, or simply a pointer
• What is the data type of pointer variables?
• int* ptr;
• the ptr variable is not type int, the statement just defines a
pointer variable that points to integer types or holds the address
of integer types
• What’s wrong with the idea of a general-purpose pointer type
that holds pointers to any data type?
• the compiler needs to know what kind of variable the pointer
points to (for purpose of address calculation at run time)
Basics of Pointers
• Accessing values and pointer variables
– Accessing the value of the pointer
int* ptr;
int x=10;
ptr=&x;
cout<<ptr; //prints the value contained by ptr

Example: 0x29cf44

– Accessing the value pointed to by the pointer (by the value of the
pointer)
Cout<<*ptr;

Output: 10
Basics of Pointers
double* myPtr;
double a=3.14;
myPtr=&a; //addressof(a)

myPtr *myPtr

0x29cf44 3.14
Basics of Pointers
• What are pointers for?

 Accessing array elements

 Passing arguments to a function when the function needs


to modify the original argument (passing by reference)

 Passing arrays and strings to functions

 Obtaining memory from the system

 Creating data structures such as linked lists


Lists
• A list is a finite sequence of data items known as elements.

• Properties of lists
• Position of elements
• Length of the list
• Beginning of the list
• End of the list
• Sorted
• Unsorted
Lists (array implementation)
• An array is a kind of list where the elements are accessed by
– identifier
– index

• Static memory is allocated to an array of values

• The allocated memory will be reserved to that array even if it


is not used
 This, in fact leads to memory wastage. What if you
want the memory grow only when an element is
added?
Linked list (Pointer implementation)
• is a technique of creating a list with the ability to add and
delete items at run time (unlike arrays which allocate memory
statically)
• is a data structure that is built from structures and pointers.
• is made up of cells, each cell consisting of data of the list item
and a pointer to the next cell on the list
• If the list is a1, a2, . . . , an, the cell holding ai has a pointer to
the cell holding ai+1
• is made up of a chain of nodes where each node contains:
• the data item, and
• a pointer to the next node
Linked list
struct list {
char name[10];
int age;
float height;
struct list *next;
};
• A linked list based on the above ADT can be represented by a
diagram:
Linked list
• If we have to define a data structure for a linked list
struct list {
char name[10];
int age;
struct list *next;
};
list *start = NULL;

• Declaring a list type


temp
list *temp = new list;

?
Linked list
• Creating the first object (element of the list)

cout<<“Enter name: ";


cin>>temp->name;
cout<<“Enter age: ";
cin>>temp->age;
cout<<“Enter height: ";
cin>>temp->height;
start=temp;
temp->nxt=NULL;

• The last line sets the pointer from this node to the next to
NULL
Linked list
Abel
24
1.7

temp->nxt=NULL
start=NULL start?

• After creating the first list element, the start pointer will point
to this node (temp)

• The nxt pointer of this node will be NULL because there is no


another list element to point to
Operations in Linked list
• Insertion
– at the beginning
– at the end
– after specified number of nodes
• Navigation through list items
• Deletion
– from the beginning
– from the end
– a specified node
Insertion
• At the beginning (assuming the list is not empty)
– start pointer is now pointing to some node

– At first initialize node type:


list *temp=new list; //creating a list
variable and allocating memory
– Store the required values in temp
• (temp->name, temp->age, temp->height)

– The nxt pointer of temp (temp->nxt) should hold the address of


the element that was at the front.

– The start pointer (which was pointing to the element at the


front) should now hold the address of temp
temp2

start 25
1.65
temp1
nxt

start 29
temp2->nxt=start; 1.70
NULL
nxt
start=temp2;
struct list{ int main() {
int age; list *temp1=new list;
float height; temp1->age=29;
list *nxt; temp1->height=1.70;
}; temp1->nxt=NULL;
list *start = NULL; start=temp1;

list *temp2=new list;


temp2->age=25;
temp2->height=1.75;
temp2->nxt=start;
start=temp2;
return 0;
}
Insertion
• At the end
– Here the first job is to find the last node of the given linked
list
– First we declare a temp variable (list item)
– If the list is empty, the start pointer is made to point to
temp
if(start==NULL)
start=temp;

– Otherwise, we declare a second pointer, temp2, to step


through the list until we find the last node (the last node is
a node whose nxt pointer is NULL)
Insertion: At the end

list *temp2 = start; //create a temporary list object


while (temp2->nxt != NULL)
{
temp2 = temp2->nxt;
}
temp2->nxt=temp; //temp is the node to be inserted
temp->nxt=NULL;
temp2 temp2 temp2 temp2
start temp

NULL data
data data data data
*nxt
*nxt *nxt *nxt *nxt

temp2->nxt=temp;

temp->nxt=NULL;

temp2 temp
start

NULL
data data data data data
*nxt *nxt *nxt *nxt *nxt
Insertion
• After specified number of nodes
– The first step is to navigate through the specified number of
nodes with a temporary node variable, temp1
node temp1=new node;
temp1=start;
cout<<“Enter node number:”;
cin>>node_number;
for(int i=1;i<node_number;i++) {
temp1 = temp1->next; // go to the next node
if( temp1 == NULL ) {
cout<<node_number<<" node is not exist"<< endl;
break;
}
}

– temp1 now points to the required position in the list


Insertion: after…..

– The next step is to create the node to be inserted and store


the required data
node *temp=new node;
temp->data=values;

– Last step is to establish the connection between new node


and the existing linked list
temp->next=temp1->next;
temp1->next=temp;
temp1 temp1

start NULL
data data data data

*nxt *nxt *nxt *nxt

start temp1 temp

NULL
data data data data

*nxt *nxt *nxt *nxt

temp->next=temp1->next;
temp1->next=temp;
Navigation
• There may be a need to go through all items in the list for
some kind of operation
• Consider the structure of employee with data members
Name, Age Salary

• Example:
• Observe age groups in the list
• Observe salary distribution

• The implementation of the navigation operation is just the


same as the “insert at the end” operation
• Perform the required operation until NULL pointer is obtained
Navigation …

• Displaying member data of the items in the list

1. Set a temporary pointer to point to the same thing as the


start pointer.
2. If the pointer points to NULL, display the message "End
of list" and stop.
3. Otherwise, display the details of the node pointed to by
the start pointer.
4. Make the temporary pointer point to the same thing as
the nxt pointer of the node it is currently indicating.
5. Jump back to step 2.
Deletion
• From the beginning
– Deleting is usually followed by releasing the memory that
was allocated.
– In c++
• new keyword is used to allocate memory
• delete keyword is used to release memory
– Steps to delete
1. Create a temporary node called *temp and allocate
memory
2. Copy the start pointer (head) to temp
3. Transfer the address temp->next to start pointer (head)
4. Delete the memory allocated to temp
Deletion: from beginning

node *temp=new node;


temp=start;
start=temp->next;
delete temp;
start

next
data next data next data
NULL
Deletion
• From the end
– The last node`s next of the linked list always point to NULL.

– So when we delete the last node, the previous node of last


node will point to NULL

– So, we will track last node and previous node of the last
node in the linked list

– next pointer of previous node will be made to point to Null

– Last node will be deleted


Deletion: from the end
• Create temporary nodes called current and previous

node *current=new node;


node *previous=new node;
current=start;
while(current->next!=NULL) {
previous=current;
current=current->next;
}
previous->next=NULL; //making it the last node in
the list
delete current;
Deletion
• A specified node
– We need to find the specified node and its previous node
node *current=new node;
node *previous=new node;
current=start;
for(int i=1;i<n;i++) {
previous=current;
current=current->next;
}
previous->next=current->next;
delete current;
Linked list variations

• Singly linked lists

• Circular linked lists

• Doubly linked lists


Circular Linked lists
• Linked lists whose last node points to NULL are called linear or
open linked lists (singly linked lists)

• In a singly linked list, given a pointer to a node anywhere in


the list, we can access all the nodes that follow but none of
the nodes that precede it.

• We must always have a pointer to the beginning of the list to


be able to access all the nodes in the list.

• We create a circular linked list by making the pointer in the


next member of the last node point back to the first node
instead of containing NULL
Circular Linked lists

• Structure definition is similar to the singly linked (open)


struct node {
int age;
float
head
height; ____
node *next;
};
node *head=NULL;
head->next=head;
Circular Linked lists
• If we let our external pointer point to the last item in the list
rather than the first, we have direct access to both the first
and the last elements in the list

• To check whether a current pointer points to the last node of


a circular list, we check for current->next==head instead of
current->next==NULL

• The algorithms insertion into and deletion from a circular list


must ensure that the next field of the last node points to the
first node of the list upon completion.
Inserting a node to a circular Linked List
• Inserting node at the beginning
• Step
1. Create a temporary pointer temp and assign head to
temp
node *temp=new node;
temp=head;
2. Navigate through the list until last node is found
while(temp->next!=head)
temp=temp->next;
3. Create a new node to be inserted and load data field
node *ND=new node;
ND->age=20;
ND->height=1.75;
Inserting a node to a circular Linked List
• Inserting node at the beginning

4. Take care of the connection or link between the new


node and the existing linked list
temp->next=ND;
ND->next=head;
head=ND; temp

ND
head data data data

data
Inserting a node to a circular Linked List
• Inserting node at the end
• Step
1. Create a temporary pointer temp and assign head to
temp
node *temp=new node;
temp=head;
2. Navigate through the list until last node is found
while(temp->next!=head)
temp=temp->next;
3. Create a new node to be inserted and load data field
node *ND=new node;
ND->age=20;
ND->height=1.75;
Inserting a node to a circular Linked List
• Inserting node at the end

4. Take care of the connection or link between the new


node and the existing linked list
ND->next=temp->next;
temp->next=ND;
temp ND

data
head data data data
Inserting a node to a circular Linked List

• Inserting a node after a specified node

– The algorithm for inserting a node after a specified node is


the same as that of open linked lists
Deleting a node from a circular Linked List
• Deleting a node from front
• The algorithm for deleting a node from front is the same as
that of open linked lists

node *temp=new node;


node *temp2=new node;
temp2=head;
temp=head;
while(temp->next!=head)
temp=temp->next;
head=temp2->next;
temp->next=head;
delete temp2;
Deleting a node from a circular Linked List
• Deleting a node from back(end)
• The algorithm for deleting a node at the end is the same as
that of open linked lists

node *current=new node;


node *previous=new node;
current=head;
while(current->next!=head){
previous=current;
current=current->next;
}
previous->next=head;
delete current;
Deleting a node from a circular Linked List

• Deleting a specified node

– The algorithm for deleting a specified node in a circular


linked list is similar to that of open linked list
Doubly linked lists
• The singly linked list allows for direct access from a list node
only to the next node in the list
• A doubly linked list allows convenient access from a list node
to the next node and also to the preceding node on the list
• In a doubly linked list, each node contains a next-node link
and a previous-node in the sequence
• Application: traverse a list both forwards and backwards
efficiently

P data N
Doubly linked lists

• To define a structure to be used as a doubly linked list, in


addition to the data, two pointers are included: One for next
and one for previous.
struct node {
int age;
float height;
node *next;
node *prev;
};
Doubly linked lists
• Let’s create the first item of a doubly linked list using the
previous structure
Node *NODE=new node;
NODE->age=28;
NODE->height=1.72;
NODE->next=NULL;
NODE->prev=NULL:

current

28
prev 1.72 next NULL
NULL
Inserting a node to a Doubly Linked List
• Inserting node at the beginning
• Steps
1. Declare a temporary pointer and move it to current
node *temp=new node;
temp=current;

2. Navigate to the first node


while (temp->prev != NULL)
temp = temp->prev;

3. Create the item to insert and load the data fields


node *temp2=new node;
temp2->age=23;
temp2->height=1.8;
Inserting a node to a Doubly Linked List
4. Taking care of the link between the new node and
existing linked list
temp2->prev=NULL; //new start of the
list
temp2->next = temp;// Links to current
list temp->prev = temp2;
current

NULL
data data data
NULL prev prev prev
next next next
node *temp=new node;

temp=current;

temp temp temp = current

temp2 NULL
data data data

prev prev prev


NULL
next next next

temp2->prev=NULL;
temp2->next = temp;
temp->prev = temp2;
Inserting a node to a Doubly Linked List
• Inserting node at the end
• Steps
1. Declare a temporary pointer and move it to current
node *temp=new node;
temp=current;

2. Navigate to the last node


while (temp->next != NULL)
temp = temp->next;

3. Create the item to insert and load the data fields


node *temp2=new node;
temp2->age=33;
temp2->height=1.5;
Inserting a node to a Doubly Linked List
4. Taking care of the link between the new node and
existing linked list

temp2->next=NULL; //new last node


temp2->prev = temp;// Links to current
list temp->next = temp2;
node *temp=new node;

temp=current;
temp2
temp = current temp temp

data data data NULL

prev prev prev


next next next

temp2->next=NULL;
temp2->prev = temp;
temp->next = temp2;
Inserting a node to a Doubly Linked List
• Inserting after a specified node
• Steps
1. Declare a temporary pointer CR
CR = head;

2. Navigate to the specified node


for(int i=1;i<n;i++){
CR = CR->next;
}
3. Create the item to insert and load the data fields
node *ND = new node;
ND->age = 33;
ND->height = 1.5;
Inserting a node to a Doubly Linked List
4. Taking care of the link between the new node and
existing linked list

ND->next = CR->next;
CR->next->prev = ND;
CR->next = ND;
ND->prev = CR;
Deleting a node from a doubly Linked List
• Deleting a node from front

node *temp=new node;


temp=head;
head=temp->next;
temp->next->prev=NULL;
delete temp;

NULL
data data data
NULL prev prev prev
next next next
Deleting a node from a doubly Linked List
• Deleting a node from back(end)

node *CR=new node;


node *PR=new node;
CR=head;
while(CR->next!=NULL){
PR=CR;
CR=CR->next;
}
PR->next=NULL;
delete CR;
Deleting a node from a doubly Linked List
• Deleting a specified node

node *CR=new node;


CR=head;
for(int i=1;i<n;i++)
CR=CR->next;
CR->prev->next=CR->next;
CR->next->prev=CR->prev;
delete CR;

You might also like