0% found this document useful (0 votes)
23 views27 pages

UNIT- 1 (IQ)

Data structure

Uploaded by

ruqsanabegum89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views27 pages

UNIT- 1 (IQ)

Data structure

Uploaded by

ruqsanabegum89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

UNIT 1

2 MARKS:

1. Infer about Data structures.

 A data structure is a storage that is used to store and organize


data.
 It is a way of arranging data on a computer so that it can be
accessed and updated efficiently.

Data Structure=Organized data + Allowed


operations

2. Define Abstract Data Types (ADTs) and list its types.

 Abstract data type (ADT) is a type (or class) for objects whose behavior
is defined by a set of values and a set of operations.

Types of ADTs:

 List ADT
 Stack ADT
 Queue ADT

3. Compare the Single, Double and circularly linked list.


Single linked list:
Single linked list is a sequence of elements in which every element has
link to its next element in the sequence.

Double linked list:


Double linked list is a sequence of elements in which every element
has links to its previous element and next element in the sequence.
Circularly linked list:
A circular linked list is a sequence of elements in which every element
has a link to its next element in the sequence and the last element has
a link to the first element.

4. What is Polynomial Manipulation?

 Polynomials: In mathematics, polynomials are mathematical


expressions consisting of in determinates (also called variables) and
coefficients, that involve only the operations of addition, subtraction,
multiplication, and positive-integer powers of variables. They are
used in various fields of mathematics, astronomy, economics, etc.
There are various examples of the polynomials such as 2x + 3, x2 +
4x + 5, etc.

5. List out the various operations used in data structures.

 Traversal
 Search
 Insertion
 Deletion
 Sorting
 Merge
 Create
 Selection
 Update
 Splitting
16 MARKS:

1. (a)Describe about Data structure and Classification with neat


diagram.

CLASSIFICATION OF DATA STRUCTURE:

PRIMITIVE DATA STRUCTURE:

 Primitive Data Structures are the data structures consisting of the


numbers and the characters that come in-built into programs.
 These data structures can be manipulated or operated directly by
machine-level instructions.
 These data types are also called Simple data types.
 Basic data types like Integer, Float, Character, and Boolean,
String come under the Primitive Data Structures.
Types are:

Integer: A data type used to represent whole numbers.

Float: A data type used to represent real numbers with fractional


parts.
Character: A data type used to represent single characters.

Boolean: A data type used to represent truth values (true or false).

String: A sequence of characters used to represent text.

NON-PRIMITIVE DATA STRUCTURE:

1. Non-Primitive Data Structures are those data structures derived


from Primitive Data Structures.
2. These data structures can't be manipulated or operated directly
by machine-level instructions.
3. The focus of these data structures is on forming a set of data elements
that is either homogeneous (same data
type)or heterogeneous (different data types).
4. Based on the structure and arrangement of data, we can divide these
data structures into two sub-categories -

 Linear Data Structures


 Non-Linear Data Structures

Linear Data Structures

A data structure that preserves a linear connection among its data elements
is known as a Linear Data Structure.

Linear Data Structures are classified into two types:

 Static Data Structures: The data structures having a fixed size


are known as Static Data Structures. Example: Array
 Dynamic Data Structures: The data structures having a
dynamic size are known as Dynamic Data Structures.
Example: Linked Lists, Stacks, and Queues.
Types of Linear Data Structures

The following is the list of Linear Data Structures that we generally use:

1. Arrays

An Array is a data structure used to collect multiple data elements of the


same data type into one variable.

Arrays can be classified into different types:

 One-Dimensional Array: An Array with only one row of data


elements is known as a One-Dimensional Array. It is stored in
ascending storage location.
 Two-Dimensional Array: An Array consisting of multiple rows and
columns of data elements is called a Two-Dimensional Array. It is also
known as a Matrix.
 Multidimensional Array: A multidimensional array is a data structure
consisting of a collection of elements organized in a grid or matrix
format with multiple indices to access its elements.

