Академический Документы
Профессиональный Документы
Культура Документы
List ADT
Abstract Data Types
A definition for a data type solely in terms of a set of values and a set of operations on
that data type.
Encapsulation:
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
Data values: {false, true}
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
88 = 00000000010110002
Signed integer:
Store in a fixed number w of bits using one of the following representations:
Sign-magnitude representation
88 _000000001011000
0
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
Array ADT Introduction
• Linear Vs Non-Linear DS
• Need Linear Relationship
• How
• Arrays, linked lists
• Operation on Linear DS
• Traversal
• Search
• Insertion
• Deletion
• Sorting
• Merging
• Now which linear structure to use?
Linked List ADT
Introduction
• Fixed-size data structures
• Arrays, structs
• Dynamic data structures
• Grow and shrink as program runs
• Linked lists
• Insert/remove items anywhere
Self-Referential Data Structures
• Self-referential data structure
• Has pointer to variable (object) of same data type
• Link together to form useful data structures
• Lists, stacks, queues, trees
• Terminated with NULL pointer
15 10
firstPtr
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
Linked List Operations
• Selected linked list operations
• Insert node at start
• Insert node at end
• Remove node from start
• Remove node from end
• Remove specific node
• Searching specific node
• Displaying all nodes
Insert at start (head)
a) firstPtr
7 11
newPtr
12
b) firstPtr
7 11
newPtr
12
firstPtr = newPtr
If list empty, then
newPtr->nextPtr = firstPtr
firstPtr = lastPtr = newPtr
Insertion at end (tail)
• Need to traverse whole list and find address of last node
• Once address of last node is known, new node can be appended at end of
list
• Problem:
• Not efficient: needs whole list traversal
• Solution: Use another pointer, must be updated after each insertion at tail
Insertion at end (tail)
a) firstPtr lastPtr newPtr
12 7 11 5
12 7 11 5
lastPtr->nextPtr = newPtr
lastPtr = newPtr
If list empty, then
firstPtr = lastPtr = newPtr
Remove Node from (start) head
a) firstPtr lastPtr
12 7 11 5
b) firstPtr lastPtr
tempPtr = firstPtr
12 7 11 5
firstPtr = firstPtr->next;
tempPtr
If there are no more nodes,
firstPtr = lastPtr = NULL;
delete tempPtr;
"Walk" list until get next-to-last node, until
currentPtr->nextPtr = lastPtr
a) firstPtr lastPtr
12 7 11 5
12 7 11 5
tempPtr = lastPtr
lastPtr = currentPtr
tempPtr
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. }
• Obviously No!!!!!!
• For destroying whole list we should define another static member
function, DeleteAll
Second Method: Using struct for node data
1.class IntList
2.{
3. struct Node
4. {
5. int data;
6. Node *next;
7. };
8. Node *ListHeadPtr;
9.public:
10. IntList() { ListHeadPtr = NULL; }
11. void AddNodeAtHead(int val);
12. void AddNodeAtTail(int val);
13. void DisplayList(void);
14. void DeleteNode(int key);
15. void DisplaySpecificNode(int key);
16. ~IntList();
17.};
1.void IntList::AddNodeAtHead(int val)
2.{
3. Node *ptrNew = new Node;
4. ptrNew->data = val;
5. ptrNew -> next = ListHeadPtr;
6. ListHeadPtr = ptrNew;
7.}
8.void IntList::AddNodeAtTail(int val)
9.{
10. Node *ptrNew = new Node, *ptrTemp=ListHeadPtr;
11. ptrNew->data = val;
12. ptrNew -> next = NULL;
13. if (ListHeadPtr == NULL)
14. {
15. ListHeadPtr = ptrNew;
16. return;
17. }
18. while (ptrTemp->next!=NULL)
19. ptrTemp=ptrTemp->next;
20. ptrTemp->next= ptrNew;
21.}
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. }
3. Node *ptrPrevious;
4. while (ListHeadPtr!=NULL)
5. {
6. ptrPrevious = ListHeadPtr;
7. ListHeadPtr= ListHeadPtr->next;
8. delete ptrPrevious;
9. }
10.}
1./*Driver program. Number of calls to functions of class to
test working, you should call more functions, and look
results thoroughly*/
2.void main(int key)
3.{
4. IntList list1,list2;
5. list1.AddNodeAtTail(20);
6. list1.AddNodeAtHead(10);
7. list1.AddNodeAtHead(34);
8. list1.AddNodeAtTail(12);
9. list1.DeleteNode(10);
10. cout<<"\nList1 after perorming few operations:";
11. list1.DisplayList();
12.
13. list2.AddNodeAtTail(23);
14. list2.AddNodeAtHead(45);
15. list2.AddNodeAtHead(75);
16. list2.AddNodeAtTail(29);
17. list2.DeleteNode(45);
18. cout<<"\nlist2 after perorming few operations:";
19. list2.DisplayList();
20.}
What about?
• 2-Way linked list (or doubly linked list)
• 1-Way circular linked list
• 2-Way circular linked list