Linked List
 More on Pointers and Linked Lists
Review of Sequential
Representations
 Previously introduced data structures, including
array, queue, and stack, they all have the property
that successive nodes of data object were stored a
fixed distance apart.
 The drawback of sequential mapping for ordered lists
is that operations such as insertion and deletion
become expensive.
 Also sequential representation tends to have less
space efficiency when handling multiple various sizes
of ordered lists.
Linked List
 A better solutions to resolve the aforementioned
issues of sequential representations is linked lists.
 Elements in a linked list are not stored in sequential
in memory. Instead, they are stored all over the
memory. They form a list by recording the address of
next element for each element in the list. Therefore,
the list is linked together.
 A linked list has a head pointer that points to the first
element of the list.
 By following the links, you can traverse the linked list
and visit each element in the list one by one.
Nodes and Linked Lists
 A linked list is a list that can grow and shrink while
the program is running
 A linked list is constructed using pointers
 A linked list often consists of classes that contain a
pointer variable connecting them to other dynamic
variables.
 A linked list can be visualized as items, drawn as
boxes, connected to other items by arrows
1
0
15.1
12 14 en
d
first
Nodes
 The boxes in the previous drawing represent
the nodes of a linked list
 Nodes contain the data item(s) and a pointer that
can point to another node of the same type

The pointers point to the entire node, not an individual
item that might be in the node
 The arrows in the drawing represent pointers
The head of a List
 The box labeled first, in figure , is not a
node, but a pointer variable that points
to a node
 Pointer variable first is declared as:
Node *first;
NULL
 The defined constant NULL is used as…
 An end marker for a linked list

A program can step through a list of nodes by following
the pointers, but when it finds a node containing NULL,
it knows it has come to the end of the list
 The value of a pointer that has nothing to point to
 The value of NULL is 0
 Any pointer can be assigned the value NULL:
int *ptr = NULL;
Designing a List in C++
 Use of two classes. Create a class that represents
the linked list. The class contains the items of another
objects of another class.
Linked Lists
 A linked list is a list of nodes in which each
node has one or more member variable and a
pointer that “points” to the next node in the
list
 The first node is called the head
 The pointer variable first, points to the first node

The pointer named first is not the head of the list…it
points to the head of the list
 The last node contains a pointer set to NULL
Friend Classes
class Chain; // forward declaration
Class Node {
friend class Chain;
private:
int info;
Node * link;
};
Class Chain {
public:
// Chain Manipulation operations
· · ·
private:
Node *first;
};
Function head_insert
 It would be better to create a function to insert
nodes at the head of a list, such as:
 void head_insert(int val);

val is the value to be stored in the list
 head_insert will create a new node for the val

The val will be copied to the new node

The new node will be inserted in the list as the new
head node
Pseudocode for head_insert
 Create a new dynamic variable pointed to by
temp.
 Place the value in data part of the new node
 Make temp's link variable point to the head
node
 Make the first pointer point to temp.
Translating head_insert to C++
 The pseudocode for head_insert can be
written as
pseudocode:
 Node *temp; //create the temporary pointer
temp = new Node; // create the new node
 temp->info = number; //copy the number
 temp->link = first; //new node points to first node
first = temp; // head points to new
// first node
An Empty List
 A list with nothing in it is called an empty list
 The first pointer of an empty list is NULL
first = NULL;
 Any functions written to manipulate a linked list
should check to see if it works on the empty list
 Initialize first with NULL in constructor of
Chain class.
Traversing a Linked List
 To design a function that will traverse
particular node in a linked list:
 We want the function to display all data in the link
list so we can need to use a temporary pointer that
can be moved forward.
 First pointer should not be used to traverse the link
list as when it is moved forward the result will be
memory leaks.
 This declaration will work:
void display();
Pseudocode for traversal
1. Make pointer variable temp point to the
head node
2. while(temp is not null)
{
display data in the node
make temp point to the next node
}
Moving Through the List
 The pseudocode for search requires the
pointer temp to move through the list
 How does temp follow the pointers from node to
node?
 When temp points to a node, temp->link is the
address of the next node
 To make temp point to the next node, make the
assignment:
temp = temp->link;
Translating display to C++
 The pseudocode for display can be written in
C++ using these lines in place of the lines of
pseudocode:
 Node *temp; //create the temporary pointer
