DS Lect 03 (ADT & Linked List)
DS Lect 03 (ADT & Linked List)
A definition for a data type solely in terms of a set of values and a set of operations
on that data type.
Def. Consists of
storage structures (data structures)
to store the data items
and
algorithms for the basic operations.
Data Structure
• A data structure is the physical implementation of an ADT.
– Each operation associated with the ADT is implemented by one or more
subroutines in the implementation.
Data Type
ADT:
Data Items:
Type
Logical Form
Operations
Each byte (or word) has an address making it possible to store and
retrieve contents of any given memory location.
Therefore:
the most basic form of data: sequences of bits
simple data types (values are atomic — can't be subdivided) are ADTs.
Implementations have:
» Storage structures: memory locations
» Algorithms: system hardware/software to do basic operations.
Boolean data
0 0 0 x !x
0 0 1
1 0 1
0 1
1 1 1 1 0
Character Data
Store numeric codes (ASCII, EBCDIC, Unicode)
1 byte for ASCII and EBCDIC,
2 bytes for Unicode
ASCII/EBCDIC
Unicode
Integer Data
88 = 0000000001011000
2
Signed integer:
Store in a fixed number w of bits using one of the following representations:
Sign-magnitude representation
88 0 _000000001011000
sign bit
–88 _000000001011000
1
Example: –88
1. 88 as a 16-bit base-two number 0000000001011000
2. Complement this bit string 1111111110100111
3. Add 1 1111111110101000
Introduction
• Fixed-size data structures
– Arrays, structs
• Dynamic data structures
– Grow and shrink as program runs
– Linked lists
• Insert/remove items anywhere
15 10
Linked Lists
firstP
tr
H D T D .. Q
.
Linked Lists
Linked lists vs. arrays
Arrays can become full
Allocating "extra" space in array wasteful, may never be used
Linked lists can grow/shrink as needed
Linked lists only become full when system runs out of memory
Linked lists can be maintained in sorted order
Insert element at proper position
Existing elements do not need to be moved
a firstPt
) r
7 11
newPt
r
12
b firstPt
) r
7 11
newPt
r
12
firstPtr = newPtr
If list empty, then
newPtr->nextPtr = firstPtr
firstPtr = lastPtr = newPtr
1 7 1 5
2 1
b first lastP newP
) Ptr tr tr
1 7 1 5
2 1
lastPtr->nextPtr = newPtr
lastPtr = newPtr
If list empty, then
firstPtr = lastPtr = newPtr
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 03: ADT CS221: Data Structures & Algo.
12 7 11 5
b firstP lastP
) tr tr
tempPtr = firstPtr
12 7 1 5
1
firstPtr = firstPtr->next;
tempP If there are no more nodes,
tr
firstPtr = lastPtr = NULL;
delete tempPtr;
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 03: ADT CS221: Data Structures & Algo.
12 7 11 5
12 7 1 5
1
tempPtr = lastPtr
lastPtr = currentPtr
tempP
tr
Linked Lists
• Node data
– Data for each node
– Link to next node
• Start pointer
1. void IntList::DisplayList(void)
2. {
3. IntList *ptrTemp=ListHeadPtr;
4. cout<<endl;
5. while (ptrTemp!=NULL)
6. {
7. cout<<ptrTemp->data<<",";
8. ptrTemp=ptrTemp->next;
9. }
10.}
11.void IntList::DisplaySpecificNode(int key)
12.{
13. IntList *ptrCurrent=ListHeadPtr;
9. if (ptrCurrent == NULL)
10. {
11. cout<<"\nElement to delete not found in the list";
12. return;
13. }
1. void main(void)
2. {
3. IntList::AddNodeAtTail(20);
4. IntList::AddNodeAtHead(10);
5. IntList::AddNodeAtHead(34);
6. IntList::AddNodeAtTail(12);
7. IntList::DisplayList();
8. IntList::DeleteNode(30);
9. IntList::DisplayList();
10.}
Destructor of list
• Do we need to write a destructor which should delete all nodes
of linked list?
• Obviously No!!!!!!
• For destroying whole list we should define another static
member function, DeleteAll
1. void IntList::DisplayList(void)
2. {
3. Node *ptrTemp=ListHeadPtr;
4. cout<<endl;
5. while (ptrTemp!=NULL)
6. {
7. cout<<ptrTemp->data<<",";
8. ptrTemp=ptrTemp->next;
9. }
10.}
11.void IntList::DisplaySpecificNode(int key)
12.{
13. Node *ptrCurrent=ListHeadPtr;
9. if (ptrCurrent == NULL)
10. {
11. cout<<"\nElement to delete not found in the list";
12. return;
13. }
1. IntList::~IntList()
2. {
3. Node *ptrPrevious;
4. while (ListHeadPtr!=NULL)
5. {
6. ptrPrevious = ListHeadPtr;
7. ListHeadPtr= ListHeadPtr->next;
8. delete ptrPrevious;
9. }
10.}
What about?
• 2-Way linked list (or doubly linked list)
• 1-Way circular linked list
• 2-Way circular linked list