CPEN 204
Data Structures
Lecture 02
ARRAYS AND LINKED LIST
AS ADT
Outline
• What are Data Types ?
• What is Abstact Data Types (ADTs)?
• Why ADT?
• Arrays as an ADT
• Link List as an ADT
• Assignment #2
What are Data Types ?
• Defines a certain domain of values
• Defines operations allowed on those values.
• Eg.
• Int ype
• - takes only integer values
• Operations: addition, subtraction,
multiplication, bitwise operations etc.
What is ADT?
• ADTs are like user defined data types which
defines operations on values using
functions without specifying what is there
inside the function and how the operations are
performed.
What is ADT?
• Eg. Stack ADT
• A stack consists of elements of the same type
arranged in a sequential order.
• Operations: initialize(), push(), pop(),
isEmpty(), isFull()
Why ADTs
• A program which uses data structure is
called a client program.
• It has access to the ADTs ie. Interface
• The program which implements the data
structure is called implementation
Advantages of DTs
• It can be used without knowing the
implementation.
• Implementation can be changed from
one type to the other without affecting
the client program.
Arrays
• The simplest type of data structure is a linear (or
one dimensional) array. E.g.
– A 1, A 2, A 3 . . . . A n
– or by the parenthesis notation
• A (1), A (2), A (3) . . . . . . A (n)
– or by the bracket notation
• A [1], A [2], A [3] . . . . . . A [n]
• A[8] =
Arrays as an ADT
• Arrays are used for implementing a variety of other
data structures.
• string,
• Strings composed of zeros and ones are called
binary strings or bit strings.
• Strings are indispensable for processing textual data,
defining computer languages and compiling programs
written in them, and studying abstract computational
models.
Operations on array
• Traversing: it means processing or visiting each
element in the array exactly once.
• Insertion: means to put values into an array
• Deletion / Remove: to delete a value from an
array.
Operations on array
• Sorting: Re-arrangement of values in an array in
a specific order
• (Ascending / Descending) is called sorting.
• Searching: The process of finding the location of
a particular element in an array is called
searching.
There are two popular searching techniques:
• Linear search and binary search and will be
Limitations of Arrays
• An array has a fixed size which means you
cannot add/delete elements after creation.
• You also cannot resize them dynamically.
• Unlike lists in Python, cannot store values of
different data types in a single array.
Linked List as an ADT
Objectives
–Learn about linked list
– Be aware of basic properties of linked list
– Explore the insertion and deletion operations on
linked list
– Discover how to build and manipulate a linked list
– Learn how to construct a doubly linked list
What is a List
• A list is a finite sequence of data items, i.e., a
collection of data items arranged in a certain linear
order.
Ways of storing list In memory:
• sequential memory locations i.e store the list in
arrays.
• using pointers or links to associate elements
sequentially i.e known as linked list.
Linked Lists
• A linked list is a sequence of zero or more elements
called nodes.
• Each item in the list is called a node.
• Each node contains two kinds of information:
o some data and
o one or more links called pointers to other nodes of the
linked list.
Linked Lists
• Information- contains the item being stored in the
list.
• Next address- contains the address of the next item
in the list.
• The last node in the list contains NULL pointer to
indicate that it is the end of the list.
Linked List
• Linked list is a very flexible dynamic data
structure :
• ie. items may be added to it or deleted from it
at will.
• program will have to accommodate in
advance.
• This allows robust programs which require
much less maintenance.
Types of Linked lists
• Singly Linked List : A singly linked list, or
simply a linked list, is a linear collection of data
items.
•
Singly Linked List
• A singly linked list has the disadvantage that we can
only traverse it in one direction.
• Many applications require searching backwards
and forwards through sections of a list.
Doubly Linked List
•Doubly Linked List permit traversing or searching of
the list in both directions.
•In this linked list each node contains three fields.
– One to store data
– Remaining are self referential pointers which points
to previous and next nodes in the list
Implementation of node using structure
• Method -1:
struct node
{
int data;
struct node *prev; struct node * next;
};
Implementation of node using class
• Method -2:
class node
{
public:
int data; node *prev; node * next;
};
Insertions operation
• To place an elements in the list there are 3 cases
 At the beginning
 End of the list
 At a given position
