Data Structure Er. Inderjeet Bal

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

DATA

STRUCTURE

Er. Inderjeet Bal


Dept. of CS & IT
DATA
STRUCTURE
• It is a logical way of storing data and it also
defi ne mechanism of retrieve data.
• A data structure is a collection of data values, the
relationships among them, and the functions or
operations that can be applied to the data.
• Data structures are generally based on the ability
of a computer to fetch and store data at any place
in its memory, specified by a pointer—a bit string,
representing a memory address, that can be itself
stored in memory and manipulated by the
program.
TYPES OF DATA STRUCTURE
OPERATIONS

Traversing: Accessing each record


exactly once so that
certain item in the record may be
processed.
Searching: finding the location of the
record with a given
key value .
Insertion : add a new record to the
structure
LINEAR DATA
STRUCTURE

1. Array

2. Stack

3. Queue

4. Linked List
ARRAY
ONE-DIMENSIONAL ARRAYS

A list of values with the same data


type that are stored using a single
group name (array name).
General array declaration
statement:
data-type array-name[number-of-
items];
The number-of-items must be
specifi ed before declaring the array.
const int SIZE = 100;
float arr[SIZE];
ONE-DIMENSIONAL ARRAYS
(CONT.)

Individual elements of the array can be accessed by


specifying the name of the array and the element's
index: arr[3]
Warning: indices assume values from 0 to number-
of-items -1!!
ONE-DIMENSIONAL ARRAYS (CONT.)

The array name arr


identifies the starting
location of the array

arr[0] arr[1] arr[2] arr[3] arr[4]

element 3

Skip over 3 elements to


get the starting
location of element 3

Start
here
1D ARRAY INITIALIZATION

Arrays can be initialized during their


declaration
int arr[5] = {98, 87, 92, 79, 85};
int arr[5] = {98, 87} - what happens in
this case??
What is the difference between the
following two declarations ?
codes[0] codes[1] codes[2] =codes[3]
char codes[] codes[4]
{'s', 'a', codes[5]
'm', 'p', codes[6]
'l', 'e'};
s a m codes[]
char p = "sample";
l e \0
TWO-DIMENSIONAL ARRAYS

A two-dimensional array consists of both


rows and columns of elements.

General array declaration statement:


data-type array-name[number-of-rows]
[number-of-columns];
TWO-DIMENSIONAL ARRAYS
(CONT.)

The number-of-rows and number-of-


columns must be specified before
declaring the array.
const int ROWS = 100;
const int COLS = 50;
float arr2D[ROWS][COLS];
Individual elements of the array can be
accessed by specifying the name of the
array and the element's row, column
indices.
2D ARRAY INITIALIZATION

Arrays can be initialized during their


declaration
int arr2D[3][3] = { {98, 87, 92},
{79, 85, 19}, {32, 18, 2}
};
The compiler fills the array row by row
(elements are stored in the memory in
the same order).
1D ARRAYS AS ARGUMENTS

Individual array elements are passed to


a function in the same manner as other
variables.
max = find_max(arr[1], arr[3]);
To pass the whole array to a function,
you need to specify the name of the
array only!!
LINKED LISTS

A B C 

Head

A linked list is a series of


connected nodes
Each node contains at least
A piece of data (any type)
Pointer to the next node in the list
Head: pointer to the first node
node
The last node points to NULL
A

dat pointe
a r
WHAT IS A STACK?

It is an ordered group of homogeneous items of


elements.
Elements are added to and removed from the top of
the stack (the most recently added items are at the
top of the stack).
The last element to be added is the first to be
removed (LIFO: Last In, First Out).
STACK SPECIFICATION

Defi nitions: (provided by the user)


MAX_ITEMS: Max number of items that might
be on the stack
ItemType: Data type of the items on the stack

Operations
MakeEmpty
Boolean IsEmpty
Boolean IsFull
Push (ItemType newItem)

PUSH (ITEMTYPE NEWITEM)

Function: Adds newItem to the top


of the stack.
Preconditions: Stack has been
initialized and is not full.
Postconditions: newItem is at the
top of the stack.
POP (ITEMTYPE& ITEM)

Function: Removes topItem from stack and returns it in


item.

Preconditions: Stack has been initialized and is not


empty.

Postconditions: Top element has been removed from


stack and item is a copy of the removed element.
Queue
• A queue is a linier data structure. The concept is quite similar
with stack.
• Additions are made at the end or tail of the queue while
removals are made from the front or head of the queue.
• Access system a queue is referred to a FIFO structure (First-In
First-Out)
Queue operation
• Add: adds a new node
Add(X,Q)  add the value X to the tail of queue
• Remove : removes a node
Remove(Q)  removes the head node and returns its value
• IsEmpty : reports whether the queue is empty
IsEmpty(Q)  report whether the queue Q is empty
• IsFull : reports whether the queue isfull
IsFull(Q)  report whether the queue Q is full
• Initialize : creates/initializes the queue
Initialize(Q)  create a new empty queue named Q
• Destroy : deletes the contents of the queue (may be implemented by
reinitializing the queue)
Destroy(Q)  deletes the contents of the queue Q
Thanks

You might also like