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

ITC - Lab 09

Here are the algorithms and C code to delete a number from any position in an array: [ALGORITHM] 1. Take input position 'pos' from where element needs to be deleted 2. Take input size of array 'n' 3. For loop from pos to n-1: a. Array[i] = Array[i+1] 4. Reduce size of array by 1 [C CODE] #include <stdio.h> void deleteElement(int array[], int size, int pos){ for(int i=pos; i<size-1; i++){ array[i] = array[i+1]; }

Uploaded by

aisha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ITC - Lab 09

Here are the algorithms and C code to delete a number from any position in an array: [ALGORITHM] 1. Take input position 'pos' from where element needs to be deleted 2. Take input size of array 'n' 3. For loop from pos to n-1: a. Array[i] = Array[i+1] 4. Reduce size of array by 1 [C CODE] #include <stdio.h> void deleteElement(int array[], int size, int pos){ for(int i=pos; i<size-1; i++){ array[i] = array[i+1]; }

Uploaded by

aisha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

CSCL-1108

INTRODUCTION
TO COMPUTER SCIENCE
DATA STRUCTURES
Data Structure

► Data Structure is defined as the way in which data is organized in the memory location.
There are 2 types of data structures:

Float
Primitive Data Structure

Primitive data types are predefined types of data, which are


supported by the programming language. For
example, integer, character, and boolean are all primitive data
types. Programmers can use these data types when creating
variables in their programs. For example, a programmer may
create a variable called “id" and define it as integer data type.
The variable will then store data as a integer.
Non-Primitive Data Structure

Non-primitive data types are not defined by the


programming language, but are instead created by
the programmer. They are sometimes called
"reference variables," or "object references," since
they reference a memory location, which stores the
data.
Linear Data Structure:

In linear data structure all the data are stored linearly in the memory.
All the data are saved in continuously memory locations and hence all
data elements are saved in one boundary. A linear data structure is one
in which we can reach directly only one element from another while
travelling sequentially. The main advantage, we find the first element,
and then it’s easy to find the next data elements. The disadvantage, the
size of the array must be known before allocating the memory.
Linear Data Structure:

The different types of linear data structures are:


►Array 12 15 18 14 13 20 21

tyu
►Stack (LIFO)
Qwe

►Queue (FIFO) 2 3 4 5 Asd

789
►Linked List
456

123
12 15 18 14 20
Xyz

Abc
Non-Linear Data Structure:

Every data item is attached to several other data items in a way that is
specific for reflecting relationships. The data items are not arranged in
a sequential structure. The various types of non-linear data structures
are: a

c d
►Trees b

►Graphs e f
Contiguous and Non-Contiguous Structures

►In contiguous structures, terms of data are kept together in memory. An


array is an example of a contiguous structure. Since each element in the
array is located next to one or two other elements.
►In contrast, items in a non-contiguous structure and scattered in memory,
but we link to each other in some way. A linked list is an example of a
non-contiguous data structure. Here, the nodes of the list are linked
together using pointers stored in each node.
Contiguous and Non-Contiguous Structures
Why to Learn Data Structure and Algorithms?

As applications are getting complex and data rich, there are three common problems that
applications face now-a-days.

Data Search − Consider an inventory of 1 million items of a store. If the application is to search
an item, it has to search an item in 1 million items every time slowing down the search. As data
grows, search will become slower.

Processor Speed − Processor speed although being very high, falls limited if the data grows to
billion records.

Multiple Requests − As thousands of users can search data simultaneously on a web server, even
the fast server fails while searching the data.
Why to Learn Data Structure and Algorithms?

To solve the mentioned problems, data structures come to


rescue. Data can be organized in a data structure in such a
way that all items may not be required to be searched, and
the required data can be searched almost instantly.
Applications of Data Structure and Algorithms

From the data structure point of view, following are some important categories of algorithms −

Search − Algorithm to search an item in a data structure.

Sort − Algorithm to sort items in a certain order.

Insert − Algorithm to insert item in a data structure.

Update − Algorithm to update an existing item in a data structure.

Delete − Algorithm to delete an existing item from a data structure.


Characteristics of a Data Structure

►Correctness − Data structure implementation should implement its


interface correctly.

►Time Complexity − Running time or the execution time of operations of


data structure must be as small as possible.

►Space Complexity − Memory usage of a data structure operation should


be as little as possible.
ARRAYS
Array is a container which can hold a fix number of items and these items should be of the
same type. Most of the data structures make use of arrays to implement their algorithms.
Following are the important terms to understand the concept of Array.

Element − Each item stored in an array is called an element.

Index − Each location of an element in an array has a numerical index, which is used to
identify the element.
Array Representation

Arrays can be declared in various ways in different languages. For


illustration, let's take C array declaration.
Array Representation

As per the above illustration, following are the important points to be considered.

► Index starts with 0.


► Array length is 10 which means it can store 10 elements.
► Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 27.
Basic Operations
Following are the basic operations supported by an array.

► Traverse − print all the array elements one by one.

► Insertion − Adds an element at the given index.

► Deletion − Deletes an element at the given index.

► Search − Searches an element using the given index or by the value.

► Update − Updates an element at the given index.


Insertion Operation

Insert operation is to insert one or more data elements into an


array. Based on the requirement, a new element can be added
at the beginning, end, or any given index of array.

Here, we see a practical implementation of insertion


operation, where we add data at the any position of the array
Algorithm
Let LA be a Linear Array (unordered) with “n”
elements and “pos” is a positive integer.
Following is the algorithm where VALUE is
inserted into the posth position of LA.
Algorithm
1. Start
2. Take input “n”
3. Set i = n
4. Repeat steps 5 and 6 while i >pos
5. Set LA[i+1] = LA[i]
6. Set i = i-1
7. Set LA[pos] = value
8. Stop
Insertion in Array
Insertion in Array
Tasks

►Write an algorithm to delete a number from


any position in an array.

►Write a code in C to delete a number from


any position in an array.

You might also like