0% found this document useful (0 votes)
30 views25 pages

Lecture 2 & 3 Single-Linked List

The document describes the abstract data type (ADT) of a linked list. Key points: - Linked lists can dynamically grow and shrink, and elements can be accessed, inserted, or deleted at any position. - Common list operations include insert, locate, retrieve, delete, and print. - Linked lists can be implemented using pointers, with each node containing an element and a pointer to the next node. A header sentinel node is often added for simplicity. - Common operations like insert, delete, and locate have time complexities of O(1) using a pointer-based linked list implementation due to direct node addressing.

Uploaded by

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

Lecture 2 & 3 Single-Linked List

The document describes the abstract data type (ADT) of a linked list. Key points: - Linked lists can dynamically grow and shrink, and elements can be accessed, inserted, or deleted at any position. - Common list operations include insert, locate, retrieve, delete, and print. - Linked lists can be implemented using pointers, with each node containing an element and a pointer to the next node. A header sentinel node is often added for simplicity. - Common operations like insert, delete, and locate have time complexities of O(1) using a pointer-based linked list implementation due to direct node addressing.

Uploaded by

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

The Abstract Data Type “Linked List"

• Lists are a particularly flexible structure because they can


• grow and shrink on demand, and
• elements can be accessed, inserted, or deleted at any position within a list.
• be concatenated together or split into sub-lists.
• Applications of List:
• information retrieval,
• programming language translation, and simulation.
• Storage management
Single Linked List
List has a set of representative set of list operations.
1. INSERT(x, p, L). Insert x at position p in list L,
2. LOCATE(x, L) returns the position of x on list L.
3. RETRIEVE(p, L) returns the element at position p on list L.
4. DELETE(p, L). Delete the element at position p of list L.
5. NEXT(p, L) and PREVIOUS(p, L) return the positions following and preceding position p
on list L.
6. END(L) returns the next of last position.
7. MAKENULL(L). causes L to become an empty list and returns position END(L).
8. FIRST(L). This function returns the first position on list L. If L is empty, the position
returned is END(L).
9. PRINTLIST(L). Print the elements of L in the order of occurrence.
Single Linked List
Example 1:
Write a statement to insert an element x after an element y in the linked list.
INSERT(x , NEXT(LOCATE(y , L),L) ,L);
Example 2:
Write a statement to insert an element x before an element y in the linked list.
INSERT(x , PREVIOUS(LOCATE(y , L) ,L) ,L);
Example 3:
Using an ADL list, write the function PURGE that takes a list as argument and eliminate duplicates from the list.
void PURGE(LIST L)
{ position p, q ;
p = FIRST(L);
while P <> End(L)
{ q := NEXT(p , L);
while q <> End(L)
if ( RETRIVE(p , L) = RETRIVE (q , L))
DELETE(q, L)
else
q:= q . NEXT(q , L);
p := NEXT(p , L);}}
ADT List
Array-Based
Implementation
Array-based Implementation

An obvious choice for implementing the List ADT is to use a fixed size array A, where A[i] stores the element at
index i.
Then we have the following structure
The structure of storage is
struct List
{
int capacity; //current array size
int * elem = new int [capacity]; //the array in which elements have been sored
int last= -1; // Last is the index of the last element in list initially list is
//empty
}
Algorithms for implementing ADT list operations using Array implementation
1. Algorithm END(L) returns the next of last position ( Defined function )

END (L ) : Pos // we will implement the function END(L) using array implementation
return L.last+1;
End
CT = O(1)
2. Algorithm FIRST(L) returns the first position on list L ( Defined function ).
FIRST (L): Pos // we will implement the function FIRST(L) using array implementation
return 0;
End
CT = O(1)
Algorithms for implementing ADT list operations using Array implementation
3. Algorithm NEXT(p , L)
▪ NEXT(p, L) returns the positions following position p on list L.
▪ If p is the last position on L, then NEXT(p, L) = END(L).
▪ NEXT is undefined if p is END(L).
Next(p , L): Pos
if p>=L.capacity -1
{ error(“p is at the end position of the list”); return -1;}
else if pos>L.last+1 or pos<0
{ error(‘pos is out of range ’);
return -1;
}
else
return p+1;
End CT = O(1)
Algorithms for implementing ADT list operations using Array implementation

4. Algorithm PREVIOUS(p, L) return the positions preceding position p on list L.


PREV predefined function is undefined if p is the first position.

Prev(p, L): Pos


if p==0
{ error(‘p is at the first position of the list’); return -1;}
else if pos>last+1 or pos<0
{error(‘pos is out of range’); return -1;}
else
return p-1;
End CT = O(1)
Algorithms for implementing ADT list operations
5. Algorithm INSERT(x, p, L). Insert x at position p in list L,
30
Insert(e,pos, L)
if last ==capacity -1
error(‘List is Full’)
else if pos>last+1 or pos<0
error(‘pos is out of range’)
else
begin
for q = last downto pos do {shift Right}
elem[q+1]←elem[q] {make room for the new element}
elem[pos]←e
last←last+1
end

