L6__LinkedLists
L6__LinkedLists
1
Objectives.
2
Array versus Linked Lists
• Arrays are suitable for:
• Inserting/deleting an element at the end.
• Randomly accessing any element.
• Searching the list for a particular value.
• Linked lists are suitable for:
• Inserting an element.
• Deleting an element.
• Applications where sequential access is required.
• In situations where the number of elements cannot be
predicted beforehand.
3
List is an Abstract Data Type
• What is an abstract data type?
• It is a data type defined by the user.
• Typically more complex than simple data types like
int, float, etc.
• Why abstract?
• Because details of the implementation are hidden.
• When you do some operation on the list, say insert an
element, you just call a function.
• Details of how the list is implemented or how the
insert function is written is no longer required.
4
Linked List- Introduction
• A linked list is a data structure which can
change during execution.
• Successive elements are connected by pointers.
• Last element points to NULL.
• It can grow or shrink in size during execution of a
program.
• It can be made just as long as required.
head
• It does not waste memory space.
A B C
5
6
Linked List- Introduction
7
Basic Operations.
Insertion
Deletion
Retrieval
Traversal
8
Insertion.
Retrieval is used to get the information related to an element without changing the
structure of the list. 11
Traversal.
12
Implementation.
13
Data structure.
14
Algorithms.
1 2 3 4
5 6 7
8 9 10
struct node
{
int data;
node *next;
};
head = NULL;
16
Pointer to structures
• Like other types, structures can also be accessed through pointers
• However, there is a selection operator -> (minus sign and greater than
symbol) to eliminate the problem of pointer to structures
• The priority of selection operator (->) and member operator(.) are the
same
• The expressions :
• (*pointerName).fieldName is same as pointerName ->fieldName
• But pointerName -> fieldName is preferred
Figure 12-16 pointer selection operator
Pointers and Structures I
struct inventory
{
char name[30];
int number;
float price;
};
• For example:
p = new int;
q = new float;
24
Insert node.
Only its logical predecessor is needed.
25
Insert node.
2. Insert at beginning
3. Insert in middle
4. Insert at end
26
Insert into empty list.
27
Insert into empty list
newNode->next = head;
head = newNode
28
Insert at beginning.
node *newNode = new node; //create a
node
newNode->data = value;
newNode->next = head;
head = newNode
29
Insert at end.
Insert at the End
•Allocate memory for new node
•Store data
•Traverse to last node
•Change next of last node to recently created
node
30
Insert at the End
• Allocate memory for new node
• Store data
• Traverse to last node
• Change next of last node to recently created node
32
Insert in the Middle
• Allocate memory and store data for new node
• Traverse to node just before the required position of new node
• Change next pointers to include new node in between
35
Delete first node
36
Delete from beginning
• Point head to the second node
37
Delete general case.
38
Delete from middle
• Traverse to element before the element to be deleted
• Change next pointers to exclude the node from the chain
node *cur=head;
node *prev = cur;
for(i=2; i<=position ;i++)
{
prev = cur;
if(cur->next != NULL)
cur=cur->next;
}
prev->next= prev->next->next;
39
40
List search
41
42
43
Retrieve node.
44
Empty list.
45
Full list.
46
List count.
47
Traversal list.
48
49
50
STACK
IMPLEMENTATIONS:
USING ARRAY AND
LINKED LIST
51
STACK USING ARRAY
PUSH
top
top
52
STACK USING ARRAY
POP
top
top
53
Stack: Linked List Structure
PUSH OPERATION
top
54
Stack: Linked List Structure
POP OPERATION
top
55
QUEUE: LINKED LIST STRUCTURE
DEQUEUE
front rear
56
THE END.
57