Download Procedure to create a linked list with two nodes of type list - E

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Centrality wikipedia , lookup

Signal-flow graph wikipedia , lookup

Transcript
STUDY MATERIAL
COURSE : II B.Sc COMPUTER SCIENCE
SEMESTER: III
SUBJECT :
DATA STRUCTURES AND ALGORITHMS
PORTION :
UNIT III
STAFF
I.GOBI
:
SYLLABUS:
Linked list – Singly linked lists – Linked stacks and queues – The storage pool –
applications - Polynomial addition, Equivalence relations, Sparse matrices.
LINKED LISTS:
The representation of simple data structures using an array and sequential
mapping has the property that successive nodes of the data object are stored at fixed
distance apart. When a sequential mapping is used for ordered lists, operations such as
insertion and deletion of arbitrary elements become expensive. Consider the following
list containing
(2, 4, 6, 10,12, 14).
To complete the list naturally we want to add 8 to the list. The insertion will
require us to move elements already in the list either one location lower or higher. We
must either move 10, 12, 14 or else move 2, 4, 6. If we have to do many insertions into
the middle, then neither alternative is attractive because of the amount of data
movement. On the other hand, suppose we decide to remove many elements we have
to move many elements so as to maintain the sequential representation of the list.
When we have several ordered lists of varying sizes, sequential representation
again proved to be inadequate. By storing each list in a different array of maximum
size, storage may be wasted. By maintaining the lists in a single array a potentially
large amount of data movement is needed. Solution to this problem of data movement
in sequential representation is achieved by using linked representations.
UNIT III
1
DSA
The use of pointers or links to refer to elements of data structure implies that
elements which are logically adjacent need not be physically adjacent in memory.
This type of allocation is called linked allocation. A list has been defined to
consist of an ordered set of elements which may vary in number. A simple way to
represent a linear list to expand each node to contain a link or pointer to the next
node. This representation is called a one way chain or singly linked list. In general
a node is collection of data, data1, data2, … data n and links link1, link2 … link
n. Each item in the node is called as a field. A field contains either a data item or
a link. The link address of nill in the last node signals the end of the list. Nill is
not an address of any possible node but is a special value. If a list does not contain
any node then it is called an empty list.
4
2
...
6
2
14
nil
It is easy to make arbitrary insertions and deletions using linked list rather than
a sequential list. To insert 8 between 6 and 10 get a new node and set its data field to
8. Create a link from 6 to 8 and from 8 to 10. It is illustrated in the following figure.
2
4
10. . .
6
2
12
nil
8
To delete the element 6 from the list remove the existing link between 4 and 6
and create a new link between 4 and 8. It is illustrated in the following figure.
UNIT III
2
8. . .
DSA
10
4
2
nil
6
2
Pointer data type:
T represents pointer to the data type T.
Consider
Type student = record
Rollno : integer;
Name : integer;
End;
Var Ram : student;
declares the data object named Ram of type student. Ram is the name of the object
which directly refers to its both components.
Consider
Type p =  student;
Var s : p;
The type p is a pointer that points to the unnamed object student. The variable s is
of type p. The value of the variable s will be an address in memory at which the
object of type student created by us. Since no object exists currently, the value of s is
said to be nil (reserve word), a new object of type student is created by executing the
procedure call new(s).
s.rollno = 200
s.name = ‘ram’
object with name s
type student
object of type student
no name for the object
Pointing to
200
=
ram
s
s serves as the name of the object. After the object s has served its purpose, we can
remove it from memory and release its location by using the procedure call dispose(s).
If a linked list is consisting of nodes which have data field of type integer and link
field, then the following type declaration is used.
Type pointer =  listnode;
listnode = record
data : integer;
UNIT III
3
DSA
link : pointer;
end;
Consider the declaration
Var f : pointer;
The fields of a node pointed by f may be referenced in the following way :
f.data and f.link.
f
f.data
f. link
Procedure to create a linked list with two nodes of type list node:
The data field of the first node is set to 10 and that of the second to 20. first is a
pointer to the first node.
first
10
20
nil
0
procedure create (var : first : pointer );
var second : pointer;
begin
new(first);
new(second);
first.link :=second; { link first node to the second }
second.link := nil; {last node}
first.data := 10;
{ set data of the first node }
second.data := 20; {set data of the second node }
end; {of create}
procedure for inserting a node into a linked list
Example: Let first be a pointer to a linked list. First = nil if the list is empty. Let x be
a pointer to some arbitrary node in the list. The following procedure inserts a new
node with data field 50 following the node pointed by x. The resulting list structures
for the two cases first = nil and first <> nil are :
UNIT III
4
DSA
first
5
2
first
x
4
6
2
10
...
12
6
...
8
y
t
procedure insert (var first : pointer; x : pointer);
Var t : pointer;
Begin
new(t); {get a new node }
t.data : = 50;
if first = nil then begin { insert into empty list }
first := t;
t.link : = nil;
end
else begin {insert after x }
t.link := x.link;
x.link := t;
end;
end;
Procedure to delete an element from the list:
Let first and x be as in the previous example. Let y point to the node that precedes
x and let y = nil if x = first. The following procedure deletes the node x from the list.
procedure delete ( x, y : pointer; var first : pointer);
begin
if y = nil then first : = first.link
else y.link := x.link;
dispose(x); {return the node }
end;
LINKED STACKS :
UNIT III
5
DSA
When several stacks and queues coexist, there is no way to represent them
sequentially. The solution is obtained by linked list representation of stacks and
queues.
Linked stack:
top
.
.
.
nil
Linked queue:
...
nil
rear
front
The direction of links for both the stack and queue are such as to facilitate easy
insertion and deletion of nodes. You can easily add or delete a node at the top of the
stack. You can add a node at the rear end and both deletion and addition can be
performed at the front. But normally we would not add nodes at the front.
Node and Pointer are defined as follows
Type pointer = ↑node;
node = record
data : integer;
UNIT III
6
DSA
link = pointer;
end;
The following global arrays of type pointer are used;
top[i] = node at top of the i th stack 1<=i<=n
front[i] = node at front of the i th queue 1<=i<=m
rear[i] = node at rear of the i th queue 1<=i<=m
The initial conditions are :
top[i] = nil , 1<=i<=n
front[i] = nil , 1<=i<=m
The boundary conditions are :
top[i] = nil, iff the i th stack is empty
front[i] = nil, iff the i th queue is empty
procedure for adding an element into a linked stack
Procedure addstack (i,y: integer);
{add y to the i’th stack,1<=i<=n}
var x : pointer;
begin
new(x); {get a node}
x.data := y; {set it’s data field}
x.link := top[i]; {attach to top of i th stack}
top[i] := x; {update stack pointer}
end; {of addstack}
procedure for deleting an element into a linked stack
Procedure deletestack ( i :integer; var y :integer);
{delete the top node from the stack i and set to y to be its data field, 1in}
var x : pointer;
begin
if top[i] = nil then stackempty;
x := top[i]; { data field of top node}
y := x.data;
top[i]:=x.link {remove the top node}
dispose(x); {free the node}
end; { of deletestack}
procedure for adding an element into a linked queue
Procedure addqueue(i,y: integer);
{add y to the i’th queue,1<=i<=m}
UNIT III
7
DSA
var x : pointer;
begin
new(x); {get a node}
x.data := y; {set it’s data field}
x.link := nil;
if front[i] = nil then
front[i] := x; { empty queue}
else
rear[i].link := x;
rear[i] := x;
end; {of addqueue}
procedure for adding an element into a linked stack
Procedure deletequeue(i, integer; var y: integer);
{delete the first node from queue i and set it to y to its data field,1in }
var x: pointer;
begin
if front[i] = nil then queueempty;
x := front[i];
front[i] := x.link; { delete the first node}
y := x.data;
dispose(x); { free the node}
end;{of deletequeue}
THE STORAGE POOL
The storage pool contains all nodes that are not currently being used. The point
that we should note in case of linked representation how a node is to be constructed.
The number and size of the fields will depend upon the kind of problem. The number
of pointers will depend upon the structural properties of the data and the operations to
be performed. The amount of space to be allocated for each field depends partly on the
problem and partly on the addressing characteristics of the machine. The next point is
whether or not any nodes will ever be returned.
If we assume that the nodes are not returned then we can allocate storage in
consecutive order. So, if the storage pool has n nodes with fields DATA and LINK,
then GETNODE could be implemented as follows:
Procedure GETNODE(I)
// I is set as a pointer to the next available node//
if AV > n then call NO_MORE_NODES
I
AV
AV
AV +1
end GETNODE
The variable AV must initially be set to one and we assume it as a global
variable.
We consider a general case. Normally we link together all of the available
nodes in a single list we call AV. Link fields are used to link the available nodes.
Procedure is given below:
UNIT III
8
DSA
Procedure INIT(n)
// initialize the storage pool , through the LINK field, to contain nodes
with addresses 1,2,3, … n and set AV to point to the first node in this
list//
for i
1 to n-1 do
LINK(i)
i +1
end
LINK(n)
0
AV
1
End INIT
After executing the INIT procedure , the program can used nodes. Every time a new
node is needed, a call to GETNODE procedure is made. GETNODE examines the list
AV and returns the first node on the list . The GEDNODE procedure is as follows:
Procedure GETNODE(X)
// X is set to point to a free node if there is one on AV //
if AV = 0 then call NO_MORE_NODES
X
AV
AV
LINK(AV)
end GETNODE
RET procedure is used to return the node to AV. AV is used as a stack since the last
node inserted into AV is the first node removed (LIFO). Procedure RET is as follows:
Procedure RET(X)
// X point to a node which is to be returned to the available space list //
LINK(X)
AV
AV
X
end RET
Most of the times if we look at the available space pool in the middle of processing ,
adjacent nodes may no longer have consecutive addresses. Suppose we have a variable
ptr which is a pointer to a node which is part of a list. Then the legal operation that we
can perform on pointer variable is :
1) Set ptr=0
2) Set ptr to point to a node.
APPLICATIONS OF LINKED LIST
POLYNOMIAL ADDITION:
Consider the polynomial
UNIT III
9
DSA
A(x) = a x + …… + a x , where the a are non-zero coefficients with
exponents e i such that em > e m-1 > …. > e 1  0; Each term will be represented
by a node. A node will be of fixed size having 3 fields which represent the
coefficient and exponent a term plus a pointer to the next term.
Assuming that all coefficients are integer, the required type declarations are:
Type polypointer = polynode;
polynode = record
Coef : integer ; {coefficient}
Exp : integer; {exponent }
Link : polypointer;
end;
Polynomials will be drawn as below :
coef
exp
link
For example the polynomial a = 3 x 14 + 2 x 8 + 1 would be stored as
3
-3
14
10
8
2
8
1
0
nil
10
0
6
nil
polynomial b = 8 x 14 – 3 x 10 + 10 x 6 would be stored as
8
14
8
-3
10
In order to add the polynomials together we examine the terms starting at the
nodes pointed by a and b. Two pointers p and q are used to move among the terms of a
and b. If the exponents are of the two terms are equal, then the coefficients are added
and a new term created for the result. If the exponent of a is less than the exponent
current of b, then a duplicate of the term of b is created and attached to c. The pointer
q is advanced to the next term. Similar action is taken on a p↑.exp > q↑.expThe
following figure illustrates the polynomial addition.
STEP 1:
UNIT III
10
DSA
3
14
2
8
1
14
-3
10
8
10
1
0
nil
p
q
8
nn
nil
il
il
6
p.exp = q.exp
11
14
nil
procedure padd (a,b : polypointer; var c : polypointer);
{polynomials a and b represented as singly linked lists
are summed to form the new polynomial named c }
var p.q.d: polypointer; x : integer ;
begin
p := a; q := b;{p, q point to next term of a and b}
new(c); d := c; {initial node for c, returned later }
while (p <> nil) and (q<> nil ) do
case compare (p.exp, q.exp) of
‘=’: begin
x := p.coef + q.coef;
if x <> 0 then attach (x , p.exp, d);
p := p.link; q := q.link; {advance to the next node}
end;
‘<’ : begin
attach (q.coef, q.exp, d);
q := q.link; {next term of b}
end;
‘>’ : begin
attach (p.coef, p.exp, d);
p := p.link; {next term of a}
end;
while p<> nil do { copy rest of a}
begin
UNIT III
11
DSA
attach (p.coef, p.exp, d);
p := p.link;
end;
while q<> nil do { copy rest of b}
begin
attach (q.coef, q.exp, d);
q := q.link;
end;
d.link := nil; { last node}
{delete the extra initial node}
p := c; c := c.link; dispose(p);
end; { of padd}
Procedure attach (c, e : integer ; var d:polypointer);
{create a new node with coef=c, and exp =e and attach it to the node pointed by d.}
var x : polypointer;
begin
new(x);
with x do begin coef :=c; exp:=e; d.link :=x; end;
d:=x;
end;
A list in which the last node points back to the first is called a circular list. A
singly linked list in which the last node has a nil link is called a chain.
-3
10
8
2
8
1
0
nil
Procedure to free all the nodes in the chain t
Procedure erase (var p: polypointer);
Var x: polypointer;
begin
While t<> nil do
begin
x := t↑.link;
dispose(t);
t := x;
end;
end;
The reason we dispose of nodes that are no longer in use is so that these nodes
may be reused later.
UNIT III
12
DSA
Procedure to erase the nodes in a circular linked list.
Procedure cerase (t : polypointer)
{erase a circular list}
var x : polypointer;
begin
if t <> nil
then begin
x := t↑.link; {second node}
t↑.link := av; {first node linked to av}
av:=x; {second node of t becomes front of av list}
end;
end ; {of cerase}
Procedure to invert a linked list.
Procedure invert (var x: pointer);
{ a chain pointed at by x is inverted so that if x= (a1,….an) , then after the execution
x=(an,…, a1) }
var p,q,r : pointer;
begin
p := x; q := nil; {q trails p}
while p <> nil do
begin
r := q; q := p; {r trails q}
p := p↑.link; { p moves to the next node}
q↑.link := r; { link q to proceeding node}
end;
x := q;
end; {of invert}
For a list of m >=1 nodes, the while loop is executed m times and so the
computing time is linear or O(m).
Procedure for concatenating two chains x and y.
Procedure concatenate (x,y : pointer ; var x : pointer)
{ x= (a1,…. an) and y = ( b1,…. bn), m,n >=0
produces the new chain z = ( a1, …am, b1,..bn)};
var p: pointer;
begin
if x = nil then
z := y
else begin
z := x;
if y <> nil
then begin { find the last node in x }
p := x;
UNIT III
13
DSA
while p↑.link <> nil do p :=p↑.link;
p↑.link := y; { link the last node of x to the first of y}
end;
end; {of concatenate}
end;
Procedure to insert a node at the front of the linked list.
Procedure insertfront (var a : pointer; x : pointer);
{insert the node pointed at by x at the front of the circular lists a, where a points to the
last node in the list}
begin
if a = nil
then begin { empty list}
a := x;
x ↑.link := x;
end
else begin
x↑.link := a↑.link;
a↑.link := x;
end;
end; { of insert}
To insert x at the rear, we need to add the additional statement a := x to the else part
of the insertfront
Function to find the length of a circular linked list
Function length (a : pointer): integer;
{ find the length of the circular linked list a}
var x : pointer;
begin
length := 0;
if a<> nil
then begin
x := a;
repeat
length := length +1;
x := x↑.link;
until x = a;
end;
end; {of length}
UNIT III
14
DSA
EQUIVALENCE RELATION
In FORTRAN one is allowed to share the same storage among several program
variables through the use of EQUIVALANCE statement. For example, the following
pair of FORTRAN statements
DIMENSION A(3), B(2,2), C(6)
EQUIVALENCE ( A(2),B(1,2),C(4)), (A(1),D), (D,E,F), (G,H)
MEMORY
1
C(1)
2
B(1,1)
C(2)
3
A(1)
B(2,1)
C(3)
4
A(2)
B(1,2)
C(4)
5
A(3)
B(2,2)
C(5)
6
7
D
E F
C(6)
G
H
As a result of equivalence of A(2), B(1,2) and C(4), these were assigned the same
storage word 4. This in turn equivalences A(1), B(2,1) and C(3), B(1,1) and C(2), and
A(3), B(2,2) and C(5). Because of the previous equivalence group, the equivalence
pair (A(1), D) also resulted in D sharing the same as B(2,1) and C(3). Hence an
equivalence of the form (D,C(5)) would conflict with previous equivalences, since
C(5) and C(3) cannot be assigned the same storage even though the sum of individual
storage requirements for these variables is 18, the memory map shows that only 7
words are actually required because of the overlapping specified by the equivalence
groups in the EQUIVALENCE statement,
The FORTRAN statement EQUIVALENCE specifies a relationship among
addresses of variables. This relation has several properties which it shares with other
relations such as the conventional mathematical equals. Suppose we denote an
arbitrary relation by the symbol ≡ and suppose that:
i)
ii)
UNIT III
For any variable x, x ≡ x. e.g. x is to be assigned the same location as itself.
Thus ≡ is reflexive.
For any two variables x, y, if x ≡ y, then y ≡ x, e.g. assigning y the same
location as x is the same as assigning x the same location as y. Thus the
relation ≡ is symmetric.
15
DSA
iii)
For any three variables x , y, and z, if x ≡ y , and y ≡ z then x ≡ z, e.g if x
and y are to be assigned the same location and y and z are also to be assigned
the same location then so also are x and z. The relation ≡ is transitive.
Definition : A relation ≡ over a set s , is said to be an equivalence relation over S iff
it is a symmetric, reflexive and transitive over S.
For example, if we have 12 variables numbered 1 through 12 and the following
equivalences are defined through the EQUIVALENCE statement.
1 ≡ 5 , 4 ≡ 2, 7 ≡ 11, 9 ≡ 10 , 8 ≡ 5, 7 ≡ 9, 4 ≡ 6, 3 ≡ 12, 12 ≡ 1
then as the result of reflexivity, symmetry, and transitivity of the relation ≡ , we get
the following partitioning of the 12 variables in to 3 equivalence classes.
{ 1, 3, 5, 8, 12 } ; { 2, 4, 6 } ; { 7, 9, 10, 11 }
So, only three words of storage are needed for the 12 variables. The number of
equivalence classes is the number of words of storage to be allocated and the members
of the same equivalence class are allocated the same word of storage.
In the following algorithm we use a one dimensional array seq[1..n] to represent
the head nodes of the n linked lists. out [1..n] is used to check whether the object i is
printed or not.
Procedure equivalence;
{declare seq, out and other local variables};
begin
initialize seq to nil and out to true;
while more pairs do { input pairs}
begin
read the next pair (i,j);
put j on the seq[i] list;
put I on the seq[j] list;
end
for i := 1 to n do { output equivalence classes}
if out[i] then begin
out[i] : = false;
output this equivalence classes;
end;
end; {of equivalence}
For the relation i ≡ j , two nodes are used. seq[i] points to a list of nodes which
contains every number which is directly equivalenced to i by an input relation.
UNIT III
16
DSA
In phase II we scan the seq array and start with the first i, 1≤i≤n such that out[i] =
true. Each element in the list seq[i]is printed. In order to process the remaining lists
which, by transitivity, belong in the same class i, a stack of their nodes is created.
After the while loop is completed the list would look like this
Seq 1
12
2
4
3
4
5
6
7
12
6
8
4
9
2
1
5
11
8
5
9
10
7
9
11
7
12
1
3
10
Sparse matrices
In a matrix if many of the entries are zero then we call it as a sparse matrix. In
case of sparse matrices space and computing time will be saved if only the non-zero
entries are retained explicitly. We represent the matrix elements in a sequential
scheme in which each node is of three fields: row, column, and value. As matrix
operations such as addition , subtraction and multiplication are performed , the
number of nonzero terms in matrices will vary. So linked scheme facilitates efficient
representation of varying size structures. In the data representation each column of a
sparse matrix is represented by a circularly linked list with a head node. In addition
each row will be a circularly linked list with a head node.
Each and every node has five fields: left,up,v,r and c. The left field points to the
node with the next smallest column subscript. Up points to the node with the next
smallest row subscript. V represents value , r represents row and c represents column.
The node structure is also referred to as MATRIX_ELEMENT.
node structure
left
V
UNIT III
up
R
C
17
DSA
Algorithm to construct sparse matrix is given below.
Algorithm Construct_Matrix ( In this algorithm M and N represents the number of
rows and columns respectively. Arrays AROW and ACOL contain pointers to the
head node of the circular lists, P and Q are auxiliary pointers.The row index, column
index, value are read into the variables ROW,COLUMN,VALUE respectively.
1. Repeat for I =1,2,…M
AROW[I] MATRIX_ELEMENT
C(AROW[I])0
LEFT(AROW[I])  AROW[I]
Repeat for I =1,2,…N
ACOL[I]  MATRIX_ELEMENT
R(ACOL[I])  0
UP(ACOL[I])  ACOL[I]
2. Repeat thru step 7 until input records are exhausted
3. Read (ROW,COLUMN,VALUE)
4. P  MATRIX_ELEMENT
R(P)  ROW
C(P)  COLUMN
V(P)  VALUE
5. Q  AROW[R(P)]
Repeat while C(P) < C(LEFT(Q))
Q  LEFT(Q)
LEFT(P)  LEFT(Q)
LEFT(Q)  P
6. Q  ACOL[C(P)]
Repeat while R(P) < R(UP(Q))
Q  UP(Q)
UP(P) UP(Q)
UP(Q)  P
7. Exit
QUESTION BANK
UNIT III
18
DSA
DATA STRUCTURES AND ALGORITHMS
UNIT III
SECTION A
1. A node in a doubly linked list has atleast _______ fields.
2. State the differences between an one dimensional array and a singly linked list.
3. In a linked list each element contains _______ and _____ parts.
4. In a circular linked list the link of the last node is _____.
5. In a linked list a node is defined as a collection of _______.
6. Explain linked list.
7. Explain singly linked list.
8. Explain doubly linked list.
9. Explain Equivalence relations.
10.Define reflexive property.
11.Define symmetric property.
12.Define transitive property.
13. In which language EQUIVALENCE statement is used which allows to share
the same storage among program variables?
14.Define storage pool.
15.Why linked list representation of sparse matrices is more advantageous than
array representation.
SECTION B
1. Compare array and linked lists.
2. Explain how deletions are performed in a doubly linked list.
3. Give an algorithm to delete a node in a linked list.
4. Write short note on Doubly linked list.
5. Explain how a singly linked list is formed.
6. Write procedure for finding the number of elements in a circular list.
7. Distinguish between singly linked list and doubly linked list.
8. What are singly linked list? Where are they used?
9. Write an algorithm to create a linked stack.
10.What are the merits and demerits of linked lists over arrays?
11.Write a procedure to invert a singly linked list.
UNIT III
19
DSA
12.Write a procedure to concatenate two lists.
13.Write a procedure to find the length of the list.
SECTION C
1. What are the advantages and disadvantages of linked lists? How linked lists can
be used for polynomial manipulation?
2. Explain in detail storage pools.
3. Give an algorithm to insert and delete in a double linked list.
4. Give an algorithm to insert a node to the right of a node whose value is given.
5. Explain how a sparse matrix and a polynomial in many variables are
represented using a linked list.
6. Write an algorithm to add and delete an item from linked stack.
7. Write an algorithm to add and delete an item form linked queue.
8. Write an algorithm to add a node at the beginning of the linked list.
9. Write an algorithm to add a node at the end of the linked list.
10.Write an algorithm to add a node in an ordered list.(middle)
11.Write an algorithm to delete a node from singly linked list.
12.Explain equivalence relation with an example.
SECTION D
1. Discuss the following in detail:
i.
Equivalence relations
ii.
Sparse matrices
2. Explain any one method of storing efficiently, the large sparse matrices.
3. Define and discuss advantages of linked stack. Explain how linked stack is
manipulated.
4. Explain the term equivalence relation. Discuss its usage and how it is
implemented.
5. Discuss how linked list structure can be used to represent polynomials. Give a
procedure to add two polynomials.
********
UNIT III
20
DSA
UNIT III
21
DSA