0% found this document useful (0 votes)
241 views9 pages

Single Linked List2

A singly linked list is a linear data structure where each node contains a data element and a pointer to the next node. Each node stores an element and a link to the next node. Variables first and last point to the first and last nodes. Operations like insertion, deletion and searching have different time complexities depending on whether the list size is stored in a variable or needs to be traversed. Common operations like insertion at the beginning or after a node have O(1) time complexity while operations requiring searching like insertion before a node have O(n) time complexity where n is the number of elements. Singly linked lists can implement stacks and queues where push/pop and enqueue/dequeue run in O(1) time respectively

Uploaded by

Rajendranbehappy
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)
241 views9 pages

Single Linked List2

A singly linked list is a linear data structure where each node contains a data element and a pointer to the next node. Each node stores an element and a link to the next node. Variables first and last point to the first and last nodes. Operations like insertion, deletion and searching have different time complexities depending on whether the list size is stored in a variable or needs to be traversed. Common operations like insertion at the beginning or after a node have O(1) time complexity while operations requiring searching like insertion before a node have O(n) time complexity where n is the number of elements. Singly linked lists can implement stacks and queues where push/pop and enqueue/dequeue run in O(1) time respectively

Uploaded by

Rajendranbehappy
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/ 9

Single Linked List

Singly Linked List


I

A singly linked list provides an implementation of List ADT.

Each node stores:


I

element (data)

link to the next node

next
element

node

Variables first and optional last point to the first/last node.

Optional variable size stores the size of the list.


first

last

element 1

element 2

element 3

element 4

Singly Linked List: size


If we store the size on the variable size, then:
size():
return size
Running time: O(1).
If we do not store the size on a variable, then:
size():
s=0
m = first
while m != do
s=s+1
m = m.next
done
return s
Running time: O(n).

Singly Linked List: insertAfter


insertAfter(n, o):
x = new node with element o
x.next = n.next
n.next = x
if n == last then last = x
size = size + 1
n

first

last

A
first

C
n

last

x
A

Running time: O(1).

C
o

Singly Linked List: insertFirst


insertFirst(o):
x = new node with element o
x.next = first
first = x
if last == then last = x
size = size + 1
first

first, x

o
A
Running time: O(1).

Singly Linked List: insertBefore


insertBefore(n, o):
if n == first then
insertFirst(o)
else
m = first
while m.next != n do
m = m.next
done
insertAfter(m, o)
(error check m != has been left out for simplicity)
We need to find the node m before n:
I

requires a search through the list

Running time: O(n) where n the number of elements in the list.


(worst case we have to search through the whole list)

Singly Linked List: Performance


Operation
size, isEmpty
first, last, after
before
replaceElement, swapElements
insertFirst, insertLast
insertAfter
insertBefore
remove
atRank, rankOf, elemAtRank
replaceAtRank
insertAtRank, removeAtRank
1
2
3

Worst case Complexity


O(1) 1
O(1) 2
O(n)
O(1)
O(1) 2
O(1)
O(n)
O(n) 3
O(n)
O(n)
O(n)

size needs O(n) if we do not store the size on a variable.


last and insertLast need O(n) if we have no variable last.
remove(n) runs in best case in O(1) if n == first.

Stack with a Singly Linked List


We can implement a stack with a singly linked list:
I

top element is stored at the first node

Each stack operation runs in O(1) time:


I

push(o):

pop():

insertFirst(o)

o = first().element
remove(first())
return o

first (top)

element 1

element 2

element 3

element 4

Queue with a Singly Linked List


We can implement a queue with a singly linked list:
I

front element is stored at the first node

rear element is stored at the last node

Each queue operation runs in O(1) time:


I

enqueue(o):

dequeue():

insertLast(o)

o = first().element
remove(first())
return o

first (top)

last (rear)

element 1

element 2

element 3

element 4

You might also like