temp = first; // initialize it with head node
 while (temp!= NULL)
{
cout << temp->info;
temp = temp->link // temp points to next node }
Searching a Linked List
 To design a function that will locate a
particular
node in a linked list:
 We want the function to return a pointer to the
node so we can use the data if we find it, else
return NULL
 The data we wish to find is the argument
 This declaration will work:
Node* search(int key);
Pseudocode for search
1. Make pointer variable temp point to the head
node
2. While(temp does not point to a node
containing key AND temp does not point to
the last node)
{
make temp point to the next node
}
3. If (temp points to a node containing the key)
return temp;
else
return NULL;
A Refinement of search
 The search function can be refined in this way:
temp = first;
while(temp->info!= key && temp->link != NULL)
{
temp = temp->link;
}
if (temp->info = = key)
return temp;
else
return NULL;
Check for last node
Searching an Empty List
 Our search algorithm has a problem
 If the list is empty, here equals NULL before the
while loop so…

temp->data is undefined

temp->link is undefined
 So do search if first is not equal to NULL.
Deleting a Node From Head
 To delete a node from a linked list
 Store the address of node to be deleted in a
temporary pointer temp.
 Assign the address of the second node to first
pointer
 The node is removed from the list, but is still in
memory
 Return *temp to the freestore: delete temp;
Pseudocode for Deletion
1. Make pointer variable temp point to the
head node
2. Make head point to the second node
3. De-allocate memory for the first node.
Translating head_del to C++
 The pseudocode for head_del can be written
in C++ using these lines in place of the lines
of
pseudocode:
 Node *temp; //create the temporary pointer
temp = first;
 first = first ->link;
 delete temp;
Translating head_del to C++
 The pseudocode for head_del can be written
in C++ using these lines in place of the lines
of
pseudocode:
 Node *temp; //create the temporary pointer
temp = first;
 first = first ->link;
 delete temp;
1
0
12 14 en
d
first
Temp
Insert at end
Inserts an element at the end of the list
void insertend(int v)
{ Node *temp = first;
while (temp -> link != NULL)
temp = temp->link;
Node *newer = new Node;
newer->info = v;
newer->link = NULL;
temp->link = newer;
}
1
0
12 14 ×head
newer
16 ×
temp
Delete a Node after a Node
Delete an element after the specific node
Node *t = Search(key); delafter(t);
void delafter(Node *temp)
{ Node *temp1 = temp->link;
int x = temp->data;
temp = temp->link;
delete temp;
}
1
0
12 14 ×first 16 ×
temp temp1
Header Node
 Header node can contain global nodes that
can contain global information about a list like
 Length of the list.
 Pointer to the last node.
Like
struct charstr
{
int length;
Node *first;
}s,s1;
Assignment
• Merge two lists.
• Search a number from list and delete it.
• Delete all occurrences of a number from the list.

Link list part 1

  • 1.
    Linked List  Moreon Pointers and Linked Lists
  • 2.
    Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive nodes of data object were stored a fixed distance apart.  The drawback of sequential mapping for ordered lists is that operations such as insertion and deletion become expensive.  Also sequential representation tends to have less space efficiency when handling multiple various sizes of ordered lists.
  • 3.
    Linked List  Abetter solutions to resolve the aforementioned issues of sequential representations is linked lists.  Elements in a linked list are not stored in sequential in memory. Instead, they are stored all over the memory. They form a list by recording the address of next element for each element in the list. Therefore, the list is linked together.  A linked list has a head pointer that points to the first element of the list.  By following the links, you can traverse the linked list and visit each element in the list one by one.
  • 4.
    Nodes and LinkedLists  A linked list is a list that can grow and shrink while the program is running  A linked list is constructed using pointers  A linked list often consists of classes that contain a pointer variable connecting them to other dynamic variables.  A linked list can be visualized as items, drawn as boxes, connected to other items by arrows 1 0 15.1 12 14 en d first
  • 5.
    Nodes  The boxesin the previous drawing represent the nodes of a linked list  Nodes contain the data item(s) and a pointer that can point to another node of the same type  The pointers point to the entire node, not an individual item that might be in the node  The arrows in the drawing represent pointers
  • 6.
    The head ofa List  The box labeled first, in figure , is not a node, but a pointer variable that points to a node  Pointer variable first is declared as: Node *first;
  • 7.
    NULL  The definedconstant NULL is used as…  An end marker for a linked list  A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list  The value of a pointer that has nothing to point to  The value of NULL is 0  Any pointer can be assigned the value NULL: int *ptr = NULL;
  • 8.
    Designing a Listin C++  Use of two classes. Create a class that represents the linked list. The class contains the items of another objects of another class.
  • 9.
    Linked Lists  Alinked list is a list of nodes in which each node has one or more member variable and a pointer that “points” to the next node in the list  The first node is called the head  The pointer variable first, points to the first node  The pointer named first is not the head of the list…it points to the head of the list  The last node contains a pointer set to NULL
  • 10.
    Friend Classes class Chain;// forward declaration Class Node { friend class Chain; private: int info; Node * link; }; Class Chain { public: // Chain Manipulation operations · · · private: Node *first; };
  • 11.
    Function head_insert  Itwould be better to create a function to insert nodes at the head of a list, such as:  void head_insert(int val);  val is the value to be stored in the list  head_insert will create a new node for the val  The val will be copied to the new node  The new node will be inserted in the list as the new head node
  • 12.
    Pseudocode for head_insert Create a new dynamic variable pointed to by temp.  Place the value in data part of the new node  Make temp's link variable point to the head node  Make the first pointer point to temp.
  • 13.
    Translating head_insert toC++  The pseudocode for head_insert can be written as pseudocode:  Node *temp; //create the temporary pointer temp = new Node; // create the new node  temp->info = number; //copy the number  temp->link = first; //new node points to first node first = temp; // head points to new // first node
  • 14.
    An Empty List A list with nothing in it is called an empty list  The first pointer of an empty list is NULL first = NULL;  Any functions written to manipulate a linked list should check to see if it works on the empty list  Initialize first with NULL in constructor of Chain class.
  • 15.
    Traversing a LinkedList  To design a function that will traverse particular node in a linked list:  We want the function to display all data in the link list so we can need to use a temporary pointer that can be moved forward.  First pointer should not be used to traverse the link list as when it is moved forward the result will be memory leaks.  This declaration will work: void display();
  • 16.
    Pseudocode for traversal 1.Make pointer variable temp point to the head node 2. while(temp is not null) { display data in the node make temp point to the next node }
  • 17.
    Moving Through theList  The pseudocode for search requires the pointer temp to move through the list  How does temp follow the pointers from node to node?  When temp points to a node, temp->link is the address of the next node  To make temp point to the next node, make the assignment: temp = temp->link;
  • 18.
    Translating display toC++  The pseudocode for display can be written in C++ using these lines in place of the lines of pseudocode:  Node *temp; //create the temporary pointer temp = first; // initialize it with head node  while (temp!= NULL) { cout << temp->info; temp = temp->link // temp points to next node }
  • 19.
    Searching a LinkedList  To design a function that will locate a particular node in a linked list:  We want the function to return a pointer to the node so we can use the data if we find it, else return NULL  The data we wish to find is the argument  This declaration will work: Node* search(int key);
  • 20.
    Pseudocode for search 1.Make pointer variable temp point to the head node 2. While(temp does not point to a node containing key AND temp does not point to the last node) { make temp point to the next node } 3. If (temp points to a node containing the key) return temp; else return NULL;
  • 21.
    A Refinement ofsearch  The search function can be refined in this way: temp = first; while(temp->info!= key && temp->link != NULL) { temp = temp->link; } if (temp->info = = key) return temp; else return NULL; Check for last node
  • 22.
    Searching an EmptyList  Our search algorithm has a problem  If the list is empty, here equals NULL before the while loop so…  temp->data is undefined  temp->link is undefined  So do search if first is not equal to NULL.
  • 23.
    Deleting a NodeFrom Head  To delete a node from a linked list  Store the address of node to be deleted in a temporary pointer temp.  Assign the address of the second node to first pointer  The node is removed from the list, but is still in memory  Return *temp to the freestore: delete temp;
  • 24.
    Pseudocode for Deletion 1.Make pointer variable temp point to the head node 2. Make head point to the second node 3. De-allocate memory for the first node.
  • 25.
    Translating head_del toC++  The pseudocode for head_del can be written in C++ using these lines in place of the lines of pseudocode:  Node *temp; //create the temporary pointer temp = first;  first = first ->link;  delete temp;
  • 26.
    Translating head_del toC++  The pseudocode for head_del can be written in C++ using these lines in place of the lines of pseudocode:  Node *temp; //create the temporary pointer temp = first;  first = first ->link;  delete temp; 1 0 12 14 en d first Temp
  • 27.
    Insert at end Insertsan element at the end of the list void insertend(int v) { Node *temp = first; while (temp -> link != NULL) temp = temp->link; Node *newer = new Node; newer->info = v; newer->link = NULL; temp->link = newer; } 1 0 12 14 ×head newer 16 × temp
  • 28.
    Delete a Nodeafter a Node Delete an element after the specific node Node *t = Search(key); delafter(t); void delafter(Node *temp) { Node *temp1 = temp->link; int x = temp->data; temp = temp->link; delete temp; } 1 0 12 14 ×first 16 × temp temp1
  • 29.
    Header Node  Headernode can contain global nodes that can contain global information about a list like  Length of the list.  Pointer to the last node. Like struct charstr { int length; Node *first; }s,s1;
  • 30.
    Assignment • Merge twolists. • Search a number from list and delete it. • Delete all occurrences of a number from the list.