0% found this document useful (0 votes)
34 views

DS Lect 03 (ADT & Linked List)

The document discusses abstract data types (ADTs) and lists the list ADT as an example. It defines an ADT as consisting of a set of values and operations on those values, independent of implementation. An ADT is implemented using data structures that provide storage for data items and algorithms for the operations. The document provides examples of different simple data types like Boolean, character, and integer data and their representation and operations. It also introduces the array and linked list linear data structures.

Uploaded by

Mukil
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

DS Lect 03 (ADT & Linked List)

The document discusses abstract data types (ADTs) and lists the list ADT as an example. It defines an ADT as consisting of a set of values and operations on those values, independent of implementation. An ADT is implemented using data structures that provide storage for data items and algorithms for the operations. The document provides examples of different simple data types like Boolean, character, and integer data and their representation and operations. It also introduces the array and linked list linear data structures.

Uploaded by

Mukil
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Lecture 03: ADT CS221: Data Structures & Algo.

Abstract Data Type


List ADT

Dr. Zahid Halim

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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.
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 03: ADT CS221: Data Structures & Algo.

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.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Data Type

ADT:
Data Items:
Type
Logical Form
Operations

Data Structure: Data Items:


Storage Space Physical Form
Subroutines

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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)
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 03: ADT CS221: Data Structures & Algo.

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 = 0000000001011000
2

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Sign-magnitude representation

Save one bit (usually most significant) for sign


(0 = +, 1 = – )

Use base-two representation in the other bits.

88  0 _000000001011000


sign bit

–88 _000000001011000
1

Cumbersome for arithmetic computations

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Array ADT Introduction


• Linear Vs Non-Liner DS
• Need Liner Relationship
– How
• Arrays, linked lists
• Operation on Liner DS
– Traversal
– Search
– Insertion
– Deletion
– Sorting
– Merging
• Now which liner structure to use?

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Linked List ADT

Dr. Zahid Halim

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Introduction
• Fixed-size data structures
– Arrays, structs
• Dynamic data structures
– Grow and shrink as program runs
– Linked lists
• Insert/remove items anywhere

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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)

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Self-Referential Data Structures


• Example Code
struct ListNode
{
int data;
ListNode *next;
};
ListNode *ListHeadPtr;

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Linked Lists

firstP
tr

H D T D .. Q
.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Insert at start (head)

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

Insertion at end (tail)


a firstP lastP newP
) tr tr tr

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.

Remove Node from (start) head


a firstP lastP
) tr tr

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.

"Walk" list until get next-to-last node, until


currentPtr->nextPtr = lastPtr
a firstPt lastPt
) r r

12 7 11 5

b firstP currentP lastP


) tr tr tr

12 7 1 5
1
tempPtr = lastPtr
lastPtr = currentPtr

tempP
tr

Remove from tail delete tempPtr


Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 03: ADT CS221: Data Structures & Algo.

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??

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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;

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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.}
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

3. Node *ptrPrevious;
4. while (ListHeadPtr!=NULL)
5. {
6. ptrPrevious = ListHeadPtr;
7. ListHeadPtr= ListHeadPtr->next;
8. delete ptrPrevious;
9. }
10.}

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

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

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 03: ADT CS221: Data Structures & Algo.

What about?
• 2-Way linked list (or doubly linked list)
• 1-Way circular linked list
• 2-Way circular linked list

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like