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

Cos 211 Lecture Note

The document discusses two linear data structures: stacks and queues, explaining their operations and principles. Stacks operate on a LIFO basis with push and pop operations, while queues operate on a FIFO basis with enqueue and dequeue operations. Additionally, it covers sorting and searching techniques, highlighting the importance of sorting methods and comparing linear and binary search methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Cos 211 Lecture Note

The document discusses two linear data structures: stacks and queues, explaining their operations and principles. Stacks operate on a LIFO basis with push and pop operations, while queues operate on a FIFO basis with enqueue and dequeue operations. Additionally, it covers sorting and searching techniques, highlighting the importance of sorting methods and comparing linear and binary search methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

STACK AND QUEUE COS 211

By : Su leim an Abu bakar


STACK

 A Stack is linear data structure.


 A stack is a list of elements in which an element may be
inserted or deleted only at one end, called the top of
the stack.
 Stack principle is LIFO (last in, first out). Which element
inserted last on to the stack that element deleted first
from the stack.
 As the items can be added or removed only from the
top i.e. the last item to be added to a stack is the first
item to be removed.
Real life examples of stacks are:
Operations on stack:

 The two basic operations associated with stacks are:


 1. Push
 2. Pop

 While performing push and pop operations, the


following test must be conducted on the stack.
 a) Stack is empty or not
 b) stack is full or not
1. Push: Push operation is used to add new
elements in to the stack. At the time of addition
first check the stack is full or not. If the stack is full
it generates an error message "stack overflow".

2. Pop: Pop operation is used to delete elements


from the stack. At the time of deletion first check
the stack is empty or not. If the stack is empty it
generates an error message "stack underflow".
QUEUE
 A queue is linear data structure and collection of
elements.
 A queue is another special kind of list, where items are
inserted at one end called the rear and deleted at
the other end called the front.
 The principle of queue is a “FIFO” or “First-in-first-out”.
 Queue is an abstract data structure. A queue is a
useful data structure in programming.
 It is similar to the ticket queue outside a cinema hall,
where the first person entering the queue is the first
person who gets the ticket.
A real-world example of queue can be a single-lane one-
way road, where the vehicle enters first, exits first.
Queue example demonstrated
Operations on QUEUE

A queue is an object or more specifically an


abstract data structure (ADT) that allows the
following operations:
Enqueue or insertion: which inserts an element at
the end of the queue.
Dequeue or deletion: which deletes an element
at the start of the queue.
SORTING AND SEARCHING TECHNIQUES
SORTING
 Sorting is a technique of organizing the data. It is a process of
arranging the records, either in ascending or descending order i.e.
bringing some order lines in the data. Sort methods are very important
in Data structures.
 Sorting can be performed on any one or combination of elements
Example: suppose an array DATA contains 8 elements as follows:
 DATA: 70, 30,40,10,80,20,60,50.
 After sorting DATA must appear in memory as follows:
 DATA: 10 20 30 40 50 60 70 80
 Since DATA consists of 8 elements, there are 8!=40320 ways that the
numbers 10,20,30,40,50,60,70,80 can appear in DATA.
The factors to be considered while choosing
sorting techniques

Programming Time
Execution Time
Number of Comparisons
Memory Utilization
Computational Complexity
Types of Sorting Techniques:
 Internal Sorting: Internal sorting method is used when small
amount of data has to be sorted. In this method , the data
to be sorted is stored in the main memory (RAM).Internal
sorting method can access records randomly. EX: Bubble
Sort, Insertion Sort, Selection Sort, Shell sort, Quick Sort, Radix
Sort, Heap Sort etc.
 External Sorting: Extern al sorting method is used when large
amount of data has to be sorted. In this method, the data
to be sorted is stored in the main memory as well as in the
secondary memory such as disk. External sorting methods
an access records only in a sequential order. Ex: Merge
Sort, Multi way Mage Sort.
SEARCHING TECHNIQUES
 Searching is the process of finding some particular
element in the list. If the element is present in the list, then
the process is called successful and the process returns
the location of that element, otherwise the search is
called unsuccessful.
 There are two popular search methods that are widely
used in order to search some item into the list. However,
choice of the algorithm depends upon the arrangement
of the list.
 Linear Search
 Binary Search
LINEAR SEARCH / SEQUENTIAL SEARCH

Linear search, also called as sequential search,


is a very simple method used for searching an
array for a particular value. It works by
comparing the value to be searched with every
element of the array one by one in a sequence
until a match is found. Linear search is mostly
used to search an unordered list of elements
(array in which data elements are not sorted).
For example, if an array
A[] is declared and
initialized as,
int A[ ] = {10, 8, 1, 21, 7, 32,
5, 11, 0};
 And the value to be searched is VAL = 5, then searching means to
find whether the value ‘5’ is present in the array or not. If yes, then it
returns the position of its occurrence. Here, POS = 6 (index starting
from 0).
Advantages of a linear search
 Will perform fast searches of small to medium lists. With today's
powerful computers, small to medium arrays can be searched
relatively quickly.
 The list does not need to sorted. Unlike a binary search, linear
searching does not require an ordered list.
 Not affected by insertions and deletions. As the linear search does
not require the list to be sorted, additional elements can be added
and deleted. As other searching algorithms may have to reorder the
list after insertions or deletions, this may sometimes mean a linear
search will be more efficient.
Disadvantages of a linear search
 Slow searching of large lists. For example, when
searching through a database of everyone in the
Northern Ireland to find a particular name, it might be
necessary to search through 1.8 million names before
you found the one you wanted. This speed
disadvantage is why other search methods have been
developed.

You might also like