SlideShare uma empresa Scribd logo
1 de 67
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
Classification of Data Structure
2
Unit-3 Linear Data Structures
Operations on data structure
•Traversal
•Search
•Insertion
•Deletion
•Sorting
•Merging
3
Unit-3 Linear Data Structures
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
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
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
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
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
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
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
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
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
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
int Array::Search(int Element)
{
int i;
for(i=0;i<Size;++i)
{
if(Element==A[i])
return i;
}
return -1;
}
14
Unit-3 Linear Data Structures
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
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.
• 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.
Insertion and Deletion
• Deletion from a linked list
• Insertion into a linked list
• 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)
Class definition of List
class Node
{
public:
int data;
Node *link;
};
Unit-3 Linear Data Structures 20
Unit-3 Linear Data Structures 21
class Llist
{
private:
Node *Head,*Tail;
public:
Llist()
{
Head=NULL;
}
~Llist();
void Create();
void Display();
Node *GetNode();
void Append(Node *NewNode);
void Insert_at_Pos(Node *NewNode,int position);
void DeleteNode(int del_position);
};
Unit-3 Linear Data Structures 22
void Llist::Create()
{
char ans;
Node *NewNode;
if(Head==NULL)
{
cout<<"Enter the first value"<<endl;
NewNode=GetNode();
Append(NewNode);
}
while(1)
{
cout<< "Any more nodes to be added (Y/N)";
cin >>ans;
if(ans=='n') break;
NewNode=GetNode();
Append(NewNode);
} }
Unit-3 Linear Data Structures 23
void Llist::Append(Node *NewNode)
{
if(Head==NULL)
{
Head=NewNode;
Tail=NewNode;
}
else
{
Tail->link=NewNode;
Tail=NewNode;
}
}
Node *Llist::GetNode() // Function for allocating memory space
{
Node *NewNode;
NewNode= new Node;
cin >> NewNode->data;
NewNode->link=NULL;
return(NewNode);
}
Unit-3 Linear Data Structures 24
void Llist::Insert_at_Pos(Node *NewNode,int position)
{
Node *temp=Head;
int count=1,flag=1;
if(position==1)
{
NewNode->link=temp;
Head=NewNode;
}
else
{
while(count!=position -1)
{
temp=temp->link;
if(temp==NULL)
{
flag=0; break;
}
count++;
}
Unit-3 Linear Data Structures 25
if(flag==1)
{
NewNode->link=temp->link;
temp->link=NewNode;
}
else
cout<<"Unable to locate position"<<endl;
}
}
Unit-3 Linear Data Structures 26
void Llist::DeleteNode(int pos)
{
int count=1,flag=1;
Node *curr,*temp;
temp=Head;
if(pos==1)
{
Head=Head->link;
delete temp;
}
else
{
while(count != pos -1)
{
temp=temp->link;
if(temp==NULL)
{
flag=0;break;
}
count++;
}
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;
}
}
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.
Polynomial Manipulation
Initialization
Procedure for adding polynomial
Polynomial Multiplication
Polynomial Multiplication(contd)
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
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
Stack model
• Stack model: input to a stack is by push,
output is by pop
36
Unit-3 Linear Data Structures 37
Unit-3 Linear Data Structures 38
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
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);
};
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;
}
}
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;
}}
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;}
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
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);
}
};
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; }}
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;
}
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
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
Model of a Queue
50
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
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
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
Queue functioning
54
55
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
Unit-3 Linear Data Structures 57
int Full(void);
void Add(T Element);
T Delete(void);
T getFront(void);
void display(void);};
Unit-3 Linear Data Structures 58
template<class T>
int Cqueue<T>::Full(void)
{
if(((Rear==MAX-
1)&&(Front==1))||((Rear!=0)&&(Front==Rear+1)))
return 1;
else
return 0;
}
template<class T>
void Cqueue<T>::Add (T Element)
{
if(Full()){
cout<<"OverFlow Error.."<<endl;
return;
}
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;
}
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
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
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
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
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
Double ended queue
65
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
Unit-3 Linear Data Structures 67

