Chap 5' General Lists - PowerPoint PPT Presentation

1 / 79
About This Presentation
Title:

Chap 5' General Lists

Description:

And you can retrieve or replace the entry in any position. ... top of a stack, and treat either ends of a list as the front and rear of a queue. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 80
Provided by: phili73
Category:
Tags: chap | entry | general | lists | position | rear

less

Transcript and Presenter's Notes

Title: Chap 5' General Lists


1
Chap 5. General Lists
  • MCCS170

2
Overview
  • List
  • Implementations of List
  • Array implementation
  • Simply-linked list
  • Doubly-linked list
  • Circular linked list
  • Linked list in array

3
List
A list is a sequence of nodes. You can insert a
node and delete a node anywhere. And you can
retrieve or replace the entry in any
position. Stack and Queue are special cases of
list.
position
0
1
2
4
Usage of List
  • When we need to keep some records in a sequence
  • With many variations...

5
The List Module
The List module provides the type List with the
following functions to work on lists. void
CreateList (List L) void InsertList (Position
pos, ListEntry x, List L) void DeleteList
(Position pos, ListEntry x, List L) void
RetrieveList (Position pos, ListEntry x, List
L) void ReplaceList (Position pos, ListEntry x,
List L) void ClearList (List L) int ListSize
(List L) int ListEmpty (List L) int ListFull
(List L)
6
Example
include ltstdio.hgt include "list.h" int main ( )
List L char x CreateList(L)
InsertList(0, a, L) InsertList(1, c,
L) InsertList(1, b, L) DeleteList(2,
x, L) PrintList(L) ClearList(L)
return 0
The type List and the functions CreateList,
InsertList, etc, are defined in list.h and
list.cpp.
Remember, call CreateList before using the list,
and call ClearList when the list is no longer
used.
7
Exercise
Q1. Stack and queue are special cases of general
list we studied in this chapter. You can treat
position 0 as the top of a stack, and treat
either ends of a list as the front and rear of a
queue. Write the following functions.
void Push (ListEntry x, List S) void Pop
(ListEntry x, List S) void Enqueue (ListEntry
x, List Q) void Dequeue (ListEntry x, List Q)
8
Implementation
  • Array
  • Inefficient to implement list using array. Need
    to copy entries around a lot
  • Linked list
  • Simply linked list next
  • Doubly linked list next, prev (previous)
  • Variations circular list, phantom head/tail
  • Linked list in array

9
Array Implementation of List
struct Student int studid char name40
double score typedef Student
ListEntry define MAXLIST 8 typedef int
Position struct List ListEntry entry
MAXLIST int count
entry
count6
studid
name
score
7
6
5
5
peter
20
4
2
tom
73
3
8
david
56.8
2
21
andy
81.5
1
3
mary
100
0
13
betty
90
We can use an array to implement list. We store
the record at position X in L.entryX. L.count
stores the number of entries.
10
Insertion in the array implementation
entry
entry
count6
count7
score
score
studid
name
studid
name
7
7
6
6
5
peter
20
5
5
peter
20
5
2
tom
73
4
4
2
tom
73
8
david
56.8
3
3
8
david
56.8
21
andy
81.5
2
21
andy
81.5
2
1
3
mary
100
3
mary
100
1
0
0
13
betty
90
13
betty
90
A drawback of the array implementation is
insertion (and deletion) may involve a large
amount of data movement.
11
Comparison of Implementations
  • Contiguous storage is generally preferable when
  • the records are individually very small
  • the size of the list is known when the program
    is written
  • few insertions or deletions are made except at
    the end and
  • random access is important
  • Linked storage is generally preferable when
  • the records are large
  • the size of the list is not known in advance
    and
  • flexibility is needed in inserting, deleting,
    and rearranging the entries

