0% found this document useful (0 votes)
28 views29 pages

DS 7

The document discusses linked lists as an alternative data structure to arrays for storing ordered lists. It describes how linked lists allow for more efficient insertion and deletion of elements by avoiding excessive data movement. It provides examples of inserting and deleting nodes from linked lists and covers related operations like searching, displaying the list, and creating ordered linked lists.

Uploaded by

Ahmad Bajwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views29 pages

DS 7

The document discusses linked lists as an alternative data structure to arrays for storing ordered lists. It describes how linked lists allow for more efficient insertion and deletion of elements by avoiding excessive data movement. It provides examples of inserting and deleting nodes from linked lists and covers related operations like searching, displaying the list, and creating ordered linked lists.

Uploaded by

Ahmad Bajwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structures & Algorithms

Syed Ali Raza


Lecture 7
Introduction
• Weakness of storing an ordered list in array:
– Insertion and deletion of arbitrary elements are
expensive.
• Example:
– Given an array which is arranged in ascending order.

2 4 6 7

– Discuss how to insert a new element ‘1’ and how to


delete the element ‘4’.
– Storage allocation is not flexible.
Possible Improvements

• The elements in an ordered list don’t need


to be stored in consecutive memory
space.
• The insertion and deletion of an element
will not induce excessive data movement.
• The element can be “dynamically”
allocated.
Linked Representation

• Data structure for a linked list:

head

Node

•Data
•Link (pointer): used to store the address of the next node.
Linked Representation

head
8 BAT 3 CAT 4 FAT 0 0

1
2
3 CAT 4
4 FAT 0
5
6
head 7
8 8 BAT 3
9
Insertion

•headInsert EAT into an ordered linked list


8 BAT 3 CAT 4 FAT 0 0
CAT 6

EAT EAT EAT


EAT
EAT 4
1
Find the position where EAT is
2 to be inserted.
3 CAT 4 1) Get a new node a
3 CAT 6
4 FAT 0 2) Set the data field of a to EAT.
5 3) Set the link field of a to point
6 the node after CAT, which
6
head 7 66 EAT contains FAT.
EAT 4
4) Set the link field of the node
8 8 BAT 3 containing CAT to a.
9
Deletion

• Remove CAT from linked list


head
8 BAT 3 CAT 6 EAT 4 FAT 0
BAT 6

1
2 Find the address of CAT
3 CAT 6 1) Set the link of BAT to EAT.
3
4 FAT 0 2) Deallocate CAT
5
6 EAT 4
head 7
8 8 BAT 3
8 BAT 6
9
Linked List Creation

struct listNode
{
int data; head
listNode* Link;
};

data link
//in main()
struct listNode *head = new
listNode;
Inserting First Element

void firstlistNode(struct listNode *tempNode,int n)


{
tempNode->data = n;
tempNode->Link =NULL;
}
Inserting Elements

void insert(struct listNode *tempNode, int n)


{
listNode *newlistNode = new listNode;
newlistNode->data = n;
newlistNode->Link = NULL;

while(tempNode)
{
if(tempNode->Link == NULL)
{
tempNode->Link = newlistNode;
return;
}
tempNode = tempNode->Link;
}
}
Adding an Item before another Item?

• Need to search list from beginning to find


previous item
• Add new item after previous item
Searching Element

struct listNode *searchlistNode(struct listNode *tempNode, int n)


{
while(tempNode)
{
if(tempNode->data == n)
return tempNode;
tempNode = tempNode->Link;
}
cout << n << " is not in the list.\n"<<endl;
}
Deleting Element

bool deletelistNode(struct listNode **tempNode, listNode *ptrDel) {


listNode *currNode = *tempNode;
if(ptrDel == *tempNode) {
*tempNode = currNode->Link;
delete ptrDel;
return true;
}
while(currNode) {
if(currNode->Link == ptrDel) {
currNode->Link = ptrDel->Link;
delete ptrDel;
return true;
}
currNode = currNode->Link;
}
return false;
}
Desplaying List

void display(struct listNode *tempNode)


{
while(tempNode)
{
cout << tempNode->data << " ";
tempNode = tempNode->Link;
}
cout <<endl;
}
Ordered List

void InsertOrderedNode(struct listNode *tempNode)


{
listNode *currNode = *tempNode;
while (currNode != NULL)
{
if (currNode->data >= value)
{
ListNode *a = new ListNode(value);
a->link = currNode;
previous->link = a;
break;
}
previous = currNode;
currNode = currNode->link;
}
}
Insertion
• Insert EAT into an ordered linked list
first previous previous

8 BAT 3 CAT 4
6 FAT 0

curr curr curr

a EAT 4

listNode *currNode = *tempNode;


while (currNode != NULL)
{
if (currNode->data >= value)
{
ListNode *a = new ListNode(value);
a->link = currNode;
previous->link = a;
break;
}
previous = currNode;
currNode = currNode->link;
}
}
Problem of Insert

• The function Insert() fails to deal with


boundary conditions.
– The insertion is always performed between
two existing nodes.
• Improvements
– Add codes before- and after the while-
statement for dealing with the boundary
conditions.
– Always maintain two (dummy) nodes so that
insertion can always be performed between
two nodes.
Doubly Linked Lists

• Each node points to both its predecessor


and its successor
• Circular doubly linked list
– precede pointer of the dummy head node
points to the last node
– next reference of the last node points to the
dummy head node
– No special cases for insertions and deletions
Doubly Linked Lists

(a) A circular doubly linked list with a dummy head node


(b) An empty list with a dummy head node
Doubly Linked Lists

• Typical operations
• Initialize the list
• Destroy the list
• Determine if list empty
• Search list for a given item
• Insert an item
• Delete an item, and so on
Insertion

• Insert a node
– Four cases
• Case 1: Insertion in an empty list
• Case 2: Insertion at the beginning of a nonempty
list
• Case 3: Insertion at the end of a nonempty list
• Case 4: Insertion somewhere in a nonempty list
– Cases 1 and 2 requirement: Change value of
the pointer first
– Cases 3 and 4: After inserting an item, count
incremented by one
Deletion

• Delete a node
• Four cases
– Case 1: The list is empty
– Case 2: The item to be deleted is in the first node of
the list, which would require us to change the value of
the pointer first
– Case 3: The item to be deleted is somewhere in the
list
– Case 4: The item to be deleted is not in the list
Stacks & Queue Revisit
Linked Stack

data link

top Pop
B
Pop
Push D

D
A

E 0
Linked Queue

• Deletion takes place at front; insertion


at rear.
front rear

B C D A E 0

Pop
Pop
Push E
Summary

• Not massive amounts of data


– Linear search is okay
• Sorting not necessary
– or sometimes not possible
• Need to add and delete data “on the fly”
– Even from middle of list
• Items often need to be added to or deleted
from the “ends”

You might also like