Chapter 4
Chapter 4
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
vat
null
1 2 3 4 5 6
0 4 1
Insertion
bat
bat first
cat
cat mat
sat
sat vat
vat null
null
Singly Linked Lists (Cont.) Deletion first bat cat mat sat vat null
Delete mat from list Create and Use Linked Lists in C++ .Node Class class ListNode{ friend class List; private:
2
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.
Implementing Linked Lists with Templates .Integer List List<int> intlist; .Float List List<float> floatlist;
3
.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 data link null rear
(b)Linked Queue
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.
4
Circular Lists Representation of Polynomials Representing polynomials Nonezero polynomials: 3x14 + 2x8 + 1
3 14
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 next right down head row value col right
first
H0
H1
H2
H3
44
H0
0 2 11
H1
1 0 12
H2
2 1 -4
H3
3 3
-15
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
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) Generalized Lists(Cont.) Represent polynomial p(x,y,z)= x10y3z2+2x8y3z2+3x8y2z2+x4y4z+6x3y4z+2yz Solution I Coef expx expy
expz
link
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.