2. Linked Lists

A Linked List is another example of a linear data structure used to store a


collection of data elements dynamically. Data elements in this data structure
are represented by the Nodes, connected using links or pointers.
3. Stacks

 Stack is a Linear Data Structure that follows Last In First Out (LIFO)
principle.
 Insertion and deletion can be do neat only one end of the stack called
TOP of the stack.
Example:-Pile of coins, stack of trays.

The primary operations in the Stack are as follows:

 Push: Operation to insert a new element in the Stack is termed as


Push Operation.
 Pop: Operation to remove or delete elements from the Stack is termed
as Pop Operation.
4. Queues

A Queue is a linear data structure similar to a Stack with some limitations on


the insertion and deletion of the elements. The insertion of an element in a
Queue is done at one end, and the removal is done at another or opposite
end. Thus, we can conclude that the Queue data structure follows FIFO (First
In, First Out) principle to manipulate the data elements. Implementation of
Queues can be done using Arrays, Linked Lists, or Stacks.

The following are the primary operations of the Queue:

 Enqueue: The insertion or Addition of some data elements to the


Queue is called Enqueue. The element insertion is always done with
the help of the rear pointer.
 Dequeue: Deleting or removing data elements from the Queue is
termed Dequeue. The deletion of the element is always done with the
help of the front pointer.
Non-Linear Data Structures

Non-Linear Data Structures are data structures where the data elements are
not arranged in sequential order. Here, the insertion and removal of data are
not feasible in a linear manner. There exists a hierarchical relationship
between the individual data items.

Types of Non-Linear Data Structures

The following is the list of Non-Linear Data Structures that we generally use:

Trees:

A tree is a collection of nodes connected by directed (or undirected) edges. A


tree is a nonlinear data structure.

Figure 9. A Tree
2. Graphs

 A Graph is a non-linear data structure that consists of vertices (nodes)


and edges.
 A vertex, also called a node, is a point or an object in the Graph, and
an edge is used to connect two vertices with each other.
 Graphs are non-linear because the data structure allows us to have
different paths to get from one vertex to another, unlike with linear
data structures like Arrays or Linked Lists.

The Graph data structure, G is considered a mathematical structure


comprised of a set of vertices, V and a set of edges, E as shown below:

G = (V,E)

Figure 10. A Graph

The above figure represents a Graph having seven vertices A, B, C, D, E, F,


G, and ten edges [A, B], [A, C], [B, C], [B, D], [B, E], [C, D], [D, E], [D, F], [E,
F], and [E, G].

1. (b) Explain the Asymptotic Notations and its various types.


Asymptotic Notations

 Asymptotic notations are the mathematical notations used to describe


the running time of an algorithm when the input tends towards a
particular value or a limiting value.
 For example: In bubble sort, when the input array is already sorted, the
time taken by the algorithm is linear i.e. the best case.
 But, when the input array is in reverse condition, the algorithm takes
the maximum time (quadratic) to sort the elements i.e. the worst case.
 When the input array is neither sorted nor in reverse order, then it
takes average time. These durations are denoted using asymptotic
notations.

There are mainly three asymptotic notations:

 Big-O notation

 Omega notation

 Theta notation

Big-O Notation (O-notation)

Big-O notation represents the upper bound of the running time of an


algorithm. Thus, it gives the worst-case complexity of an algorithm.
Big-O gives the upper bound of a function

O(g(n)) = { f(n): there exist positive constants c and n0


such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }

The above expression can be described as a function f(n) belongs to the


set O(g(n)) if there exists a positive constant c such that it lies
between 0 and cg(n), for sufficiently large n.
For any value of n, the running time of an algorithm does not cross the time
provided by O(g(n)).
Since it gives the worst-case running time of an algorithm, it is widely used
to analyze an algorithm as we are always interested in the worst-case
scenario.
Omega Notation (Ω-notation)

Omega notation represents the lower bound of the running time of an