Deletions operation
Removing an element from the list, without destroying
the integrity of the list itself.
To place an element from the list there are 3 cases :
Delete a node at beginning of the list
Delete a node at end of the list
Delete a node at a given position
Circularly Linked List
• Simply circular list, is a linked list in which the last
node always points to the first node.
• It can be built by replacing the NULL pointer at the
end of the list with a pointer which points to the first
node.
• There is no first or last node in the circular list.
Advantages
Any node can be traversed starting from any other
node in the list.
There is no need of NULL pointer to signal the end
of the list and hence, all pointers contain valid
addresses.
In contrast to singly linked list, deletion operation
in circular list is simplified as the search for the
previous node of an element to be deleted can be
started from that item itself.
Applications of the Linked List
• It can be used to represent polynomials.
• Using a linked list, we can perform the
polynomial manipulation.
• performed addition or subtraction of long
integers arithmetic operations.
• To implement stacks and queues.
Limitations of Linked List
The linked allocation has the following draw backs:
• No direct access to a particular element.
• Additional memory required for pointers.
• Searching must be done sequentially, however,
when the list is large, it becomes time consuming.
Basic Operations
• 1. Initialize the list.
• 2. Determine whether the list is empty.
• 3. Print the list.
• 4. Find the length of the list.
• 5. Destroy the list
Basic Operations
• 16. Retrieve the info contained in the first node.
• 7. Retrieve the info contained in the last node.
• 8. Search the list for a given item.
• 9. Insert an item in the list.
• 10. Delete an item from the list.
• 11. Make a copy of the linked list
Operations of Linked List
• Operations on Doubly linked list:
 Insertion of a node
 Deletions of a node
 Traversing the list
Assignment 02
1. Write the algorithm and a program to determine the
median of the array given below: (9, 4, 5, 1, 7, 78,
22, 15, 96, 45,25)
Note that the median of an array is the middle element
of a sorted array.
2. Discuss 4 differences between Circular Array and
Linked List

