This document provides an introduction to data structures and linear data structures. It discusses what data and data structures are, why we need data structures, and common operations on data structures like traversal, search, insertion, and deletion. It then covers specific linear data structures like arrays, linked lists, stacks, and queues. For each, it provides class definitions and examples of implementation and common operations like insertion, deletion, traversal, etc. It also discusses some applications of different data structures.
1. INTRODUCTION
Data
A collection of facts, concepts, figures,
observation, instructions in a formalized manner.
What is Data Structure?
The way information is organized in the memory of a
computer is called as data structure.
A way of organizing, storing and retrieving data and
their relationship with each other.
Why do we need data structure?
Component reuse.
Data structure has been implemented once , it
can be used many applications.
1
Unit-3 Linear Data Structures
3. Operations on data structure
•Traversal
•Search
•Insertion
•Deletion
•Sorting
•Merging
3
Unit-3 Linear Data Structures
4. Data Structure Applications
•Operating System
•Compiler Design
•Network Analysis
•Expert Systems
•Data Base Management
Systems
•Statistical and Numerical
Analysis
4
Unit-3 Linear Data Structures
5. Abstract Data Types (ADTs)
An abstract data type (ADT) is a set of
operations. Abstract data types are
mathematical abstractions without
implementation details.
Objects such as lists, sets, and graphs, along
with their operations, can be viewed as abstract
data types, just as integers, real's, and Booleans
are data types. Integers, real's, and Booleans
have operations associated with them, and so
do abstract data types.
5
Unit-3 Linear Data Structures
6. List ADT
For any list except the null list, we say that ai+l
follows (or succeeds) ai (i < n) and that ai-1
precedes ai (i > 1). The first element of the list is
a1, and the last element is an.
6
Unit-3 Linear Data Structures
7. Operations on List ADT
•Insert and Delete -which generally insert
and delete some key from some position in
the list
•Find - returns the position of the first
occurrence of a key
•Find next – returns the position of the
successor element of the list.
•Find previous – returns the position of the
predecessor element of the list.
7
Unit-3 Linear Data Structures
8. Array based implementation
• An array implementation allows print_list and
find to be carried out in linear time, and the
find_kth operation takes constant time. However,
insertion and deletion are expensive.
• For example, inserting at position 0 (which
amounts to making a new first element) requires
first pushing the entire array down one spot to
make room, whereas deleting the first element
requires shifting all the elements in the list up
one, so the worst case of these operations is
linear in time.
Working
8
Unit-3 Linear Data Structures
9. Array List -Type declarations
class Array
{
private:
int MaxSize;
int A[20];
int Size;
public:
Array()
{
MaxSize=20;
Size=0; } 9
Unit-3 Linear Data Structures
10. void Read_Array();
void Display();
void Traverse_Backward();
void Insert(int Location,int Element);
void Delete(int Location);
int Search(int Element);
};
10
Unit-3 Linear Data Structures
11. void Array::Read_Array()
{
int i,N;
cout<<"Enter size of Array";
cin>>N;
if(N > MaxSize)
{
cout<< "Array of this size cannot be created";
cout<< " Maximum size is "<<MaxSize;
exit(0);
}
else
{
for(i=0;i<N;++i)
cin>>A[i];
Size=N;
}} 11
Unit-3 Linear Data Structures
12. void Array::Insert(int Location,int Element)
{
int i;
if(Size >= MaxSize)
{
cout<<"Overflow Error.....";
exit(0);
}
for(i=Size-1;i>=Location-1;i--)
A[i+1]=A[i];//Shifting element to right by one position.
A[Location -1]=Element;
Size=Size+1;
}
12
Unit-3 Linear Data Structures
13. void Array::Delete(int Location)
{
int i;
for(i=Location;i<Size;i++)
A[i-1]=A[i];//shifting elements to left by one position.
A[Size-1]=0;
Size=Size-1;
}
13
Unit-3 Linear Data Structures
15. void main()
{
clrscr();
Array A;
A.Read_Array();
cout<<"nContents of the arrarn";
A.Display();
cout<<"nTraversal in reverse directionn";
A.Traverse_Backward();
cout<<"nInsertion of an Element in the given positionn";
A.Insert(2,90);
A.Display();
cout<<endl;
cout<<"nDeletion of an Element";
A.Delete(2);
A.Display();
cout<<endl;
cout<<"nSearch for an element ";
cout<<A.Search(90);
cout<<A.Search(3);
} 15
Unit-3 Linear Data Structures
16. Linked List Implementation
• The linked list consists of a series of nodes,
which are not necessarily adjacent in memory.
Each node contains the element and a pointer
to a node containing its successor.
17. • The list contains five nodes, which happen to
reside in memory locations 1000, 800,712,
992, and 692 respectively.
• The next pointer in the first node has the
value 800, which provides the indication of
where the second node is. The other nodes
each have a pointer that serves a similar
purpose.
19. • The delete command can be executed in one
pointer change. The general idea is shown in
the illustration.
• The insert command requires obtaining a new
cell from the system by using an new call and
then executing two pointer maneuvers.
• Demo(Insert)
• Demo (Delete)
20. Class definition of List
class Node
{
public:
int data;
Node *link;
};
Unit-3 Linear Data Structures 20
27. Unit-3 Linear Data Structures 27
if(flag==1)
{
curr=temp->link;
temp->link=curr->link;
delete curr;
}
else
cout<<" Unable to locate the position"<<endl;
}
}
28. Polynomial Manipulation
• One can define an abstract data type for
single-variable polynomials (with nonnegative
exponents) by using a list.
• We could then write routines to perform
addition,subtraction,multiplication,differentiat
ion,and other operations on these
polynomials.
34. STACKS AND QUEUES
Stack ADT :( LIFO Lists)
•A stack is a data structure that operates with
the principle of Last In First Out (LIFO).
•A stack is a list with the restriction that
inserts and deletes can be performed in only
one position, namely the end of the list called
the top.
Demo
34
35. FUNDAMENTAL OPERATIONS
• The fundamental operations on a stack are
push, which is equivalent to an insert, and
pop, which deletes the most recently inserted
element.
• The most recently inserted element can be
examined prior to performing a pop by use of
the top routine.
35
39. Linked List Implementation of Stack
• The first implementation of a stack uses a
singly linked list. We perform a push by
inserting at the front of the list.
• We perform a pop by deleting the element at
the front of the list.
• A top operation merely examines the element
at the front of the list, returning its value.
39
40. Unit-3 Linear Data Structures 40
class Stack{
private:
struct Node{
T data;
Node *next;
};
typedef struct Node *node;
node top;
public:
Stack()
{
top=NULL;
}
void push(T element);
T pop(void);
T GetTop(void);
int Empty(void);
int CurrSize(void);
void display(void);
};
41. Unit-3 Linear Data Structures 41
template<class T>
void Stack<T>::push(T element)
{
node newnode=new Node;
newnode->data=element;
newnode->next=NULL;
if(Empty())
top=newnode;
else
{
newnode->next=top;
top = newnode;
}
}
42. Unit-3 Linear Data Structures 42
template<class T>
T Stack<T>::pop(void)
{
node temp;
T dataout;
if(Empty())
{
cout<<"Stack Underflow ..."<<endl;
return NULL;
}
else
{
dataout=top->data;
temp=top;
top=top->next;
delete temp;
return dataout;
}}
43. Unit-3 Linear Data Structures 43
template<class T>
int Stack<T>::Empty(void)
{
if(top==NULL)
return 1;
else
return 0;
}
template<class T>
T Stack<T>::GetTop(void)
{
if(Empty())
{
cout<<"Stack Underflow...."<<endl;
return NULL;
}
else
return top->data;}
44. Array Implementation of Stack
template<class T>
class Stack{
private:
T list[50];
int MaxCapacity;
int top;
public:
Stack()
{
MaxCapacity=50;
top=-1;
}
T getTop(void);
T pop(void);
void display(void);
void push(T element);
Unit-3 Linear Data Structures 44
45. Unit-3 Linear Data Structures 45
inline int IsFull()
{
if(top==MaxCapacity-1) return 1; else return 0;
}
inline int Empty()
{
if(top==-1) return 1;else return 0;
}
inline int CurrSize()
{
return(top+1);
}
};
46. Unit-3 Linear Data Structures 46
template<class T>
T Stack<T>::getTop(void)
{
if(!Empty())
return(list[top]);
else{
cout<<"Stack Underflow condition"<<endl;
return -1;
}
}
template<class T>
T Stack<T>::pop(void)
{
if(!Empty())
return(list[top--]);
else{
cout<<"Stack Underflow condition"<<endl;
return -1; }}
47. Unit-3 Linear Data Structures 47
template<class T>
void Stack<T>::push(T element)
{
if(!IsFull())
list[++top]=element;
else
cout<<"Stack Overflow Error";
}
template<class T>
void Stack<T>::display(void)
{
int i;
for(i=CurrSize()-1;i>=0;--i)
cout<<list[i]<<endl;
}
48. Applications Of Stack
• It is very useful to evaluate arithmetic expressions. (Postfix
Expressions)
• Infix to Postfix Transformation
• It is useful during the execution of recursive programs
• A Stack is useful for designing the compiler in operating
system to store local variables inside a function block.
• A stack (memory stack) can be used in function calls including
recursion.
• Reversing Data
• Reverse a List
• Convert Decimal to Binary
48
49. Queue ADT: (FIFO Lists)
• A queue operates with the principle of First in
First out (FIFO), Hence they are sometimes
called FIFO lists.
• The basic operations on a queue are enqueue,
which inserts an element at the end of the list
(called the rear), and dequeue, which deletes
(and returns) the element at the start of the
list (known as the front).
49
51. Array representation of queues
• we keep an array, QUEUE[], and the positions
q_front and q_rear, which represent the ends
of the queue.
• We also keep track of the number of elements
that are actually in the queue, q_size.
51
52. Enqueue and Dequeue Operations
• To enqueue an element x, we increment
q_size and q_rear, then set QUEUE[q_rear] =
x.
• To dequeue an element, we set the return
value to QUEUE[q_front], decrement q_size,
and then increment q_front.
• Demo
52
53. A Potential drawback
• There is one potential problem with this
implementation. After 10 enqueues, the queue
appears to be full, since q_front is now 10, and
the next enqueue would be in a nonexistent
position.
• The simple solution is that whenever q_front or
q_rear gets to the end of the array, it is wrapped
around to the beginning. This is known as a
circular array implementation.
• Demo
53
56. Class definition of Queue
#define MAX 5
template<class T>
class Cqueue
{
private:
int Rear,Front;
T Queue[MAX];
int Max;
int Size;
public:
Cqueue()
{
Size=0;
Max=MAX;
Rear=Front=0;
}
int
Empty(void){return(Size==0)
;}
Unit-3 Linear Data Structures
56
57. Unit-3 Linear Data Structures 57
int Full(void);
void Add(T Element);
T Delete(void);
T getFront(void);
void display(void);};
59. Unit-3 Linear Data Structures 59
if(Front==0)
Front=1;
if(Rear==MAX-1)
Rear=1;
else
Rear=Rear+1;
Queue[Rear]=Element;
Size=Size+1;
}
60. Deletion in circular queue
template<class T>
T Cqueue<T>::Delete(void)
{
T dataout;
if(Empty())
{
cout<<"Underflow
Error.."<<endl;
return NULL;
}
else
{
dataout=Queue[Front];
if(Front==Rear)
Front=Rear=0;
else
{
if(Front==MAX-1)
Front=1;
else
Front=Front+1;
}
}
Size=Size-1;
return dataout;
}
Unit-3 Linear Data Structures 60
61. Applications of Queues
• When jobs are submitted to a printer, they are arranged in
order of arrival. Thus, essentially, jobs sent to a line printer
are placed on a queue.
• Virtually every real-life line is (supposed to be) a queue. For
instance, lines at ticket counters are queues, because service
is first-come first-served.
• Another example concerns computer networks. There are
many network setups of personal computers in which the
disk is attached to one machine, known as the file server.
• Calls to large companies are generally placed on a queue
when all operators are busy.
61
62. Priority Queue
• A priority queue is a collection of elements
such that each element has been assigned a
priority and such that the order in which
elements are deleted and processed comes
from the following rules:
• An element of higher priority is processed
before any element of lower priority.
• Two elements with the same priority are
processed according to the order in which they
were added to the queue.
62
63. Priority queues-Types
Minimum Priority Queue
Collection of items into which item can be inserted
arbitrarily & from which only the Smallest item can
be removed.
Maximum Priority Queue
Collection of items into which item can be inserted
arbitrarily & from which only the largest item can
be removed.
63
64. Double Ended Queue
• A deque (short for double-ended queue) is an
abstract data structure for which elements can
be added to or removed from the front or back
(both end).
• This differs from a normal queue, where
elements can only be added to one end and
removed from the other.
• Both queues and stacks can be considered
specializations of deques, and can be
implemented using deques.
64
66. Evaluating an arithmetic expression
•Initialise an empty stack
•While token remain in the input stream
•Read next token
•If token is a number, push it into the stack
•Else, if token is an operator, pop top two
tokens off the stack,apply the operator, and
push the answer back into the stack
•Pop the answer off the stack
•Demo
Unit-3 Linear Data Structures 66