algorithm. Thus, it provides the best case complexity of an algorithm.

Omega gives the lower bound of a function

Ω(g(n)) = { f(n): there exist positive constants c and n0


such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }

The above expression can be described as a function f(n) belongs to the


set Ω(g(n)) if there exists a positive constant c such that it lies above cg(n),
for sufficiently large n.
For any value of n, the minimum time required by the algorithm is given by
Omega Ω(g(n)).
Theta Notation (Θ-notation)

Theta notation encloses the function from above and below. Since it
represents the upper and the lower bound of the running time of an
algorithm, it is used for analyzing the average-case complexity of an
algorithm.

Theta bounds the function within constants factors

For a function g(n), Θ(g(n)) is given by the relation:

Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0


such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
The above expression can be described as a function f(n) belongs to the
set Θ(g(n)) if there exist positive constants c1 and c2 such that it can be
sandwiched between c1g(n) and c2g(n), for sufficiently large n.
If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥ n0,
then f(n) is said to be asymptotically tight bound.

2. Interpret the List ADTs and its array-based implementation with C


program.

LIST ADT:

List is an ordered set of elements.

The general form of the list is A1 ,A2 , ……,AN

A1 - First element of the list

A2- 1st element of the list

N –Size of the list

If the element at position i is Ai, then its successor is Ai+1 and its
predecessor is Ai-1

Various operations performed on List

1. Insert (X, 5)- Insert the element X after the position 5.

2. Delete (X) - The element X is deleted

3. Find (X) - Returns the position of X.

4. Next (i) - Returns the position of its successor element i+1.

5. Previous (i) Returns the position of its predecessor i-1.

6. Print list - Contents of the list is displayed.

7. Make empty- Makes the list empty.

Implementation of list ADT:

1. Array based Implementation


2. Linked List based implementation

Array based Implementation:

Array:

A set of data elements of same data type is called array. Array is a static
data structure i.e., the memory should be allocated in advance and the size
is fixed. This will waste the memory space when used space is less than the
allocated space. An array implementation allows the following operations

The basic operations are:

a. Creation of a List.

b. Insertion of a data in the List

c. Deletion of a data from the List

d. Searching of a data in the list

Create Operation:

Create operation is used to create the list with „ n „ number of elements .If
„ n „ exceeds the arrays max size, then elements cannot be inserted into the
list. Otherwise the array elements are stored in the consecutive array
locations (i.e.) list [0], list [1] and so on.

Routine to create an element in the array:

void Create ( )

int i;

printf ("\n Enter the number of elements to be added in the list:\t");

scanf ("%d", &n);

printf ("\n Enter the array elements:\t");

for(i=0;i<n; i++)
scanf ("%d",& list[i]);

20 10 30 40 50 60

A[0] A[1] A[2] A[3] A[4] A[6]

Insert Operation:

Insert operation is used to insert an element at particular position in the


existing list. Inserting the element in the last position of an array is easy. But
inserting the element at a particular position in an array is quite difficult
since it involves all the subsequent elements to be shifted one position to the
right.

Routine to insert an element in the array:

void Insert( )
{
int i,data,pos;
printf("\nEnter the data to be inserted:\t");
scanf("%d",&data);
printf("\nEnter the position at which element to be inserted:\t");
scanf("%d",&pos);
if (pos==n)
printf (“Array overflow”);
for(i = n-1 ; i >= pos-1 ; i--)
list[i+1] = list[i];
list[pos-1] = data;
n=n+1;
Display();
}
Consider an array with 5 elements [ max elements = 10 ]

If data 15 is to be inserted in the 2 nd position, then 50 has to be moved to


next index position, 40 has to be moved to 50 position, 30 has to bemoved
to 40 position and 20 has to be moved to 30 position.

After this found at a movement, 15 is inserted in the 2 nd position of the


array.

10 15 20 30 40 50

If data 15 is to be inserted in the 2nd position, then 50 has to be moved to


