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

Data Structures and Overview

A data structure organizes data in a computer program and specifies how it is accessed and processed. There are two types: primitive structures like integers that are directly operated on by machine instructions, and non-primitive structures like arrays and linked lists that are more sophisticated structures derived from primitive ones. Common operations on data structures include traversing, sorting, merging, searching, and insertion/deletion. The goals in designing data structures are correctness and efficiency.

Uploaded by

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

Data Structures and Overview

A data structure organizes data in a computer program and specifies how it is accessed and processed. There are two types: primitive structures like integers that are directly operated on by machine instructions, and non-primitive structures like arrays and linked lists that are more sophisticated structures derived from primitive ones. Common operations on data structures include traversing, sorting, merging, searching, and insertion/deletion. The goals in designing data structures are correctness and efficiency.

Uploaded by

srinidhi1956
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Data structures

1. What do you mean by data structure ?


Introduction to data structures:

A data structure is the relationship


between the data and programs that is traced at the beginning of
computing. In simple it is the way of organizing or representing the
data in the program.
The logical or mathematical model of a
particular organization of data is called data structures.
Data structure specifies the following:
1. Organization of data.
2. Accessing methods.
3. Degree of associativity.
4. Processing alternatives for information.

Goals of data structures:


1. Correctness.
2. Efficiency.

Classification of data structures:


1. Primitive data structure.
2. Non-primitive data structure.
Primitive data structure:
These are basic structures and are
directly operated upon the machine instructions. They can be
represented as integer, floating point, character constants, string
constants, pointers etc.
Non-primitive data structures:
1. These are more sophisticated data structures. These are
derived from the primitive data structures. The non-
primitive data structures emphasize on data structuring
of a group of homogeneous data items.
Ex: arrays, lists and files.

Data structure

Primitive data Non-primitive data


structure structure

Linear data non-linear data


Structure structure

Strings Trees

Arrays Graphs

Stacks

Linked lists
2. Explain the operations performed on data structures (7m)?

a. Creation operation:
This operation is used to create a DS.

b. Destroy operation:
Used to destroy a DS by using function free ( ).

c. Selection operation:
Used to access a data in DS.
d. Applied operation:
Is used data in a structure

Operations on non-primitive are:

a. Traversing:
Processing each element at least once.
b. Sorting:
Arranging the elements in ascending or descending order.
c. Merging:
Combining the element of two different sorted lists into
single sorted list.
d. Searching:
Finding the location of the element in the given T value.
e. Insertion & deletion:
Adding and removing the element to & from a data
structure.
3. Mention the goals of data structure (3m)?
Two fundamental goals, which are used in designing
high-level descriptions of data structures and algorithm, are:
*Correctness
*Efficiency
Correctness means that a data structure or algorithm is
designed at a high level to work correctly for all possible inputs
that one might encounter within a certain domain of interest.
Useful data structure and algorithm solutions also need to be
efficient i.e. they should be fast and not use more of the
computers resources than is necessary.

4. What are the implementation goals of data structure (2m)?


The implementation goals of data structure are:
*Robustness
*Adaptability
*Reusability

5. Explain the need of an algorithm (2m)?


*It is easy to understand code and debug.
*It makes use of computer resources efficiently.

6. Explain the complexity of algorithms (6m)?


The complexity of an algorithm is the function that gives
the running time or space in terms of the input size.

There are two parameters on which the efficiency of the


algorithms is measured
*Memory require
*Time taken
If algorithm takes very less time to produce the result and
it not wasting any memory space then it is considered as
efficient algorithm.

t f(n)

n ->

Suppose m is an algorithm and suppose n is the size of the input


data clearly the complexity f (n) of m increases as n increases.
The time is measured by counting the no of key operations.

7. Mention how the complexity is measured (3m)?


The complexity function f (n) is measured by three cases:
*Worst case
*Average case
*Best case
Worst case: the max value of f (n) for any possible input
f (n) =n
Average case: expected value of f (n)
f (n) = 1
Best case: minimum possible value of f (n).
f (n) =1.1/n+2.1/n+3.1/n ++ n.1/n
=1/n (1+2+3++n)
= (1/n) * n * (n+1) / 2
= n+1/2 =O (n).
8. Explain the big O notation (6m)?
DEF: we say f (n) is O (g (n)) if their exists two positive
constants k and n such /f (n)/ <= k/g (n)/ for n>n O-notation gives
an asymptotic upper board for a function within a constant factor
k (g (n)).
The big O-notation helps to characterize the asymptotic time or
space requirements of running algorithms.
The graphical representation of O-notation is as follows:

