DSA 1 (Concept of Data Structures)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Unit 1: Concept of data structures

• Introduction: data types, data structures and abstract data types


• Introduction to algorithms

What is data structure?


- Data structure is a way of organizing all data items and establishing relationship among those data
items.
- Data structures are the building blocks of a program.
Data structure mainly specifies the following four things:
• Organization of data.
• Accessing methods
• Degree of associativity
• Processing alternatives for information

To develop a program of an algorithm, we should select an appropriate data structure for that
algorithm. Therefore algorithm and its associated data structures form a program.

Algorithm + Data structure = Program

A static data structure is one whose capacity is fixed at creation. For example, array. A dynamic data
structure is one whose capacity is variable, so it can expand or contract at any time. For example, linked
list, binary tree etc.

Abstract Data Types (ADTs)


An abstract data type is a data type whose representation is hidden from, and of no concern to the
application code. The definition of ADT only mentions what operations are to be performed but not how
these operations will be implemented. It does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations. It is called “abstract” because it gives an
implementation-independent view. The process of providing only the essentials and hiding the details is
known as abstraction.

The user of data type does not need to know how that data type is implemented, for example, we have
been using Primitive values like int, float, char data types only with the knowledge that these data type
can operate and be performed on without any idea of how they are implemented. So a user only needs
Compiled by: Er. Sandesh S. Poudel
to know what a data type can do, but not how it will be implemented. Think of ADT as a black box
which hides the inner structure and design of the data type.

For example, when writing application code, we don’t care how strings are represented: we just declare
variables of type String, and manipulate them by using string operations.

Once an abstract data type has been designed, the programmer responsible for implementing that type
is concerned only with choosing a suitable data structure and coding up the methods. On the other hand,
application programmers are concerned only with using that type and calling its methods without
worrying much about how the type is implemented.

Compiled by: Er. Sandesh S. Poudel


Classification of data structure:
Data structure

Primitive Non-
Data structure Primitive
Data structure

Integer Float Character Pointer Array List File

Linear list Non-linear list


Stack
Trees

Queue
Graphs

Fig:- Classification of data structure

Primitive data structure is a fundamental type of data structure that stores the data of only one type whereas the
non-primitive data structure is a type of data structure which is a user-defined that stores the data of different
types in a single entity

Algorithm:
An algorithm is a precise specification of a sequence of instructions to be carried out in order to
solve a given problem. Each instruction tells what task is to be done. There should be a finite
number of instructions in an algorithm and each instruction should be executed in a finite amount
of time.
Properties of Algorithms:
• Input: A number of quantities are provided to an algorithm initially before the algorithm begins. These
quantities are inputs which are processed by the algorithm.
• Definiteness: Each step must be clear and unambiguous.
Compiled by: Er. Sandesh S. Poudel
• Effectiveness: Each step must be carried out in finite time.

• Finiteness: Algorithms must terminate after finite time or step

• Output: An algorithm must have output.


• Correctness: Correct set of output values must be produced from the each set of inputs.

Write an algorithm to find the greatest number among three numbers:

Step 1: Read three numbers and store them in X, Y and Z

Step 2: Compare X and Y. if X is greater than Y then go to step 5 else step 3

Step 3: Compare Y and Z. if Y is greater than Z then print “Y is greatest” and go to step 7 otherwise
go to step 4
Step 4: Print “Z is greatest” and go to step 7

Step 5: Compare X and Z. if X is greater than Z then print “X is greatest” and go to step 7 otherwise
go to step 6
Step 6: Print “Z is greatest” and go to step 7

Step 7: Stop

Lab work
Write a program using an array to perform the following tasks:(use switch case for menu)
a. Insert element into an array at specified position
b. Delete element from an array
c. Traversing
d. Searching a particular element in the array
#include<stdio.h>
#include<conio.h>
void insert(int [100], int*);
void delet(int [100], int*);
void traverse(int [100], int*);
void searching(int [100], int*);
void main()
{
int a[100],nel,pos,i;

Compiled by: Er. Sandesh S. Poudel


int n;
int choice;
printf("Enter no of elements to be inserted");
scanf("%d", &n);
printf("Enter %d elements", n);
for(i=0;i<n;i++)
{ scanf("%d", &a[i]);
} do
{ printf("\nmanu for program:\n");
printf("1:insert\n2:delete\n3:Traverse\n4:searching\n5:exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert(a,&n);
break;
case 2:
delet(a,&n);
break;
case 3:
traverse(a,&n);
break;
case 4:
searching(a,&n);
break;
case 5:
exit(1);
break;
default:
printf("Invalied choice");
}
}while(choice<6);
}

void insert(int a[100], int *n)


Compiled by: Er. Sandesh S. Poudel
{
int pos, nel, i;
printf("Enter position at which you want to insert new element");
scanf("%d", &pos);
printf("Enter new element");
scanf("%d", &nel);
for(i=*n-1; i>=pos; i--)
{
a[i+1] = a[i];
} a[pos]=nel;
*n=*n+1;
printf("New array is:\n");
for(i=0; i<*n; i++)
{
printf("%d\t", a[i]);
}
}

void delet(int a[100], int *n)


{
int pos, i;
printf("Enter position at which you want to delete an element");
scanf("%d", &pos);
for(i=pos; i<*n; i++)
{
a[i] = a[i+1];
}
*n=*n-1;
printf("New array is:\n");
for(i=0; i<*n; i++)
{
printf("%d\t", a[i]);
}
}

void traverse(int a[100], int *n)

Compiled by: Er. Sandesh S. Poudel


{
int i;
printf("Elements of array are:\n");
for(i=0;i<*n;i++)
{
printf("%d\t",a[i]);
}
}

void searching(int a[100], int *n)


{
int k,i;
printf("Enter searched item");
scanf("%d",&k);
for(i=0;i<*n;i++)
{
if(k==a[i])
{ printf("******successful search******"); break;
}
} if(i==*n)
printf("*********unsuccessful search*********");
}

Compiled by: Er. Sandesh S. Poudel

You might also like