next index position, 40 has to be moved to 50 position, 30 has to be moved
to 40 position and 20 has to be moved to 30 position.
Deletion Operation:

Deletion is the process of removing an element from the array at any


position.

Deleting an element from the end is easy. If an element is to be deleted from


any particular position,it requires all subsequent element from that position
is shifted one position towards left.

Routine to delete an element in the array:

void Delete( )
{
int i, pos ;
printf("\nEnter the position of the data to be deleted:\t");
scanf("%d",&pos);
printf("\nThe data deleted is:\t %d", list[pos-1]);
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1];
n=n-1;
Display();
}

Consider an array with 5 elements [ max elements = 10 ]

10 20 30 40 50

If data 20 is to be deleted from the array,then 30 has to be moved to data


20 position, 40 has to be moved to data 30 position and 50 has to be
moved to data 40 position.
After this 3 data movements, data 20 is deleted from the 2 nd position of the
array.

10 30 40 50

Search Operation:
Search ( ) operation is used to determine whether a particular element is
present in the list or not. Input the search element to be checked in the list.

Routine to search an element in the array:


void Search( )
{
int search,i,count = 0;
printf("\nEnter the element to be searched:\t");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(search == list[i])
count++;
}
if(count==0)
printf("\nElement not present in the list");
else
printf("\nElement present in the list");
}

Program for array based implementation in c:

#include <stdio.h>

