Data Structure Lecture 5
Data Structure Lecture 5
CHAPTER FIVE
INTRODUCTION TO DATA
STRUCTURE
Lecture 5
Link list
2
Slide 1- 3
Linked List
Insertion and deletion are easier and efficient.
Linked list provides flexibility in inserting a data
item at a specified position and deletion of a data
item from the given position.
Many complex applications can be easily carried
out with linked list.
Slide 1- 4
ADVANTAGES AND
DISADVANTAGES
5
Slide 1- 9
10
Cont..
Creation operation is used to create a linked list. Once a
linked list is created with one node, insertion operation
can be used to add more elements in a node.
Insertion operation is used to insert a new node at any
Slide 1- 13
Singly Linked Lists
NODE
Single Linked list
start
10 30 40
start
Fig. 5.6. Insert a node with DATA(40) at the end
30
Fig. 5.5. Create a node with DATA(30)
start
30 40
Fig. 5.6. Insert a node with DATA(40) at the end
Slide 1- 17
Single Linked list
NODE
The link field contains address of next node and data
field contains actual data of the node
Slide 1- 18
Why linked lists?
Linked lists are used to implement many important data structures such
as stacks, queues, graphs, hash tables, etc.
Linked lists are used as components of some data structures.
Examples: trees, skip lists, etc.
LISP An important programming language in artificial intelligence
makes extensive use of linked lists in performing symbolic processing.
Memory management: An important role of operating systems. An
operating system must decide how to allocate and reclaim storage for
processes running on the system. A linked list can be used to keep
track of portions of memory that are available for allocation.
Scrolled lists, components found in graphical user interfaces, can be
implemented using linked lists.
Singly-linked lists vs. 1D-arrays
ID-array Singly-linked list
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
No memory waste if the array is full or almost Extra storage needed for references; however
full; otherwise may result in much memory uses exactly as much memory as it needs
waste.
Sequential access is faster because of greater Sequential access is slow because of low
locality of references [Reason: Elements in locality of references [Reason: Elements not in
contiguous memory locations] contiguous memory locations]
Example
#include <iostream>
using namespace std;
class node
{ public:
int data;
node* next;};
void println(node* n)
{while(n!=NULL)
{ cout<<n->data <<" ";
n=n->next; } }
int main() head->data=1;
{ head-
node* head=NULL; >next=second;
node* second=NULL; second->data=2;
node* third=NULL; second-
>next=third;
node* four=NULL;
third->data=3;
third->next=four;
head=new node();
four->data=4;
second=new node();
four->next=NULL;
third=new node();
println(head);
four=new node();
return 0; }
#include <iostream> void insertAfter(Node*
using namespace std; prev_node, int new_data)
class Node { if (prev_node == NULL)
{ public: { cout<<"the given
int data; previous node cannot be
Node *next; }; NULL";
void push(Node** head_ref, return; }
int new_data) Node* new_node = new
{ Node* new_node = new Node();
Node(); new_node->data =
new_node->data = new_data;
new_data;
new_node->next =
new_node->next =
prev_node->next;
(*head_ref);
prev_node->next =
(*head_ref) =
new_node; }
new_node; }
void append(Node** head_ref, int void printList(Node *node)
new_data)
{ while (node != NULL)
{ Node* new_node = new { cout<<" "<<node-
Node();
>data;
Node *last = *head_ref;
node = node->next; } }
new_node->data = new_data; int main()
new_node->next = NULL; { Node* head = NULL;
if (*head_ref == NULL) append(&head, 6);
{ *head_ref = push(&head, 7);
new_node; push(&head, 1);
return; } append(&head, 4);
while (last->next != NULL) insertAfter(head->next,
8);
last = last->next;
cout<<"Created Linked
last->next = new_node; list is: ";
return; } printList(head);
return 0; }
Example
#include <bits/stdc++.h> Node *prev = head;
using namespace std; while(prev->next !=
class Node NULL && prev->next !=
{ public: n)
int data; prev = prev->next;
Node *next; };
if(prev->next ==
void deleteNode(Node *head, Node *n) NULL)
{ if(head == n)
{ cout << "\
{ if(head->next == NULL)
nGiven node is not
{ cout << "There is only one node."
present in Linked
<< " The list can't be made empty ";
List";
return; }
head->data = head->next->data;
return; }
n = head->next; prev->next = prev-
head->next = head->next->next; >next->next;
free(n); free(n);
return; } return; }
void push(Node void printList(Node *head)
**head_ref, int {
new_data) while(head!=NULL)
{ Node *new_node {
= new Node(); cout<<head-
new_node->data = >data<<" ";
new_data; head=head-
>next;
new_node->next =
*head_ref;
}
cout<<endl;
*head_ref =
new_node; }
}
int main() cout<<"Given Linked List: ";
{ Node *head = NULL; printList(head);
push(&head,3); cout<<"\nDeleting node "<< head-
>next->next->data<<" ";
push(&head,2); deleteNode(head, head->next->next);
push(&head,6); cout<<"\nModified Linked List: ";
push(&head,5); printList(head);
push(&head,11); cout<<"\nDeleting first node ";
push(&head,10); deleteNode(head, head);
cout<<"\nModified Linked List: ";
push(&head,15);
printList(head);
push(&head,12); return 0; }
END
?