Mais conteúdo relacionado

Semelhante a 12888239 (2).ppt

Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Getachew Ganfur
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructuresNguync91368
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTSwapnil Mishra
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxsarala9
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUEDev Chauhan
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxSaralaT3
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using Ccpjcollege
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structuresDeepa Rani
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queuesurya pandian
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxSTACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxsunitha1792
 

Semelhante a 12888239 (2).ppt (20)

linkedLists.ppt
linkedLists.pptlinkedLists.ppt
linkedLists.ppt
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
03-Lists.ppt
03-Lists.ppt03-Lists.ppt
03-Lists.ppt
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUE
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structures
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxSTACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
 

Último

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Último (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

12888239 (2).ppt

  • 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
  • 2. Classification of Data Structure 2 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
  • 14. int Array::Search(int Element) { int i; for(i=0;i<Size;++i) { if(Element==A[i]) return i; } return -1; } 14 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.
  • 18. Insertion and Deletion • Deletion from a linked list • Insertion into a linked list
  • 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
  • 21. Unit-3 Linear Data Structures 21 class Llist { private: Node *Head,*Tail; public: Llist() { Head=NULL; } ~Llist(); void Create(); void Display(); Node *GetNode(); void Append(Node *NewNode); void Insert_at_Pos(Node *NewNode,int position); void DeleteNode(int del_position); };
  • 22. Unit-3 Linear Data Structures 22 void Llist::Create() { char ans; Node *NewNode; if(Head==NULL) { cout<<"Enter the first value"<<endl; NewNode=GetNode(); Append(NewNode); } while(1) { cout<< "Any more nodes to be added (Y/N)"; cin >>ans; if(ans=='n') break; NewNode=GetNode(); Append(NewNode); } }
  • 23. Unit-3 Linear Data Structures 23 void Llist::Append(Node *NewNode) { if(Head==NULL) { Head=NewNode; Tail=NewNode; } else { Tail->link=NewNode; Tail=NewNode; } } Node *Llist::GetNode() // Function for allocating memory space { Node *NewNode; NewNode= new Node; cin >> NewNode->data; NewNode->link=NULL; return(NewNode); }
  • 24. Unit-3 Linear Data Structures 24 void Llist::Insert_at_Pos(Node *NewNode,int position) { Node *temp=Head; int count=1,flag=1; if(position==1) { NewNode->link=temp; Head=NewNode; } else { while(count!=position -1) { temp=temp->link; if(temp==NULL) { flag=0; break; } count++; }
  • 25. Unit-3 Linear Data Structures 25 if(flag==1) { NewNode->link=temp->link; temp->link=NewNode; } else cout<<"Unable to locate position"<<endl; } }
  • 26. Unit-3 Linear Data Structures 26 void Llist::DeleteNode(int pos) { int count=1,flag=1; Node *curr,*temp; temp=Head; if(pos==1) { Head=Head->link; delete temp; } else { while(count != pos -1) { temp=temp->link; if(temp==NULL) { flag=0;break; } count++; }
  • 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.
  • 31. Procedure for adding polynomial
  • 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
  • 36. Stack model • Stack model: input to a stack is by push, output is by pop 36
  • 37. Unit-3 Linear Data Structures 37
  • 38. Unit-3 Linear Data Structures 38
  • 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
  • 50. Model of a Queue 50
  • 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
  • 55. 55
  • 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);};
  • 58. Unit-3 Linear Data Structures 58 template<class T> int Cqueue<T>::Full(void) { if(((Rear==MAX- 1)&&(Front==1))||((Rear!=0)&&(Front==Rear+1))) return 1; else return 0; } template<class T> void Cqueue<T>::Add (T Element) { if(Full()){ cout<<"OverFlow Error.."<<endl; return; }
  • 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
  • 67. Unit-3 Linear Data Structures 67