int main() {

// Define an array

int arr[5] = {1, 2, 3, 4, 5};

// Print original array

printf("Original Array: ");

for (int i = 0; i < 5; i++) {

printf("%d ", arr[i]);

printf("\n");

// Insert element at index 2

int insertElement = 10;

for (int i = 4; i >= 2; i--) {

arr[i + 1] = arr[i];

arr[2] = insertElement;

printf("Array after insertion: ");

for (int i = 0; i < 6; i++) {


printf("%d ", arr[i]);

printf("\n");

// Delete element at index 2

for (int i = 2; i < 5; i++) {

arr[i] = arr[i + 1];

printf("Array after deletion: ");

for (int i = 0; i < 5; i++) {

printf("%d ", arr[i]);

printf("\n");

// Update element at index 2

int updateElement = 20;

arr[2] = updateElement;

printf("Array after update: ");

for (int i = 0; i < 5; i++) {

printf("%d ", arr[i]);

printf("\n");

// Search for element 3

int searchElement = 3;
int found = 0;

for (int i = 0; i < 5; i++) {

if (arr[i] == searchElement) {

found = 1;

break;

if (found) {

printf("Element found\n");

} else {

printf("Element not found\n");

// Sort array in ascending order

for (int i = 0; i < 5; i++) {

for (int j = i + 1; j < 5; j++) {

if (arr[i] > arr[j]) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

printf("Sorted array: ");

for (int i = 0; i < 5; i++) {


printf("%d ", arr[i]);

printf("\n");

return 0;

Output:

Original Array: 1 2 3 4 5

Array after insertion: 1 2 10 3 4 5

Array after deletion: 1 2 3 4 5

Array after update: 1 2 20 4 5

Element not found

Sorted array: 1 2 4 5 20

--------------------------------

Process exited after 0.08788 seconds with return value 0

Press any key to continue . . .

3. Summarize the Polynomial manipulation with routine for all


operation in polynomial manipulation.

Polynomials Definition

Polynomial is defined as the mathematical expression that are made up of


variables, constant in which the powers of variables are non-negative
integers. In mathematics a Polynomials is defined as,
P(x)=anxn+an−1xn−1+⋯+a1x+a0P(x)=anxn+an−1xn−1+⋯
+a1x+a0

 an,an−1,…,a1,a0an,an−1,…,a1,a0 are coefficients (which can be


real or complex numbers).
 x is the variable.
 n is a non-negative integer representing the degree of the
polynomial.
 an is the leading coefficient and a0a0 is the constant term.

Types of Polynomials:
Polynomials are classified based on the number of terms they contain, and
they can be divided into the following categories:
Monomial: A polynomial with only one term is called a Monomial, and it
must not be zero.
Examples: 2x, 5x3, 10
Binomial: A polynomial with two terms is called a binomial. It can be
expressed as the sum or difference of two or more monomials.
Examples: 3x4 + 4x, 5y3 + 3x, xy3 + 3y
Trinomial: A polynomial with three terms is referred to as a trinomial.
Examples: 5x2 + 4x + 10, 10y4 + 4x4 + 2x2, 7y2 + 3y + 10
Quadronomial: Polynomial containing 4 terms is called the Quadronomial.
Example: 3x2 + 2x + y3 – 11y, 13x3 – x + y2 – 4y, etc.

Operations on Polynomials:
There are four main polynomial operations which are:

 Addition of Polynomials
 Subtraction of Polynomials
 Multiplication of Polynomials
 Division of Polynomials

Addition of Polynomials
When adding polynomials, it is important to combine the like terms,
which means adding the terms that have the same variable and
exponent. Adding polynomials will always yield a polynomial of the
same degree as the original polynomials being added.

Example: Add the polynomials 3x2 + 2x + 1 and 2x2 – 4x + 3.


Solution:
Identify like terms,
 x2 are 3x2 and 2x2 are like terms.
 x are 2x and -4x are like terms.
 Constant terms are 1 and 3.
Combining the like terms, we have and add the coefficients of the like
terms
(3x2 + 2x2) + (2x – 4x) + (1 + 3)
Simplifying,
5x2 – 2x + 4
Therefore,
(3x2 + 2x + 1) + (2x2 – 4x + 3)
= 5x2 – 2x + 4

Subtraction of Polynomials

When it comes to subtracting polynomials, the process is similar to


addition, but with a different operation. You subtract the like terms to find
the solution. It’s important to remember that subtracting polynomials will
always result in a polynomial of the same degree.

Example: Subtraction the polynomial 2x2 + 3x – 5 from the


polynomial 4x2 – 2x + 7.
Solution:
(4x2 – 2x + 7) – (2x2 + 3x – 5)
= 4x2 – 2x + 7 – 2x2 – 3x + 5
= (4x2 – 2x2) + (-2x – 3x) + (7 + 5)
= 2x2 – 5x + 12

Multiplication of Polynomials

When two or more polynomials are multiplied together, the resulting


polynomial will generally have a higher degree than the original
polynomials, unless one of them is a constant polynomial.

Example: Multiply the polynomials (x + 2) and (x – 3).


Solution:
= (x + 2) × (x – 3)
= x × x + x × (-3) + 2 × x + 2 × (-3)
= x2 – 3x + 2x – 6
= x2 – x – 6

Division of Polynomials
It is an arithmetic operation by which a polynomial is divided by
another polynomial in a known as polynomial division. For this
operation to succeed, the divisor polynomial must have a degree that
is less than or equal to the dividend polynomial.
There are several ways to divide polynomials, some of which include:
 Long Division
 Synthetic Division
 Polynomial Division Using Factors

Routine for all operation in polynomial manipulation:


int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;
append (&poly1, 5, 2);
append(&poly1, 4, 1);
append(&poly1, 2, 0);
append(&poly2, 3, 1);
append(&poly2, 1, 0);

Node* sum = addPoly(poly1, poly2);


Node* diff = subtractPoly(poly1, poly2);
Node* prod = multiplyPoly(poly1, poly2);
Node* quotient = NULL;
Node* remainder = NULL;

dividePoly(poly1, poly2, &quotient, &remainder);

printf("Polynomial 1: ");
printPoly(poly1);
printf("Polynomial 2: ");
printPoly(poly2);

printf ("Sum: ");


printPoly (sum);
printf ("Difference: ");
printPoly (diff);
printf ("Product: ");
printPoly (prod);
printf ("Quotient: ");
printPoly(quotient);
printf ("Remainder: ");
printPoly(remainder);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy