Вы находитесь на странице: 1из 40

Abstract Data Type

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.

Each ADT operation is defined by its inputs and outputs.

Encapsulation:

Combine Data and Functions in a single entity to hide implementation details.


Abstract Data Types
Def. a collection of related data items together with
an associated set of operations

e.g. whole numbers (integers) and arithmetic operators for


addition, subtraction, multiplication and division.

e.g. Flight reservation


Basic operations: find empty seat, reserve a seat,
cancel a seat assignment
Why "abstract?"
Data, operations, and relations are studied
independent of implementation.

What, not how is the focus.


Abstract Data Types
Def. Consists of
storage structures (data structures)
to store the data items
and
algorithms for the basic operations.

The storage structures/data structures used in


implementations are provided in a language (primitive or built-
in) or are built from the language constructs (user-defined).

In either case, successful software design uses data


abstraction:

Separating the definition of a data type from its


implementation.
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 structure usually refers to an organization of data in main memory.

• File structure is an organization for data on peripheral storage, such as a disk


drive.
Data Type

ADT:
Data Items:
Type
Logical Form
Operations

Data Structure: Data Items:


Storage Space Physical Form
Subroutines
Simple Data Types
Memory:
2-state devices « bits 0 and 1

Organized into bytes (8 bits) and


words (machine dependent — e.g., 2 bytes).

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}

In C/C++: false = 0, true = 1 (or nonzero)

Operations: and &&


or ||
not !
&& 0 1 | | 0 1

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

Basic operation: comparison to determine if Equal, Less than ,Greater


than, etc. use their numeric codes (i.e. use ordinal value)
Integer Data

Non-negative (unsigned) integer:


Store its base-two representation in a fixed number w of bits
(e.g., w = 16 or w = 32)

88 = 00000000010110002

Signed integer:
Store in a fixed number w of bits using one of the following representations:
Sign-magnitude representation

Save one bit (usually most significant) for sign


(0 = +, 1 = – )

Use base-two representation in the other bits.

88  _000000001011000
0

sign bit

–88 _000000001011000
1

Cumbersome for arithmetic computations


Two's complement representation
Same as
For nonnegative n: sign mag.
Use ordinary base-two representation with leading (sign) bit 0

For negative n (–n):


(1) Find w-bit base-2 representation of n
(2) Complement each bit.
(3) Add 1 (Flip all bits from rightmost 0 to the end)

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

Data member and


pointer NULL pointer (points to nothing)
Self-Referential Data Structures
• Example Code
struct ListNode
{
int data;
ListNode *next;
};
ListNode *ListHeadPtr;
Linked Lists

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

b) firstPtr lastPtr newPtr

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

b) firstPtr currentPtr lastPtr

12 7 11 5

tempPtr = lastPtr
lastPtr = currentPtr

tempPtr

Remove from tail delete tempPtr


Linked Lists
• Node data
• Data for each node
• Link to next node
• Start pointer

• How to implement a Linked List structure


using a class?
Suggestions??
Linked List Class
• Depends upon requirement
• Do we need to create only one list or more lists?
• If only one list is desired then simply make
• start pointer as static (only one start pointer required)
• data members and link to next node (next pointer) as non static
• If number of lists desired are more than one then start pointer required
for each list
• Creating struct for each node
• And making a list class
• We will see implementation of linked list by both methods
First Method: Declaring start pointer as static
1.class IntList
2.{
3. static IntList *ListHeadPtr;
4. int data;
5. IntList *next;
6.public:
7. static void AddNodeAtHead(int val);
8. static void AddNodeAtTail(int val);
9. static void DisplayList(void);
10. static void DeleteNode(int key);
11. static void DisplaySpecificNode(int key);
12.};
13.//defining and initializing static member of class
14.IntList* IntList::ListHeadPtr=NULL;
1. void IntList::AddNodeAtHead(int val)
2. {
3. IntList *ptrNew = new IntList;
4. ptrNew->data = val;
5. ptrNew -> next = ListHeadPtr;
6. ListHeadPtr = ptrNew;
7. }
8. void IntList::AddNodeAtTail(int val)
9. {
10. IntList *ptrNew = new IntList, *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. 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;

14. while (ptrCurrent!=NULL && ptrCurrent->data!=key)


15. ptrCurrent = ptrCurrent->next;

16. if (ptrCurrent == NULL)


17. cout<<"\nElement not found in the list";
18. else
19. cout<<"\nData for node is :"<<ptrCurrent->data;
20.}
1. void IntList::DeleteNode(int key)
2. {
3. IntList *ptrCurrent=ListHeadPtr,*ptrPrevious;

4. while (ptrCurrent!=NULL && ptrCurrent->data!=key)


5. {
6. ptrPrevious = ptrCurrent;
7. ptrCurrent = ptrCurrent->next;
8. }

9. if (ptrCurrent == NULL)
10. {
11. cout<<"\nElement to delete not found in the list";
12. return;
13. }

14. if (ptrCurrent == ListHeadPtr) //node to delete is first node


15. ListHeadPtr = ListHeadPtr->next;
16. else //node to delete is in middle or at end of list
17. ptrPrevious->next = ptrCurrent->next;

18. delete ptrCurrent;


19.}
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
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;

14. while (ptrCurrent!=NULL && ptrCurrent->data!=key)


15. ptrCurrent = ptrCurrent->next;

16. if (ptrCurrent == NULL)


17. cout<<"\nElement not found in the list";
18. else
19. cout<<"\nData for node is :"<<ptrCurrent->data;
20.}
1. void IntList::DeleteNode(int key)
2. {
3. Node *ptrCurrent=ListHeadPtr,*ptrPrevious;

4. while (ptrCurrent!=NULL && ptrCurrent->data!=key)


5. {
6. ptrPrevious = ptrCurrent;
7. ptrCurrent = ptrCurrent->next;
8. }

9. if (ptrCurrent == NULL)
10. {
11. cout<<"\nElement to delete not found in the list";
12. return;
13. }

14. if (ptrCurrent == ListHeadPtr) //node to delete is first node


15. ListHeadPtr = ListHeadPtr->next;
16. else //node to delete is in middle or at end of list
17. ptrPrevious->next = ptrCurrent->next;

18. delete ptrCurrent;


19.}
1.IntList::~IntList()
2.{

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

Вам также может понравиться