Singly Linked Lists

advertisement
Linked Lists
Pointers
Singly Linked Lists
Dynamically Linked Stacks and Queues
Polynomials
Additional List Operations
Equivalence Relations
Sparse Matrices
Doubly Linked Lists
Generalized Lists
Pointers
•
•
Sequential representation
– Disadvantages
• Insertion & Deletion
• Varying size
Linked representation
– Singly/Doubly linked lists
Singly Linked Lists
•
Linked list
– An ordered sequence of nodes with links
– The nodes do not reside in sequential locations
– The locations of the nodes may change on different runs
sat
bat
cat
vat
null
1
•
Linked list may be represented in mmemory: Data[ ], Link[ ]
1
vat
0
2
cat
4
sat
1
bat
2
3
4
5
6
Singly Linked Lists(Cont.)
•
Insertion
bat
cat
bat
sat
cat
first
vat null
sat
mat
vat
null
Insert mat after cat
Singly Linked Lists (Cont.)
•
Deletion
first
bat
cat
mat
sat
vat
null
Delete mat from list
2
Create and Use Linked Lists in C++
.Node Class
class ListNode{
friend class List;
private:
int data; //char data[3]
ListNode *Link;
};
.List Class
class List {
public:
//List manipulation operations
void create();
void insert();
void delete();
private:
ListNode *first;
};
.Creating a two-node list
//Program 4.3, example 4.2, page 172.
.ListNode constructor
//Program 4.3, example 4.2, page 172.
.Inserting a node
//Program 4.4, figure 4.11, example 4.3, page 173.
.Deleting a node
//Program 4.5, example 4.4, page 174.
3
Implementing Linked Lists with Templates
.Integer List
List<int> intlist;
.Float List
List<float> floatlist;
.Character List
List<char> charlist;
.Template definition of linked lists
//Program 4.6, page 176.
•
Circular Lists
Circular list: the link field of the last node points to the first
//Figure 4.13, page 184.
. The empty circular lists
//Figure 4.16, page 185.
Linked Stacks and Queues
.When we need multiple stacks and queues, linked stacks and linked
queues are good solution.
top
data
link
null
(a)Linked Stack
front
rear
data
link
null
(b)Linked Queue
4
A Representation of m Stacks
.A collection of m stacks
Stack *stack = new Stack[m];
.Stack class definition and adding a node and deleting top node
//program 4.15, program 4.16, program 4.17, page 188.
Polynomials
•
Polynomial representation
coef
expon
link
.Polynomial class definition and declaration of operator+( )
//program 4.20, page 191
. Implementation of operator+( )
//program 4.21, page 193
•
Example
14
8
– a= 3x + 2x + 1
–
b= 8x14- 3x10+ 10x6
//figure 4.18: Polynomial representation, page 191.
//figure 4.19: Polynomial adding, C= a + b, page 192.
5
Additional List Operations
•
Reversing a chain
• Concatenates two chain
•
Circular Lists Representation of Polynomials
Representing polynomials
14
8
– Nonezero polynomials: 3x + 2x + 1
3 14
2
8
1
0
Sparse Matrices
.Head field: to distinguish between head node and element node(false).
.The Head node for row i is also the head node for column i.
down
head
right
next
(a) head node
down
head
row
col
right
value
(b) typical node
6
.Example:
0
12
0
0
first
0
0
-4
0
H0
11
0
0
0
0
0
0
-15
H1
H2
H3
44
0
H0
2
11
H1
1
0
12
H2
2
1
-4
3
H3
3
-15
Note: The tag field of a node is
not shown;
7
Doubly Linked Lists
.If we have a problem in which moving in either direction is often
necessary, then it is useful to have doubly linked lists.
Head node
llink
rlink
Doubly linked circular list with head
node
Insertion into a Doubly Linked Circular List
.Program 4.35, page 219.
Insert
Deletion from a Doubly Linked Circular List
.Program 4.34, figure 4.31, page 219.
Generalized Lists
•
•
•
Generalized List
– A generalized list, A, is a finite sequence of n>= 0 elements, a0,…,an-1,
where ai is either an atom or a list.
Examples
– D=()
– A= (a, (b, c))
– B= (A, A, ())
– C= (a, C)
Consequences
– lists may be shared by other lists (Example B)
– lists may be recursive(Example C)
8
Generalized Lists(Cont.)
•
•
Represent polynomial
10 3 2
8 3 2
8 2 2
4 4
3 4
– p(x,y,z)= x y z +2x y z +3x y z +x y z+6x y z+2yz
Solution I
Coef
expx
expy
expz
•
link
Solution II
– Using general list
2
– 3x y: See Figure 4.32, p233.
Summary
•
•
Lists
– Singly/Doubly linked list
– Circular
• Singly/Doubly linked list
– Linked lists with head
– Generalized linked lists
Operations
– Insertion, Deletion, Modification, Retrieval, Concatenation, etc.
9
Download