LIST,STACK,QUEUE
What is Algorithm? 
An algorithm is a set of rules for carrying out calculation 
either by hand or on a machine. 
Data structures 
Methods of organizing data 
Program = algorithms + data structures 
2 kinds of algorithm efficiencies 
 Space efficiency /space complexity 
 How much extra space the algorithm needs 
 Time efficiency / time complexity 
 How much time does it take to run the algorithm? 
 How fast the algorithm runs?
SET OF FUNCTIONS TO REPRESENT GROWTH 
RATES IN INCREASING ORDER OF MAGNITUDES: 
Function Name 
 c constant 
 lg n logarithmic 
 n linear 
 n lgn n lgn (between 
linear and quadratic) 
 n2 quadratic 
 n3 cubic 
 2n exponential 
 n! factorial
ABSTRACT DATA TYPES (ADT) 
 Definition of data 
 Definition of operations 
 Encapsulation of data & operation
LIST ADT 
 General Linear list 
 Operations possible –Insertion ,Deletion & Retrieval of 
an element 
 Implementations 
 Array 
 Linked list
ARRAY IMPLEMENTATION 
A0, A1, A2, ..., AN-1 
A0 A1 A2 An-1 
Operation: insertion 
A0 A1 A2 An-1 
A0 A’ A1 A2 An-1 An 
Operation: deletion 
A0 AA11 A2 An-1 
A0 A2 An-1
LINKED LIST 
 A linked list is a collection of nodes that together form a 
linear ordering. Each node is a compound object that 
stores an element and a reference, called next, to 
another node. 
Structure of a node 
A0 A1 A2 A3 
first 
Data Link
LINKED LIST TYPES 
1.Single linked lists : 
Each node contains two links – to the previous and to 
the next node 
2.Double linked lists : 
Each node contains a link only to the next node 
3.Circular lists: 
The tail is linked to the head.
The List operations: 
Insertion operation: 
Deletion operation: 
first 
20 10 30 11 
link 
data 
0 
80
Traversal: 
public int countNodes() 
{ 
int count = 0; 
Element e = head; 
while(e != null) 
{ 
count++; 
e = e.next; 
} 
return count; 
} 
A method that computes the number of elements in any 
list
Retrieval operation: 
public int countElement() 
{ 
int count = 0,num; 
Element e = head; 
while(e != null) 
{ 
if(e.data==num) 
count++; 
e = e.next; 
} 
return count; 
} 
A method that computes the number of occurance an 
element in any list
DOUBLY LINKED LIST 
 An ordered sequence of nodes in which each node 
has two pointers: left and right.
INSERTION OPERATION:
CIRCULAR LINKED LIST
CIRCULAR DOUBLY LINKED LIST 
 Add a head node at the left and/or right ends 
 In a non-empty circular doubly linked list: 
 LeftEnd->left is a pointer to the right-most node 
(i.e., it equals RightEnd) 
 RightEnd->right is a pointer to the left-most node 
(i.e., it equals LeftEnd)
STACK ADT 
 A stack is a restricted linear list in which all additions and 
deletions are made at one end, the top 
 This reversing attribute is why stacks are known as last in, 
first out (LIFO) data structures 
The stack operations 
Push operation: 
The push operation inserts an item at the top of the stack
Pop operation: 
The pop operation deletes the item at the top of the stack 
Empty operation: 
The empty operation checks the status of the stack. This 
operation returns true if the stack is empty and false if the 
stack is not empty.
STACK IMPLEMENTATIONS
QUEUE ADT 
 A queue is a linear list in which data can only be 
inserted at one end, called the rear, and deleted from 
the other end, called the front. In other words, a queue 
is a first in, first out (FIFO) structure. 
Operations on queues: 
 Enqueue operation: 
The enqueue operation inserts an item at the rear of the 
queue
Dequeue operation: 
The dequeue operation deletes the item at the front of the queue
QUEUE IMPLMENTATIONS
Data Structure -List Stack Queue

Data Structure -List Stack Queue

  • 1.
  • 2.
    What is Algorithm? An algorithm is a set of rules for carrying out calculation either by hand or on a machine. Data structures Methods of organizing data Program = algorithms + data structures 2 kinds of algorithm efficiencies  Space efficiency /space complexity  How much extra space the algorithm needs  Time efficiency / time complexity  How much time does it take to run the algorithm?  How fast the algorithm runs?
  • 3.
    SET OF FUNCTIONSTO REPRESENT GROWTH RATES IN INCREASING ORDER OF MAGNITUDES: Function Name  c constant  lg n logarithmic  n linear  n lgn n lgn (between linear and quadratic)  n2 quadratic  n3 cubic  2n exponential  n! factorial
  • 4.
    ABSTRACT DATA TYPES(ADT)  Definition of data  Definition of operations  Encapsulation of data & operation
  • 5.
    LIST ADT General Linear list  Operations possible –Insertion ,Deletion & Retrieval of an element  Implementations  Array  Linked list
  • 6.
    ARRAY IMPLEMENTATION A0,A1, A2, ..., AN-1 A0 A1 A2 An-1 Operation: insertion A0 A1 A2 An-1 A0 A’ A1 A2 An-1 An Operation: deletion A0 AA11 A2 An-1 A0 A2 An-1
  • 7.
    LINKED LIST A linked list is a collection of nodes that together form a linear ordering. Each node is a compound object that stores an element and a reference, called next, to another node. Structure of a node A0 A1 A2 A3 first Data Link
  • 8.
    LINKED LIST TYPES 1.Single linked lists : Each node contains two links – to the previous and to the next node 2.Double linked lists : Each node contains a link only to the next node 3.Circular lists: The tail is linked to the head.
  • 9.
    The List operations: Insertion operation: Deletion operation: first 20 10 30 11 link data 0 80
  • 10.
    Traversal: public intcountNodes() { int count = 0; Element e = head; while(e != null) { count++; e = e.next; } return count; } A method that computes the number of elements in any list
  • 11.
    Retrieval operation: publicint countElement() { int count = 0,num; Element e = head; while(e != null) { if(e.data==num) count++; e = e.next; } return count; } A method that computes the number of occurance an element in any list
  • 12.
    DOUBLY LINKED LIST  An ordered sequence of nodes in which each node has two pointers: left and right.
  • 13.
  • 14.
  • 15.
    CIRCULAR DOUBLY LINKEDLIST  Add a head node at the left and/or right ends  In a non-empty circular doubly linked list:  LeftEnd->left is a pointer to the right-most node (i.e., it equals RightEnd)  RightEnd->right is a pointer to the left-most node (i.e., it equals LeftEnd)
  • 16.
    STACK ADT A stack is a restricted linear list in which all additions and deletions are made at one end, the top  This reversing attribute is why stacks are known as last in, first out (LIFO) data structures The stack operations Push operation: The push operation inserts an item at the top of the stack
  • 17.
    Pop operation: Thepop operation deletes the item at the top of the stack Empty operation: The empty operation checks the status of the stack. This operation returns true if the stack is empty and false if the stack is not empty.
  • 18.
  • 19.
    QUEUE ADT A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front. In other words, a queue is a first in, first out (FIFO) structure. Operations on queues:  Enqueue operation: The enqueue operation inserts an item at the rear of the queue
  • 20.
    Dequeue operation: Thedequeue operation deletes the item at the front of the queue
  • 21.