11 - Recursive Data Structures: Linked Lists: COL 100 - Introduction To Computer Science
11 - Recursive Data Structures: Linked Lists: COL 100 - Introduction To Computer Science
Linked Lists
COL 100 - Introduction to Computer Science
II Semester 2014-2015
Department of Computer Science and Engineering
Indian Institute of Technology Delhi
Stack Functions
Top of Stack
d
c
b
a
3
2
1
0
Top of Stack
Top of Stack
3
2
1
0
April 2015
d
c
b
a
x
d
c
b
a
4
3
PUSH (x) 2
1
0
Top of Stack
POP
2
1
0
c
b
a
3
April 2015
class Stack {
private:
int A[100];
int top;
public:
Stack ( ) {top = -1;}
void push (int x) {
A [++top] = x;
}
int pop () {
return A [top--];
}
bool empty () {
return top == -1;
}
};
4
Error Conditions
Check for Stack
overflow and underflow
pop() does not really
remove the element
only moves top
class Stack {
private:
int A[100];
int top;
public: // ...
void push (int x) {
if (top >= 99)
cout << Overflow;
else
A [++top] = x;
}
int pop () {
if (top == -1)
cout << Underflow;
else
return A [top--];
} // ...
};
5
Memory Allocation
Stack size was statically declared to be 100
Actual size not known at compile-time
depends on run-time data
April 2015
Maintaining 2 Stacks
What if we wanted to maintain
2 stacks A and B in a given
memory space?
Stack A can grow to maximum
6 elements
but more space may be available
because B is not full
April 2015
Bottom of
0
Stack A
1
2
3
Top of
4
Stack A
5
Bottom of
6
Stack B
7
Top of
8
Stack B
9
10
11
Free Space
Memory
7
0
1
2
3
4
5
6
7
8
9
10
11
Data Next
-1
0
1
2
3
4
-1
6
7
5
9
Memory
Bottom of
Stack A
Bottom of
Stack B
Top of
Stack B
Top of
Stack A
Free Space
8
0
1
2
3
4
5
6
7
8
9
10
11
Data Next
-1
0
1
2
3
4
-1
6
7
5
9
Memory
Bottom of
Stack A
Bottom of
Stack B
Top of
Stack B
Top of
Stack A
Free Space
9
10
April 2015
11
Earlier Program
We dont have an
array any more
Elements returned by
new not necessarily
next to each other
April 2015
class Stack {
private:
int A[100];
int top;
public: // ...
void push (int x) {
if (top >= 99)
cout << Overflow;
else
A [++top] = x;
}
int pop () {
if (top == -1)
cout << Underflow;
else
return A [top--];
} // ...
};
12
April 2015
class StackElem {
public:
int data;
StackElem *next;
};
class Stack {
private:
StackElem *top;
public:
Stack () {top = 0;}
// ...
};
13
Dynamic Memory
Allocation
Memory allocated in
push
If new fails to find
available memory, it
returns 0
x filled into newly
allocated StackElem
Element next to p is 0
April 2015
14
Initialising and
Growing the Stack
Stack()
top = 0
push(6)
new returns 100
100
6
0
top = 100
push(7)
new returns 150
100
6
0
150
7
top = 150
April 2015
100
Initialising and
Growing the Stack
push(25)
new returns 250
100
150
100
250
25
150
top = 250
April 2015
Shrinking the
Stack
pop() removes the top
element from the stack
and returns the data
if stack is empty, report
error
April 2015
17
delete an element
also, delete element at a particular position
18
data next
9
data next
6
0 or NULL
data next
2
data next
9
data next
9
data next
6
April 2015
data next
6
19
(C) P. R. Panda, IIT Delhi
April 2015
data next
2
class Node {
int data;
Node *next;
};
class List {
private:
Node *first;
public:
List () {first = 0;}
};
20
data next
2
class Node {
int data;
Node *next;
};
Could we have:
April 2015
class Node {
int data;
Node next;
};
??
21
Insert element x
Insert element x at a given position
Delete element x
Delete element at a given position
Search for element x
April 2015
22
element is found, or
end of list is reached
data next
2
data next
9
search (9)
1st iter:
p=
p->data = 2
2nd iter:
p=
p->data = 9
FOUND. Return true
April 2015
data next
6
data next
9
search (10)
1st iter:
p=
p->data = 2
2nd iter:
p=
p->data = 9
3rd iter:
p=
p->data = 6
p becomes 0
End of Loop. NOT FOUND
April 2015
data next
6
data next
9
insert (2)
p=
fill 2 in p->data
April 2015
data next
6
data next
?
data next
2
25
data next
9
data next
6
insert (list, 2)
data next
p=
2
?
p->data = 2
p p->next = first
first
data next
data next
data next
2
9
6
after insert (2):
data next
2
first
April 2015
data next
9
data next
6
first
data next
9
data next
6
data next
2
Corner cases: What if pos pointed to the last/first node?
April 2015
27
data next
2
data next
6
remove (2)
April 2015
28
Rearrange pointers
NEXT of previous Node is NEXT
node
data next
2
p
data next
6
remove (2)
April 2015
29
data next
2
p
data next
6
remove (2)
April 2015
Rearrange pointers
NEXT of pos is NEXT of NEXT
of pos
data next
2
data next
6
remove (pos)
(C) P. R. Panda, IIT Delhi
31
data next
2
data next
6
remove (pos)
(C) P. R. Panda, IIT Delhi
TIME?
Insert element x
Insert element x at a given position
Delete element x
Delete element at a given position
Search for element x
April 2015
33
0
2
fixed/variable
Time?
insert
delete
search
April 2015
first
Array
1
9
2
6
Linked List
data next
2
data next
9
data next
6
34
April 2015
data next
2
data next
9
data next
6
35
data
2
prev
April 2015
next
data
9
next
prev
data
6
next
prev
36
p
data
2
q
next
prev
class NodeD {
int data;
Node *next;
Node *prev;
};
April 2015
data
9
r
next
prev
data
6
next
prev
37
first
first
data next
2
data
2
prev
data next
9
next
data
9
data next
6
next
prev
(C) P. R. Panda, IIT Delhi
data
6
next
prev
38
April 2015
39
10
April 2015
15
10
20
15
20
40
Merging two
Sorted Lists
1. Special Cases
1.
empty lists
April 2015
41
Principle of CACHE
Memory requests in Processor
File requests to disk/server
Web page requests to web server
April 2015
42
LRU Illustration
New
Request
7
LRU
5
9
9
7
April 2015
43
New
Request
On Access:
5
9
If Present
re-arrange List: bring to Head
If Absent
replace LRU element (tail)
bring to Head
April 2015
LRU
44
first
last
5
9
9
7
April 2015
45