K (g (n)

F (n)

N0 n

In the big O-notation bubble sort can be expressed as O (n2). As n


increases, the complexity of the function f (n) of the algorithm M
also increases. But here, we are more concerned in examining the
rate of change of f(n).This is usually done by comparing f(n) with
some standard functions like log2n, n, nlog2n, n2, n3, 2n.
Big O-notations always give the worst case analysis, which
gives the max value of f (n) for all possible inputs.
9. Explain towers_of_Hanoi (5m)?
This is a puzzle begins with 3 poles &n disks where all
disks start on the left most pole A the disks each have a diff size
& are stacked in order of decreasing size with the largest disk with
the largest disk at the bottom .The objective of this game is to
move the disk from pole A to pole C in a series of steps
Step 1: move the top n-1 disks from pole A to pole B.
Step 2: move the top disk from pole A to pole C.
Step 3: move the top n-1 disks from pole B to pole C (using pole A
as the temporary pole).

A B C

10. Define data type (2m)?


Data type is a term, which refers to kinds that variables may
hold in a programming language.
The data types are integer, character, float and double.

11. Define strings and mention applications (3m)?


String is a linear data structure, which is useful for programs
involving non-numeric applications (sequence of zero or more
characters).
The applications are:
Used in word processing
Used in the construction of the programming language
12. What are the different operations on strings? Explain (10m)?
The different operations on strings are:
1. Finding the length of string:
Length operation: to find the number of characters in a given
string Length(s), where s is the given string, Length is the name of
the function.
NOTE: A string with zero character is called a null string or empty
string.

2. Concatenation operation:
This is used to join (concatenate) one or more strings to form
a single string.
In c it can be done by using the function strcat ().
Syntax: strcat (str1, str2)

3. Substring operation:
Extracting a portion of a string from a given string is called
sub string operation.
It requires 3 pieces of information:
*The given string
*Position of the first character of the sub string in the given string.
*Length of the sub string or the position of the last character of the
sub string
Syntax: sub string(s, k, l).

4. Indexing operation:
It refers to finding the position where a string pattern P first
appears in a given string text of T .It is also called as pattern
matching. We call this operation INDEX and write
Syntax: INDEX (T, P) where T is the text & P is the pattern.

5. Find and replace operation:


This will replace the first occurrence of pattern P1 with P2
in the text T. This can be written in the form of function.
Syntax: replace (T, P1, P2).

13. Mention the word processing operations (3m)?


The word processing operations are:
1. Insertions
2. Deletion
3. Replacing.

14. Explain the memory representation of strings with eg (10m)?


The strings can be represented in a memory by 3 methods: -
Fixed length method:
In a fixed length a string will be allocated fixed number of
storage locations. All the strings are stored in memory cells that are
all of same length. The actual amount of storage required by a
fixed length string is known when a string is created.
Eg: char str [10]
Str
0 12 3 4 5 6 7 8 9
Advantages:
*Easy to implement.
*Updating is easy provided length of updated data does not exceed
the fixed storage.

Disadvantages:
*Memory is not utilized efficiently.
*Length of the string should not exceed the allocated memory
Space.

Variable length with fixed maximum method:


Here the strings are stored in memory cells all of the same length,
however, one also knows the actual length of the string. There is an
information field which contains the length of the strings.
E.g.:
2@2@ 2 -> a a
Information field contains 3 ->
Length of the string 2 -> b a c
1 ->
ab
a

Advantages:
*The unoccupied space can be utilized by some other string and
hence memory wastage is minimized.
Disadvantages:
*Memory is wasted for information field.

Linked list method:


Here the storage in the form of a linked list. The characters are
stored in the data field of the node.
E.g.:
su ra na

Advantages:
*Modifications, such as insertion, deletions are easier.
*Efficient storage representation.
Disadvantages:
*Extra memory is required for the link field.
*One cannot directly access a character in the middle of the list.
15. WAP to find the length of the string using pointer variable.
#include<stdio.h>
main ()
{
Char ptr1, ptr2;
Ptr2=ptr1;
printf (enter a string);
while ((*ptr1=get char ())!=\n)
*Ptr1++;
*ptr1=\0;
printf(length=%d, ptr1-ptr2);
}

16. WAP to concatenation two strings using pointer variables.


#include<stdio.h>
main()
{
char *ptr1, *ptr2;
ptr2=ptr1;
printf(enter the first string);
while((*ptr1=get char ())!=\n)
Ptr1++;
printf(enter the second string);
While ((*ptr1=getchar ())! =\n)
Ptr1++;
*Ptr1=\0;
Printf (the concatenated string is: %s, ptr2);
}

17. WAP to extract a sub string from the given string.


#include<stdio.h>
main()
{
Int len, pos;
Char ptr1, ptr2, *ptr;
Ptr2=ptr1;
Printf (enter a string);
While ((*ptr1=getchar ())! =\n)
Ptr1++;
*Ptr1=\0;
Printf (enter position);
Scanf (%d, &pos);
Printf (enter the length of the sub string);
Scanf (%d, &len);
Printf (sub string is);
For (ptr=ptr2+pos-1; ptr<ptr2+pos+len-1; ptr++)
Putchar (*ptr);
}

18. WAP for the towers_of_Hanoi with N disks using recursive


function.
#include<stdio.h>
main()
{
Int n;
Printf (enter number of disks);
Scanf(%d&n);
towers_of_Hanoi(n,a,b,c);
}

Void towers_of_Hanoi (int n, char a, char b, char c)


{
If (n<=0)
Printf (illegal operation);
Else
If (n==1)
Printf (%c->%c, a, c);
Else
{
Towers_of_Hanoi (n-1, a, c, b);
Towers_of_Hanoi (1, a, b, c);
Towers_of_Hanoi (n-1, b, a, c);
}

You might also like