End CT = O(n)
Algorithms for implementing ADT list operations
6. Algorithm DELETE(p, L). Delete the element at position p of list L.
Delete(pos, L)
if L.last == -1
error(‘List is Empty’)
else if pos>L.last or pos<0
error(‘pos is out of range’)
else
begin
for q = pos+1 to L.last do {shift Left}
L.elem[q-1]←L.elem[ q]
end
L.last--;
end CT = O(n)
Algorithms for implementing ADT list operations
7. Algorithm LOCATE(x , L). returns the position of x on list L.
If x appears more than once, then the position of the first occurrence is returned. If x does not appear at all, then END(L)
is returned.
Locate(ElementType e, List L): Pos
for q = 0 to L.last do
if elem[q]=e
return q;
return L.END(); {not found}
end
CT = O(n)
Algorithms for implementing ADT list operations
8. Algorithm RETRIEVE (p, L) returns the element at position p on list L.
RETRIEVE(int pos, list L) :ElementType
if pos>L.last or pos<0
error(‘pos is out of range’)
return -9999
else
return L.elem[pos];
End
O(1)
Algorithms for implementing ADT list operations
9. Algorithm MAKENULL(L) causes L to become an empty list and returns position END(L).
MAKENULL (L)
L.last= -1;
End O(1)
10. Algorithm PRINTLIST(L). Print the elements of L in the order of occurrence.
PRINTLIST(L )
for i=0 to L.last
print(L.elem[i])
end
End O(n)
Test ADT List
void main()
{
List L; // last = -1 last
Insert(9,First(L),L); 9

Insert(20,END(L),L); 9 20
Insert(100,First(L),L); last 100 9 20

pos p= Locate(9,L); cout<<“9 exists at pos =”<<p; last


PRINTLIST(L ); //100 9 20
Delete(Locat(9,L),L); 100 20

PRINTLIST(L ); // 100 20
Delete(Locat(9,L),L); // pos is out of range
PRINTLIST(L ); // 100 20
}
ADT List
Pointer-Based
Linked-List based
Implementation
Single Linked List
The Single linked list is a way to store a collection of elements. Each element in the single linked list
is stored in the form of a node. The node is a collection of two sub-elements or parts:
1. The data part that stores the element
2. The next part that stores the address of next node.

The Single linked list is formed when many such nodes are linked together to form a chain. Each
node points to the next node present in the order. The first node is always used as a reference to
traverse the list and is called head.
Declaring the single linked list using pointer implementation:
struct node{
eType element;
node* next;
node( ) Header Sentinel
{ next = Null;
▪To simplify programming, it is convenient to add special
}
nodes at front of a linked list: a header node just before the
};
Algorithm create-dummy-head(node * head)
head of the list.
{
// creat dummy head cell ▪This “dummy” or sentinel node do not store any elements.
head= new node;
typedef node * position; ▪It provide quick access to the first of the list.
} ▪In particular, the header’s next pointer points to the first
node of the list.
Creating a linked list by inserting from the back

head temp
Algorithm Create(node * head)
Begin
node * temp = new node( ) We create node *temp2. Transfer the address of
node * temp2 = head; *head to *temp2. So *temp2 is also pointed at the
while (temp2->next != NULL)
temp2 = temp2->next;
front of the linked list. We then traverse the list
temp2->next = temp; CT = O(n)
End

head
Algorithm Insert an element at position p

p is the address of the node that we insert after it


Insert (etype x; node *p);
Begin
node *temp = new node( );
temp −> element := x;
temp −> next=p −> next
p −>next := temp ;

end; CT=O(1)
Deleting an element at position p
p is the address of the node we want to delete after it

Procedure Delete (p: position);


begin
p^. next := p^. next^. next
end; {Delete} CT=O(1)
Locate function

CT=O(n)
Retrieve. Returns the element at position p
on list.

x null

function Retrieve (p : position) : etype


{
return (p ^. element);

}
CT=O(1)
Empty(p, L) .
Make List empty.

Function empty(node*& head)


{
head->next=NULL;
}

display(L) .
Display list L
Function display(node* head){
position p = head;
while(p->next!=NULL)
{
etype x=p->next->element;
cout<<x ;
endl;
p=p->next;}} CT=O(n)
ADT linked list
Delete last element

Insert b after x

Swap the first and the last


elements of a linked list.

Append()
Write an Append() function that takes two lists, 'a' and 'b',
appends 'b' onto the end of 'a'

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy