Objects: ( item 0 , item 1 , , item N1 ) Operations: Finding the length, N, of a list. Printing all the items in a list. Making an empty list. Finding X item from a list, 0 k < N. Inserting a new item after the k-th item of a list, 0 k < N. Deleting an item from a list. Finding next of the current item from a list. Finding previous of the current item from a list. 4.1 The List ADT: CHAPTER 4 Lists
CHAPTER 4 Lists. 4.1 The List ADT:. Objects : ( item 0 , item 1 , , item N 1 ). Operations : Finding the length , N , of a list. Printing all the items in a list. Making an empty list. Finding X item from a list, 0 k < N . - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Objects: ( item0, item1, , itemN1 )
Operations: Finding the length, N, of a list.
Printing all the items in a list.
Making an empty list.
Finding X item from a list, 0 k < N.
Inserting a new item after the k-th item of a list, 0 k < N.
Deleting an item from a list.
Finding next of the current item from a list.
Finding previous of the current item from a list.
4.1 The List ADT:
CHAPTER 4
Lists
Simple Array implementation of Lists
array[ i ] = itemi
MaxSize has to be estimated.
Address Content
array+i itemi
array+i+1 itemi+1
…… ……
…… ……
Sequential mapping
Finding takes O(n) time.
Insertion and Deletion not
only take O(N) time, but also involve a lot of data movements which takes time.
4.2 Singly Linked Lists• Linked list
– an ordered sequence of nodes with links represented as arrows
ptrbat cat sat vat null
– Delete mat from list :
– Insert mat after cat :ptr
bat cat sat vat null
mat
ptrbat cat sat vat nullmat
The capabilities to make linked representation:
• Define a node's structure ---- self-referential structures
• Create a new node ---- malloc function• Remove nodes ---- free function
〖 example 〗 list of words ending in atdefine a node typedef struct listNode *listPointer;structure typedef struct listNode {
char data [ 4 ]; listPointer link;
};create a new list listPointer ptr = NULL; ( #define IS_EMPTY(ptr) ( ! ( ptr )) )create new nodes ptr = (listPointer) malloc ( sizeof (listNode ));place a word into the list strcpy ( ptr ->data, "bat");
ptr -> link = null;Free point free(ptr);
〖 example 〗 list of words ending in atdefine a node typedef struct listNode *listPointer;structure typedef struct listNode {
char data [ 4 ]; listPointer link;
};create a new list listPointer ptr = NULL; ( #define IS_EMPTY(ptr) ( ! ( ptr )) )create new nodes ptr = (listPointer) malloc ( sizeof (listNode ));place a word into the list strcpy ( ptr ->data, "bat");
ptr -> link = null;Free point free(ptr);
address offirst node ptr->data ptr->link
ptr
b a t \0
address offirst node ptr->data ptr->link
ptr
b a t \0
〖 example 〗 List insertion
a1
first
NULLai ai+1 an... ...
b
temp
temp->link = x-> link
x-> link = temp
Question: What will happen if the order of the two steps is reversed?
Read Program 4.2 on p.153 and answer the question:Why is first passed as a pointer to listPointer?
x
〖 example 〗 List insertion#define IS_FULL (ptr ) ( ! (ptr ))void insert(listPointer *first, listPointer x){ /* insert a new node with data = 50 into the list ptr after node */
Question: How can wedelete the first node from a list?
Read Program 4.3 on p.155 and find the answer.
〖 example 〗 list deletion void delete(listPointer *first, listPointer trail, listPointer x){ /* delete node from the list, trail is the preceding node ptr is the head of the list */
if ( trail )trail->link = x->link;
else*first= (*first)->link;
free(x);}
ptr
10 20 null50
node
ptr
50 20 null
trail = NULL
before deletion after deletion
ptr
10 20 null50
node
ptr
50 20 null
trail = NULL
before deletion after deletion
List after the function call delete(&ptr, NULL, ptr)List after the function call delete(&ptr, NULL, ptr)
ptr
10 20 null50
trail
ptr
10 20 null
before deletion after deletion
nodeptr
10 20 null50
trail
ptr
10 20 null
before deletion after deletion
nodeList after the function call delete(&ptr, ptr, prt->link)List after the function call delete(&ptr, ptr, prt->link)
〖 example 〗 Printing out a list
void print_list(listPointer first){
printf("The list contains: ");for ( ; first; first= first->link )
printf("%4d”,first->data);printf("\n");
}
void print_list(listPointer first){
printf("The list contains: ");for ( ; first; first= first->link )
printf("%4d”,first->data);printf("\n");
}
4.3 Dynamically Linked Stacks and Queues• Linked Stacks top
null
element link
....
top
null
element link
....
STACK
NULLitem
item
item top
Push:
item top
temp
temp->link = top
top = temp
Rea
d P
rog
ram
4.5
an
d P
rog
ram
4.6
on
p.1
58ab
ou
t im
ple
men
tati
on
of
pu
sh a
nd
po
p
Pop: temp = top
top = top->link
top item = temp->item
free ( temp ) item
return item
Question: How to represent n stacks?
Answer:stackPointer top [ n ] ;
Program : Add to a linked stack
void push(int i, element item){
/* add an element to the top of the stack */stackPointer temp = (stackPointer)malloc(sizeof(stack));if ( IS_FULL(temp) ) {
the boudary condition for the queues:front [ i ] = NULL, iff the i th queue is empty 0 <= i < MAX_QUEUESIS_FULL ( temp) iff the memory is full
the boudary condition for the queues:front [ i ] = NULL, iff the i th queue is empty 0 <= i < MAX_QUEUESIS_FULL ( temp) iff the memory is full
4.5 Additional List Operations
1. operations for chains– a) Inverting a singly linked list– b) Concatenating singly linked lists
2. operations for circularly linked lists– a) Inserting at the front of a list– b) Finding the length of a circular list
a) Inverting a singly linked list
listPointer invert (listPointer lead ){ /* invert the list pointed to by lead */ listPointer middle, trail ; middle = NULL ; trail=NULL; /* middle is head of the inverted chain */ while ( lead ) { middle = lead ; lead = lead->link; middle->link= trail ; trail = middle ; } /* end while-loop */ return middle ;}
1
2
3
lead
NULL
middle
NULLtrail2
3
lead
NULL
1
middle
NULLtrail
2
3
lead
NULL1
middle
NULLtrail
Tinvert = O( length of lead )
b) Concatenating singly linked lists
listPointer concatenate listPointer ptr1, listPointer ptr2){/* produce a new list hat contains the list ptr1 followed by the list ptr2. The list pointed to by ptr1 is changed permanently */
listPointer concatenate listPointer ptr1, listPointer ptr2){/* produce a new list hat contains the list ptr1 followed by the list ptr2. The list pointed to by ptr1 is changed permanently */
a) Inserting at the front of a circularly linked list
void insertFront listPointer *last, listPointer node)/* insert node at the front of the circular list last, where last is the last node in the list */{ if ( IS_EMPTY(*last) ) {
/* list is empty, change ptr to point to new entry */*last= node;node->link = node;
}else { /* list is not empty, add new entry at front */
node->link = (*last)->link;(*last)->link = node;
}}
void insertFront listPointer *last, listPointer node)/* insert node at the front of the circular list last, where last is the last node in the list */{ if ( IS_EMPTY(*last) ) {
/* list is empty, change ptr to point to new entry */*last= node;node->link = node;
}else { /* list is not empty, add new entry at front */
node->link = (*last)->link;(*last)->link = node;
}}
x1 x3x2 a x1 x3x2 a
x1 x3x2 a
x1 x3x2 a
b) Finding the length of a circular list
int length(listPointer last){ /* find the length of the circular list last */
list_pointer temp;int count = 0;if (last) {
temp = last;do {
count ++;temp = temp->link;
} while ( temp != last);}return count;
}
int length(listPointer last){ /* find the length of the circular list last */
list_pointer temp;int count = 0;if (last) {
temp = last;do {
count ++;temp = temp->link;
} while ( temp != last);}return count;
}
Don’t we have enough headache already?
Why do we need the doubly linked lists?
Suppose you have a list 1->2->3->…->m.Now how would youget the m-th node?
I’ll go from the 1st nodeto the m-th node.
Then you are asked to find its previous node m 1?
Uhhh ... Then I’ll have to go from the 1st node again.