Chapter 4 Teacher
Chapter 4 Teacher
Linked List
1
Types of Data Structures
2
Static Data Structures
Are data structures that are defined &
allocated before execution, thus the
size cannot be changed during time of
execution.
Example:
Array implementation of ADTs.
3
Dynamic Data Structure
Are data structure that can grow and
shrink in size or permits discarding of
unwanted memory during execution
time.
Example:
Linked list implementation of ADTs.
4
Structure
Is a collection of data items and the data items
can be of different data type.
struct student {
char name[20];
int age;
char Dept[20];
};
Example:
struct student stud;
struct student *studptr;
cout<<stud.name;
OR
cout<<studptr->name; 7
Self-Referential Structures
struct student{
char name[20];
int age;
char Dept[20];
struct student *next;
};
8
Linked List
Is self-referential structure.
data pointer
A B C
Start
list).
• This linked list has four nodes in it, each with
a link to the next node in the series (in the
linked list).
11
Variations of Linked Lists
Single linked lists: Discussed in the
previous two slides.
Circular linked lists:
▪ The last node points to the first node of the list.
A B C
Start
▪ How do we know when we have finished traversing the list? (Hint:
check if the pointer of the current node is equal to the Start (head)
pointer).
12
Doubly linked lists
• Each node points not only to Successor node (Next node), but
also to Predecessor node (Previous node).
• There are two NULL: at the first and last nodes in the linked
list.
A B C
13
Steps
1. Allocate a new node.
21
Inserting at the End
Steps
1. Allocate a new node.
2. Set the node data values and make the
next pointer of the new node point to
NULL.
3. Make old last node’s next pointer point to
the new node.
4. Update end to point to the new node.
22
Insertion in the middle
Steps:
Create a new Node
23
Exercise Single linked list
Write the function for add a new node
at the begging.
at the middle
at the end
Write a function that will be delete node
from the begging
from the end
from the middle by searching
Write the function of that displays the contents of each nodes.
Write a function search an element from the linked list.
Write a function to sort linked list elements by one of the
member of the node.
24
Quiz
Write the function for add a new node at
the begging.
25