lecture 02.2.ppt

  • 1.
    CPEN 204 Data Structures Lecture02 ARRAYS AND LINKED LIST AS ADT
  • 2.
    Outline • What areData Types ? • What is Abstact Data Types (ADTs)? • Why ADT? • Arrays as an ADT • Link List as an ADT • Assignment #2
  • 3.
    What are DataTypes ? • Defines a certain domain of values • Defines operations allowed on those values. • Eg. • Int ype • - takes only integer values • Operations: addition, subtraction, multiplication, bitwise operations etc.
  • 4.
    What is ADT? •ADTs are like user defined data types which defines operations on values using functions without specifying what is there inside the function and how the operations are performed.
  • 5.
    What is ADT? •Eg. Stack ADT • A stack consists of elements of the same type arranged in a sequential order. • Operations: initialize(), push(), pop(), isEmpty(), isFull()
  • 6.
    Why ADTs • Aprogram which uses data structure is called a client program. • It has access to the ADTs ie. Interface • The program which implements the data structure is called implementation
  • 7.
    Advantages of DTs •It can be used without knowing the implementation. • Implementation can be changed from one type to the other without affecting the client program.
  • 8.
    Arrays • The simplesttype of data structure is a linear (or one dimensional) array. E.g. – A 1, A 2, A 3 . . . . A n – or by the parenthesis notation • A (1), A (2), A (3) . . . . . . A (n) – or by the bracket notation • A [1], A [2], A [3] . . . . . . A [n] • A[8] =
  • 9.
    Arrays as anADT • Arrays are used for implementing a variety of other data structures. • string, • Strings composed of zeros and ones are called binary strings or bit strings. • Strings are indispensable for processing textual data, defining computer languages and compiling programs written in them, and studying abstract computational models.
  • 10.
    Operations on array •Traversing: it means processing or visiting each element in the array exactly once. • Insertion: means to put values into an array • Deletion / Remove: to delete a value from an array.
  • 11.
    Operations on array •Sorting: Re-arrangement of values in an array in a specific order • (Ascending / Descending) is called sorting. • Searching: The process of finding the location of a particular element in an array is called searching. There are two popular searching techniques: • Linear search and binary search and will be
  • 12.
    Limitations of Arrays •An array has a fixed size which means you cannot add/delete elements after creation. • You also cannot resize them dynamically. • Unlike lists in Python, cannot store values of different data types in a single array.
  • 13.
    Linked List asan ADT Objectives –Learn about linked list – Be aware of basic properties of linked list – Explore the insertion and deletion operations on linked list – Discover how to build and manipulate a linked list – Learn how to construct a doubly linked list
  • 14.
    What is aList • A list is a finite sequence of data items, i.e., a collection of data items arranged in a certain linear order. Ways of storing list In memory: • sequential memory locations i.e store the list in arrays. • using pointers or links to associate elements sequentially i.e known as linked list.
  • 15.
    Linked Lists • Alinked list is a sequence of zero or more elements called nodes. • Each item in the list is called a node. • Each node contains two kinds of information: o some data and o one or more links called pointers to other nodes of the linked list.
  • 16.
    Linked Lists • Information-contains the item being stored in the list. • Next address- contains the address of the next item in the list. • The last node in the list contains NULL pointer to indicate that it is the end of the list.
  • 17.
    Linked List • Linkedlist is a very flexible dynamic data structure : • ie. items may be added to it or deleted from it at will. • program will have to accommodate in advance. • This allows robust programs which require much less maintenance.
  • 18.
    Types of Linkedlists • Singly Linked List : A singly linked list, or simply a linked list, is a linear collection of data items. •
  • 19.
    Singly Linked List •A singly linked list has the disadvantage that we can only traverse it in one direction. • Many applications require searching backwards and forwards through sections of a list.
  • 20.
    Doubly Linked List •DoublyLinked List permit traversing or searching of the list in both directions. •In this linked list each node contains three fields. – One to store data – Remaining are self referential pointers which points to previous and next nodes in the list
  • 21.
    Implementation of nodeusing structure • Method -1: struct node { int data; struct node *prev; struct node * next; };
  • 22.
    Implementation of nodeusing class • Method -2: class node { public: int data; node *prev; node * next; };
  • 23.
    Insertions operation • Toplace an elements in the list there are 3 cases  At the beginning  End of the list  At a given position
  • 24.
    Deletions operation Removing anelement from the list, without destroying the integrity of the list itself. To place an element from the list there are 3 cases : Delete a node at beginning of the list Delete a node at end of the list Delete a node at a given position
  • 26.
    Circularly Linked List •Simply circular list, is a linked list in which the last node always points to the first node. • It can be built by replacing the NULL pointer at the end of the list with a pointer which points to the first node. • There is no first or last node in the circular list.
  • 27.
    Advantages Any node canbe traversed starting from any other node in the list. There is no need of NULL pointer to signal the end of the list and hence, all pointers contain valid addresses. In contrast to singly linked list, deletion operation in circular list is simplified as the search for the previous node of an element to be deleted can be started from that item itself.
  • 28.
    Applications of theLinked List • It can be used to represent polynomials. • Using a linked list, we can perform the polynomial manipulation. • performed addition or subtraction of long integers arithmetic operations. • To implement stacks and queues.
  • 29.
    Limitations of LinkedList The linked allocation has the following draw backs: • No direct access to a particular element. • Additional memory required for pointers. • Searching must be done sequentially, however, when the list is large, it becomes time consuming.
  • 30.
    Basic Operations • 1.Initialize the list. • 2. Determine whether the list is empty. • 3. Print the list. • 4. Find the length of the list. • 5. Destroy the list
  • 31.
    Basic Operations • 16.Retrieve the info contained in the first node. • 7. Retrieve the info contained in the last node. • 8. Search the list for a given item. • 9. Insert an item in the list. • 10. Delete an item from the list. • 11. Make a copy of the linked list
  • 32.
    Operations of LinkedList • Operations on Doubly linked list:  Insertion of a node  Deletions of a node  Traversing the list
  • 33.
    Assignment 02 1. Writethe algorithm and a program to determine the median of the array given below: (9, 4, 5, 1, 7, 78, 22, 15, 96, 45,25) Note that the median of an array is the middle element of a sorted array. 2. Discuss 4 differences between Circular Array and Linked List