12
Simply linked list implementation
count 3
0
1
2
position
typedef char ListEntry struct ListNode
ListEntry entry ListNode next typedef
int Position struct List int count
ListNode head
13
CreateList ( )
void CreateList (List L) L-gthead NULL
L-gtcount 0
head
count 0
14
SetPosition ( )
L-gthead
B
C
A
p
ListNode p p SetPosition(1, L)
/ SetPosition return a pointer to a node at
position pos. Pre pos is a valid position on
list L 0 lt pos lt L-gtcount. Post return a
pointer to the position pos. / ListNode
SetPosition (Position pos, List L)
15
SetPosition ( )
ListNode SetPosition (Position pos, List L)
if (poslt0 posgt L-gtcount)
printf("Attempt to set a position not in the
list.") return NULL ListNode q
L-gthead for (int i0 iltpos i) qq-gtnext
return q
16
InsertList ( )
B
head
C
A
InsertList(2,x,L)
B
C
A
head
17
InsertList ( )
Pay attention to the valid values for pos
/ InsertList insert a new entry in the list.
Pre The list L has been created, L is not full,
x is a valid list entry, 0 lt pos lt n
where n is the number of entries in L.
Post x has been inserted into position pos in
L the entry formerly in position pos
(provided posltn) and all later entries have
their position numbers increased by 1. /
void InsertList (Position pos, ListEntry x, List
L) ...
18
InsertList ( )
void InsertList (Position pos, ListEntry x,
List L) if (poslt0 posgtL-gtcount)
printf("Attempt to insert in a position not in
the list.") else ListNode np
(ListNode) malloc(sizeof(ListNode))
np-gtentry x if (pos0)
np-gtnext L-gthead L-gthead np
else ListNode p SetPosition(pos-1,L)
np-gtnext p-gtnext p-gtnext
np L-gtcount
Special handling for inserting at position 0
19
DeleteList ( )
B
head
C
A
DeleteList(1,x,L)
C
A
head
B
4.Return dp-gtentry and free(dp)
20
DeleteList ( )
Pay attention to the valid values for pos
/ DeleteList delete an entry from the list and
return it. Pre The list L has been created, L
is not empty, 0 lt pos lt n where n is the
number of entries in L. Post The entry at
position pos has been deleted from L and
return as x. The entry formerly in position
pos1 (provided pos1ltn) and all later
entries have their position numbers decreased
by 1. / void DeleteList (Position pos,
ListEntry x, List L) ...
21
DeleteList ( )
void DeleteList (Position pos, ListEntry x, List
L) ListNode dp if (poslt0
posgtL-gtcount) printf("Attempt to delete a
position not in the list.") else if
(pos0) dp L-gthead L-gthead
dp-gtnext else ListNode p
SetPosition(pos-1, L) dp p-gtnext
p-gtnext dp-gtnext L-gtcount-- x
dp-gtentry free(dp)
Special handling for deleting the node at
position 0.
22
Special case
  • InsertList(0,x,L) and DeleteList(0,x,L) have
    to be handled specially!

x A InsertList(0,x,L)
DeleteList(0,x,L)
23
Insert and Delete on Empty List
Check whether our InsertList and DeleteList
handle empty list correctly.
head
x B InsertList(0,x,L)
DeleteList(0,x,L)
24
Exercise
Q2. Complete the simply linked list
implementation of List. void CreateList (List
L) void InsertList (Position pos, ListEntry x,
List L) void DeleteList (Position pos,
ListEntry x, List L) void RetrieveList
(Position pos, ListEntry x, List L) void
ReplaceList (Position pos, ListEntry x, List
L) void ClearList (List L) int ListSize (List
L) int ListEmpty (List L) int ListFull (List
L)
25
Exercise
Q3. Write the functions in Exercise 5.1 on page
189 of textbook. E1-E11. Please refer to the
textbook for detail. For each of them, write two
versions. One uses the functions provided by the
List module, and the other manipulates the lists
directly. void InsertFirst (ListEntry x, List
L) void DeleteFirst (ListEntry x, List L) void
InsertLast (ListEntry x, List L) void DeleteLast
(ListEntry x, List L) void MedianList
(ListEntry x, List L) void InterchangeList
(Position pos1, Position pos2, List L) void
CopyList (List dest, List source) void JoinList
(List L1, List L2) / L1 to L2 / void
ReverseList (List L) void SplitList (List
source, List oddlist, List evenlist)
26
Example
/ v1. Copy the content of List source to List
dest / void CopyList (List dest, List source)
CreateList(dest) for (int i0
iltListSize(source) i) ListEntry x
RetrieveList(i, x, source) InsertList(i,
x, dest)
Write two versions of the function CopyList(List
dest, List source). One version can use the
functions provided by the List module, while the
other version manipulates the List directly
without using functions in the module.
27
Example
/ v2. Copy the content of List source to List
dest / void CopyList (List dest, List source)
if (source-gtcount0) dest-gtheadNULL
dest-gtcount0 return ListNode tail
ListNode p source-gthead while (p!NULL)
ListNode np (ListNode) malloc(sizeof(ListNo
de)) np-gtentry p-gtentry / special
handling for first node / if
(psource-gthead) dest-gthead np
else tail-gtnext np tail np
p p-gtnext tail-gtnext NULL
dest-gtcount source-gtcount
28
Simply linked list with current position
If the list is long and we often need to insert
nodes at consecutive position, we can improve the
list by remembering the last-used position.
B
head
C
A
0
1
2
position
typedef char ListEntry struct ListNode
ListEntry entry ListNode next
typedef int Position struct List int count
ListNode head Position currentPos
ListNode current
29
SetPosition ( )
ListNode SetPosition (Position pos, List L)
if (poslt0 posgt L-gtcount)
printf("Attempt to set a position not in the
list.") return NULL if
(posltL-gtcurrentPos) L-gtcurrent L-gthead
L-gtcurrentPos 0 ListNode q
L-gtcurrent for (int iL-gtcurrentPos iltpos
i) qq-gtnext L-gtcurrent q
L-gtcurrentPospos return q
SetPosition( ) tries to take advantage of
current in locating the pointer q. In addition
it updates the current pointer (and currentPos)
to the last pos searched.
30
Exercise
Q4. Review the simply linked list implementation
of List (list.h, list.cpp). Make suitable
modification to maintain the correctness of the
current position (L-gtcurrent and
L-gtcurrentPos). void CreateList (List L) void
InsertList (Position pos, ListEntry x, List
L) void DeleteList (Position pos, ListEntry x,
List L) void RetrieveList (Position pos,
ListEntry x, List L) void ReplaceList
(Position pos, ListEntry x, List L) void
ClearList (List L) int ListSize (List L) int
ListEmpty (List L) int ListFull (List L)
struct List int count ListNode head
Position currentPos ListNode current
31
Doubly linked list implementation
0
1
2
position
C
B
A
head
count 3
typedef char ListEntry struct ListNode
ListEntry entry ListNode next,
prev typedef int Position
struct List int count ListNode head
32
Doubly linked list
void CreateList (List L) void InsertList
(Position pos, ListEntry x, List L) void
DeleteList (Position pos, ListEntry x, List
L) void RetrieveList (Position pos, ListEntry
x, List L) void ReplaceList (Position pos,
ListEntry x, List L) void ClearList (List
L) int ListSize (List L) int ListEmpty (List
L) int ListFull (List L)
The implementation of most operations are the
same as simply linked list. However, InsertList
and DeleteList are more sophisticated.
33
InsertList ( )
  • Four cases
  • case 1 insert into empty listL-gtcount0
  • case 2 insert as first node of nonempty list,
    pos0, L-gtcountgt0
  • case 3 insert as last node of nonempty list,
    posL-gtcount, L-gtcountgt0
  • case 4 insert as a middle node in a nonempty
    list, 0 lt pos lt L-gtcount, L-gtcountgt0

34
Insert List, case 1
head
CreateList(L) InsertList(0,B,L)
35
Insert List, case 2
head
C
A
InsertList(0,B,L)
C
A
36
Insert List, case 3
head
C
A
InsertList(2,B,L)
head
C
A
37
Insert List, case 4
head
C
A
InsertList(1,B,L)
head
A
C
38
InsertList ( )
void InsertList (Position pos, ListEntry x, List
L) if (poslt0 posgtL-gtcount)
printf("Attempt to insert in a position not in
the list") else ListNode np
(ListNode) malloc (sizeof(ListNode))
np-gtentry x if (pos0 L-gtcount0)
np-gtnext NULL np-gtprev NULL
L-gthead np else if (pos0
L-gtcountgt0) np-gtprev NULL
np-gtnext L-gthead L-gthead-gtprev np
L-gthead np else
Case 1
Case 2
39
InsertList ( )
... else if (posL-gtcount
L-gtcountgt0) ListNode p
SetPosition(L-gtcount-1,L) p-gtnext np
np-gtprev p np-gtnext NULL
else ListNode p SetPosition(pos-1,L)
ListNode q p-gtnext p-gtnext np
np-gtprev p np-gtnext q
q-gtprev np L-gtcount
Case 3
Case 4
40
Delete List
  • Four cases
  • case 1 delete the only node of a list
    L-gtcount1
  • case 2 delete the first node, pos0,
    L-gtcountgt1
  • case 3 delete the last node, posL-gtcount-1,
    L-gtcountgt1
  • case 4 delete a middle node,0 lt pos lt
    L-gtcount-1, L-gtcountgt1

41
Exercise
void DeleteList (Position pos, ListEntry x, List
L) ListNode dp, p, q if (poslt0
posgtL-gtcount) printf("Attempt to delete a
position not in the list") else dp
SetPosition(pos, L) if (pos0
L-gtcount1) else if (pos0
L-gtcountgt1) else if
(posL-gtcount-1 L-gtcountgt1)
else L-gtcount-- x
dp-gtentry free(dp)
Q5. Complete the function DeleteList for doubly
linked list.
42
Circular Linked List
In a circular linked list, the next pointer of
the last node points back to the first node. This
allows traversal of the whole list to start at
any node.
Q6. Write all the functions in the List module
for circular linked list. You can start by
modifying the simply linked list implementation.
43
Linked list with a dummy head node
In a simply linked list, insertion and deletion
at position 0 have to be handled specially
because we need to modify the head pointer,
instead of the next pointer of the previous node.
One way to avoid this special case is to keep an
artificial head (dummy) node that does not store
data.
Q7. Write all the functions in the List module
for a linked list with a dummy head node. You
can start by modifying the simply linked list
implementation.
44
Linked List in Array
entry
count6
score
studid
name
7
Even if we use contiguous storage, we can
implement the next pointer in a linked list
with an array of next index. This example shows
a linked list of student records in ascending
order of studid.
6
5
5
peter
20
4
2
tom
73
3
8
david
56.8
2
21
andy
81.5
1
3
mary
100
0
13
betty
90
45
Linked Lists in Array
score
studid
name
7
6
5
5
peter
20
4
2
tom
73
3
8
david
56.8
2
21
andy
81.5
1
3
mary
100
0
13
betty
90
entry
count6
We can even store multiple ordering of the same
set of records using more than one linked lists
in array.
46
Available Entries as Linked list
next
score
studid
name
-1
3
We also need to keep track of a list of available
entries in the array of records. (We cannot free
entry in an array as in a real linked list.) An
easy way is to store them as another linked list
in the next array.
7
7
avail
6
5
peter
20
0
5
2
tom
73
1
4
delete
6
3
21
andy
81.5
-1
2
3
mary
100
5
1
13
betty
90
2
0
entry
count6
4
head
47
Linked List in Array
next
typedef char ListEntry define MAXLIST
8 typedef int ListIndex typedef int
Position struct List ListEntry entry
MAXLIST int count ListIndex next
MAXLIST ListIndex head, avail
entry
-1
7
7
6
C
0
5
A
1
4
4
head
6
3
3
avail
E
-1
2
5
B
5
1
count
D
2
0
Data are stored in the array entry. The number
of records stored is count. The next array
contains two linked lists a linked list of
entries with data, and one of available entries.
The index of the first entries of the lists are
head and avail respectively.
48
CreateList ( )
next
entry
void CreateList (List L) L-gtcount 0
L-gthead -1 for (int i0 iltMAXLIST-1 i)
L-gtnext i i1 L-gtnext MAXLIST-1 -1
L-gtavail 0
-1
7
7
6
6
5
5
4
-1
head
4
3
0
avail
3
2
0
2
1
count
1
0
In pointer implementation, the head points to
NULL. Here we use 1 to mean end of list. We
also need to initialize the list of available
entries in an empty list in CreateList( ).
49
SetPosition ( )
next
entry
ListIndex SetPosition (Position pos, List L)
if (poslt0 posgt L-gtcount)
printf("Attempt to set a position not in the
list.") return -1 else int
kL-gthead for (int i0 iltpos i) k
L-gtnextk return k
-1
7
7
6
C
0
5
A
1
4
6
3
E
-1
2
B
5
1
D
2
0
4
head
ListIndex p p SetPosition(2, L) // p 5
3
avail
5
count
50
NewNode ( )
next
entry
In linked list in array implementation, we
cannot use malloc ( ) and free ( ) to allocate
and de-allocate storage cells. We need to
manipulate the list of available cells ourselves.
-1
7
7
6
C
0
5
A
1
4
6
3
E
-1
2
B
5
1
ListIndex NewNode (List L) Allocate a storage
cell from the available list. In the example,
NewNode(L) returns 3. We can then store our new
entry at L-gtentry3.
D
2
0
4
head
3
avail
5
count
51
NewNode ( )
ListIndex NewNode (List L) ListIndex
newindex -1 if (L-gtavail -1)
printf("Overflow workspace for linked list is
full.") return -1 else newindex
L-gtavail L-gtavail L-gtnext L-gtavail
// L-gtnext newindex -1 return
newindex
52
DisposeNode ( )
next
entry
7
-1
void DisposeNode (ListIndex oldindex, List
L) Return the storage cell at index oldindex
to the available list. Suppose we want to free
the entry E, we can call DisposeNode(2, L).
6
7
5
C
0
4
A
1
3
6
2
E
-1
1
B
5
0
D
2
4
head
3
avail
5
count
53
DisposeNode ( )
void DisposeNode (ListIndex oldindex, List L)
if (oldindex -1) printf("Disposing a
nonexistent node in workspace array.") else
L-gtnext oldindex L-gtavail L-gtavail
oldindex
54
InsertList ( )
next
entry
E
7
6
6
3
D
5
C
0
4
A
1
3
-1
C
2
E
-1
1
B
5
0
D
B
2
A
4
head
7
avail
head
5
count
InsertList(2,x,L)
55
InsertList ( )
void InsertList (Position pos, ListEntry x,
List L) ListIndex np, p if (poslt0
posgtL-gtcount) printf("Attempt to insert in
a position not in the list.") else
np NewNode(L) // if (np-1), handle
list full error L-gtentrynp x if
(pos0) L-gtnextnp L-gthead
L-gthead np else p
SetPosition(pos-1, L) L-gtnextnp
L-gtnextp L-gtnextp np
L-gtcount
InsertList( ) and DeleteList( ) are similar to
the versions in simply linked list.
56
DeleteList ( )
next
entry
E
7
6
6
3
D
5
C
0
4
A
1
3
-1
C
2
E
-1
1
B
5
0
D
B
2
A
4
head
7
avail
head
5
count
DeleteList(2,x,L)
57
Exercise
Q8. Complete the linked list in array
implementation of List. void CreateList (List
L) void InsertList (Position pos, ListEntry x,
List L) void DeleteList (Position pos,
ListEntry x, List L) void RetrieveList
(Position pos, ListEntry x, List L) void
ReplaceList (Position pos, ListEntry x, List
L) void ClearList (List L) int ListSize (List
L) int ListEmpty (List L) int ListFull (List
L)
58
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
4
3
-1
head
3
2
0
avail
2
1
0
count
1
0
head
59
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
4
3
0
head
3
2
1
avail
2
1
1
count
a
-1
0
head
a
60
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
4
3
0
head
3
2
2
avail
b
-1
1
2
count
a
1
0
b
head
a
61
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
4
3
2
head
c
0
2
3
avail
b
-1
1
3
count
a
1
0
a
head
b
c
62
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
d
1
3
2
head
c
0
2
4
avail
b
-1
1
4
count
a
3
0
a
head
d
c
b
63
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
d
1
3
2
head
c
3
2
0
avail
b
-1
1
3
count
4
0
d
head
b
c
64
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
d
1
3
3
head
0
2
2
avail
b
-1
1
2
count
4
0
b
head
d
65
Example
int main ( ) List L char x
CreateList(L) InsertList(0, a, L)
InsertList(1, b, L) InsertList(0, c,
L) InsertList(2, d, L) DeleteList(1,
x, L) DeleteList(0, x, L) InsertList(2,
e, L)
next
entry
-1
7
7
6
6
5
5
4
d
1
3
3
head
e
-1
2
0
avail
b
2
1
3
count
4
0
b
head
e
d
66
Answer to Q3
void InsertFirst (ListEntry x, List L)
InsertList(0,x,L) void DeleteFirst (ListEntry
x, List L) DeleteList(0,x,L) void
InsertLast (ListEntry x, List L)
InsertList(ListSize(L), x, L) void DeleteLast
(ListEntry x, List L) DeleteList(ListSize(L)-
1, x, L) void MedianList (ListEntry x, List
L) int pos (ListSize(L)-1) / 2
RetrieveList(pos, x, L)
67
Answer to Q3
void InterchangeList (Position pos1, Position
pos2, List L) ListEntry x, y
RetrieveList(pos1, x, L) RetrieveList(pos2,
y, L) ReplaceList(pos2, x, L)
ReplaceList(pos1, y, L)
68
Answer to Q3
/ copy entries from L1 to end of L2. According
to textbook / void JoinList (List L1, List L2)
for (int i0 ilt ListSize(L1) i)
ListEntry x RetrieveList(i, x, L1)
InsertList(ListSize(L2), x, L2) / move
entries from L1 to end of L2 / void JoinList2
(List L1, List L2) ListEntry x while
(!ListEmpty(L1)) DeleteList(0, x, L1)
InsertList(ListSize(L2), x, L2)
69
Answer to Q3
void ReverseList (List L) for (int i0
iltListSize(L)/2 i) ListEntry x,y
int j ListSize(L) - i -1 RetrieveList(i,
x, L) RetrieveList(j, y, L)
ReplaceList(i, y, L) ReplaceList(j, x, L)

70
Answer to Q3
/ source remains unchanged. / / According to
the questions in textbook / void SplitList (List
source, List oddlist, List evenlist)
CreateList(oddlist) CreateList(evenlist)
for (int i0 ilt ListSize(source) i)
ListEntry x RetrieveList(i, x, source)
if (i21) InsertList(ListSize(oddlist),
x, oddlist) else InsertList(ListSize(e
venlist), x, evenlist)
71
Answer to Q3
/ source becomes empty / void SplitList2 (List
source, List oddlist, List evenlist) int n
ListSize(source) CreateList(oddlist)
CreateList(evenlist) for (int i0 iltn i)
ListEntry x DeleteList(0, x,
source) if (i21)
InsertList(ListSize(oddlist), x, oddlist)
else InsertList(ListSize(evenlist), x,
evenlist)
72
Answer to Q4
void InsertList (Position pos, ListEntry x,
List L) ListNode np, p if (poslt0
posgtL-gtcount) printf("Attempt to insert in
a position not in the list.") else
np (ListNode) malloc(sizeof(ListNode))
np-gtentry x if (pos0)
np-gtnext L-gthead L-gthead np
if (L-gtcountgt0) L-gtcurrentPos
else L-gtcurrent L-gthead
L-gtcurrentPos0 else
p SetPosition(pos-1, L) np-gtnext
p-gtnext p-gtnext np
L-gtcount
void CreateList (List L) L-gthead NULL
L-gtcount 0 / optional change / /
similar for ClearList / L-gtcurrent NULL
L-gtcurrentPos 0
73
Answer to Q4
void DeleteList (Position pos, ListEntry x, List
L) ListNode dp, p if (poslt0
posgtL-gtcount) printf("Attempt to delete a
position not in the list.") else if
(pos0) dp L-gthead L-gthead
dp-gtnext if (L-gtcurrentPos0)
L-gtcurrentL-gthead else
L-gtcurrentPos-- else p
SetPosition(pos-1, L) dp p-gtnext
p-gtnext dp-gtnext L-gtcount-- x
dp-gtentry free(dp)
74
Answer to Q5
void DeleteList (Position pos, ListEntry x, List
L) ListNode dp, p, q if (poslt0
posgtL-gtcount) printf("Attempt to delete a
position not in the list") else dp
SetPosition(pos, L) if (pos0
L-gtcount1) L-gthead NULL else
if (pos0 L-gtcountgt1) L-gthead
dp-gtnext L-gthead-gtprev NULL else
if (posL-gtcount-1 L-gtcountgt1)
dp-gtprev-gtnext NULL else
dp-gtprev-gtnext dp-gtnext dp-gtnext-gtprev
dp-gtprev L-gtcount-- x
dp-gtentry free(dp)
Case 1
Case 2
Case 3
Another solution for case 4. p dp-gtprev q
dp-gtnext p-gtnext q q-gtprev p
Case 4
75
Answer to Q6
void PrintList (List L) ListNode p p
L-gthead printf(" ") if (L-gthead!NULL)
do printf("c ", p-gtentry) p
p-gtnext while (p!L-gthead)
printf("\n")
void PrintList (List L) ListNode p p
L-gthead printf(" ") for (int i0
iltL-gtcount i) printf("c ", p-gtentry)
p p-gtnext printf("\n")
76
Answer to Q6
void ClearList (List L) ListNode p
ListNode dp if (L-gtheadNULL) return p
L-gthead-gtnext while (p!L-gthead) dp
p p p-gtnext free(dp)
free(L-gthead) L-gthead NULL L-gtcount 0
void ClearList (List L) ListNode
pL-gthead ListNode dp for (int i0
iltL-gtcount i) dp p p p-gtnext
free(dp) L-gthead NULL L-gtcount
0
77
Answer to Q6
// for circular linked list void InsertList
(Position pos, ListEntry x, List L)
ListNode np, p if (poslt0 posgtL-gtcount)
printf("Attempt to insert in a position not
in the list.") else np (ListNode)
malloc (sizeof(ListNode)) np-gtentry x
if (L-gtcount0) L-gthead np
np-gtnext np else if (pos0)
p SetPosition(L-gtcount-1, L)
np-gtnext p-gtnext / or L-gthead /
p-gtnext np L-gthead np else
p SetPosition(pos-1, L)
np-gtnext p-gtnext p-gtnext np
L-gtcount
78
Answer to Q6
// for circular linked list void DeleteList
(Position pos, ListEntry x, List L)
ListNode dp, p if (poslt0 posgtL-gtcount)
printf("Attempt to delete a position not in the
list.") else if (L-gtcount1)
dp L-gthead L-gthead NULL else
if (pos0) p SetPosition(L-gtcount-1,
L) dp L-gthead L-gthead
dp-gtnext p-gtnext L-gthead else
p SetPosition(pos-1, L) dp
p-gtnext p-gtnext dp-gtnext
L-gtcount-- x dp-gtentry free(dp)

79
Answer to Q8
// For the linked list in array
implementation void DeleteList (Position pos,
ListEntry x, List L) ListIndex dp, p if
(poslt0 posgtL-gtcount) printf("Attempt to
delete a position not in the list.") else
if (pos0) dp L-gthead
L-gthead L-gtnextdp else p
SetPosition(pos-1, L) dp L-gtnextp
L-gtnextp L-gtnextdp L-gtcount
-- x L-gtentrydp DisposeNode(dp,
L)
Write a Comment
User Comments (0)
About PowerShow.com