Download UNIT –I LINEAR STRUCTURES 1. What are the main objectives of

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Array data structure wikipedia , lookup

Linked list wikipedia , lookup

Transcript
UNIT –I
LINEAR STRUCTURES
1. What are the main objectives of Data structure?
To identify and create useful mathematical entities and operations to determine what classes of
problems can be solved by using these entities and operations.
To determine the representation of these abstract entities and to implement the abstract
operations on these concrete representation.
2. Need of a Data structure?
To understand the relationship of one data elements with the other and organize it within the
memory.
A data structures helps to analyze the data, store it and organize it in a logical or mathematical
manner.
3. Define data structure with example.
A data structure is a way of organizing data that considers not only the items stored but also
their relationship to each other.
e.g.: Arrays, Records etc.
e.g of complex data structure
Stacks, Queues, Linked list, Trees, Graphs.
4. Define abstract data type and list its advantages.
ADT is a set of operations. An abstract data type is a data declaration packaged together with the
operations that are meaning full on the data type.
Abstract Data Type
1. Declaration of data
2. Declaration of operations.
Advantages:
1. Easy to debug small routines than large ones.
2. Easy for several people to work on a modular program simultaneously.
3. A modular program places certain dependencies in only one routine, making changes easier.
5. What are the two basic types of Data structures?
1. Primitive Data structure
Eg., int,char,float
2. Non Primitive Data Structure
i. Linear Data structure Eg., Lists Stacks Queues
ii. Non linear Data structure Eg., Trees Graphs
6. What are the four basic Operations of Data structures?
1. Traversing
2. Searching
3. Inserting
4. Deleting
7.Define Recursion?
The process of calling a function by itself is known as recursion. Recursion is generally
used when the result of the current function call is the input to the successive call of itself. For
example, ‘factorial of a digit’.
The factorial f(n) = n * (n-1) * (n-2) * .. 1.
For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the
factorial of the parameter n.
int factorial( int n)
{
if (n==1)
return 1
else
return n* factorial( n-1) ;
}
int value=factorial(6) ;
8.Define Dynamic Programming
Dynamic programming is a method for solving complex problems by breaking them
down into simpler sub problems. It is applicable to problems exhibiting the properties of
overlapping sub problems and optimal substructure ( sub problems can be nested recursively
inside larger problems, then there is a relation between the value of the larger problem and the
values of the sub problems). When applicable, the method takes far less time than naive methods
that don't take advantage of the sub problem overlap.
(ie)Dynamic programming is when you use past knowledge to solve a future problem easier.
A good example is solving the fibonacci sequence for n=1,000,002.
This will be a very long process, but what if I give you the results for n=1,000,000 and
n=1,000,0001? Suddenly the problem just becomes more manageable.
Dynamic programming is used a lot in string problems, such as the string edit problem.
9.Define Dynamic Programming Language?
Dynamic programming languages are often refered to as 'weakly typed' which means that
variables are not bound to a particular type until runtime. For example, C++ is a 'strongly typed
language, while Perl is a 'weakly typed' or 'dynamic' language.
A strongly typed language requires that you specify a type:
int i = 0;
In that C++ snippet, an integer names i is created and assigned the value of 0. The following
would not compile in C++:
int i = 0;
i = "Hello world";
This would fail because i has been declared to be an integer and can not be assigned a string
value. The following code would succeed in a dynamic language like Perl:
i = 0;
i = "Hello world";
The type is not declared here at all, because it is not bound to a type until runtime. In the first
statement, i is bound to an integer type but in the second statement it is bound to a string type.
While dynamic language are easier to program in, they are much slower than strongly typed
languages and are generally regarded as less safe since checking is not done until run time.
10. List out the different ways to implement the list?
1. Array Based Implementation
2. Linked list Implementation
i. Singly linked list
ii. Doubly linked list
iii. Cursor based linked list
11. Define singly linked list with neat diagram.
A singly linked list is a collection of nodes each node is a structure it consisting of an element
and a pointer to a structure containing its successor, which is called a next pointer.
The last cell’s next pointer points to NULL specified by
zero.
12. List the operations of single linked list.
MakeEmpty
IsEmpty
IsLast
Find
Delete
FindPrevious
Insert
Deletelist
Header
First
Advance
Retrieve
13. Write the routine for insertion operation of singly linked list.
/* Insert (after legal position P)*/
/* Header implementation assumed*/
/* Parameter L is unused in this implementation*/
Void Insert (ElementType X, List L, Position P)
{
Position TmpCell;
TmpCell=malloc(sizeof(struct Node));
if(TmpCell==NULL)
FatalError(“Out of space!!!”);
TmpCell->Element =X;
TmpCell->Next=P->Next;
P->Next=TmpCell;
}
14. Write the routine for deletion operation of singly linked list.
/* Delete first occurrence of x from a list*/
/* Assume use of a header node*/
Void Delete(ElementType X, List L)
{
Position P, TmpCell;
P=FindPrevious(X, L);
if (! IsLast(P,L))
{
TmpCell=P->Next;
P->Next=TmpCell->Next;
Free(TmpCell);
}
}
15. List out the advantages and disadvantages of singly linked list.
Advantages:
1. Easy insertion and deletion.
2. Less time consumption.
Disadvantages:
1. Data not present in a linear manner.
2. Insertion and deletion from the front of the list is difficult
without the use of header node.
16. Define doubly linked linked list with neat diagram.
Doubly linked list is a collection of nodes where each node is a structure containing the
following fields
1. Pointer to the previous node.
2. Data.
3. Pointer to the next node.
17. Define circularly linked list with neat diagram.
Circularly linked list is a collection of nodes , where each node is a structure containing the
element and a pointer to a structure containing its successor.
The pointer field of the last node points to the address of the first node. Thus the linked list
becomes
circular.
18. Define Algorithm?
Algorithm is a solution to a problem independent of programming language. It consist of
set of finite steps which, when carried out for a given set of inputs, produce the corresponding
output and terminate in a finite time.
19. What are the features of an efficient algorithm?
Free of ambiguity
Efficient in execution time
Concise and compact
Completeness
Definiteness
Finiteness
20. List down any four applications of data structures?
Compiler design
Operating System
Database Management system
Network analysis
21. What are the operations of ADT?
Union, Intersection, size, complement and find are the various operations of ADT.
22. What is meant by list ADT?
List ADT is a sequential storage structure. General list of the form a1, a2, a3.…., an and
the size of the list is 'n'. Any element in the list at the position I is defined to be ai, ai+1 the
successor of ai and ai-1 is the predecessor of ai.
23. What are the various operations done under list ADT?
Print list
Insert
Make empty
Remove
Next
Previous
Find kth
24. What is a Rational number?
A Rational number is a number that can be expressed as the quotient of two integers.
Operations on Rational number:
 Creation of rational number from two integers.
 Addition
 Multiplication
 Testing for equality.
25. What are the two parts of ADT?
 Value definition
 Operator definition
26. What is a Sequence?
A sequence is simply an ordered set of elements. A sequence S is sometimes written as the
enumeration of its elements, such as S = <s0,s1,………..sn-1> If S contains n elements, then
length of S is n.
27. What are the four basic data types?
int, float, char and double
28. What are the two things specified in declaration of variables in C?
 It specifies the amount of storage that must be set aside for objects declared with
that type.
 How data represented by strings of bits are to be interpreted.
29. What is a pointer?
Pointer is a variable, which stores the address of the next element in the list. Pointer is
basically a number.
30. What is an array ?
Array may be defined abstractly as a finite ordered set of homogenous elements. Finite
means there is a specific number of elements in the array.
31. What are the two basic operations that access an array?
Extraction:
Extraction operation is a function that accepts an array, a ,an index i and returns an element of
the array.
Storing:
Storing operation accepts an array , a ,an index i , and an element x.
32. Define Structure?
A Structure is a group of items in which each item is identified by its own identifier ,each of
which is known as a member of the structure.
33. Define Union ?
Union is collection of Structures , which permits a variable to be interpreted in several different
ways.
34. Define Automatic and External variables?
Automatic variables are variables that are allocated storage when the function is invoked.
External variables are variables that are declared outside any function and are allocated storage
at the point at which they are first encountered for the remeinder of the program’s execution.
35. What are the different types of data structures?
i) Primitive data structure
ii) Non primitive data structure.
36. What do you mean by primitive data structure?
Primitive data structure is concerned with structuring of data at their most primitive level
within a computer, that is the data structures are directly operated upon by machine-level
instructions
eg) Integers, real number, characters
37. What is searching?
Searching is a process of finding a record in the table according to the given key value.
Searching is extensively used in data processing.
38. What is Linear search?
Linear search scan each entry in the table in a sequential manner until the desired record
is found. Efficiency: O(n)
39. Define Space Complexity
The Space complexity of an algorithm is the amount of memory it needs
to run to completion
40. Define Time Complexity
Time complexity of an algorithm is the amount of computer time it needs
to run to completion
41. What are asymptotic notations?
The notations that enables us to make meaningful statements about the
time and space complexity of a program is called asymptotic notations
42. Write postfix from of the expression –A+B-C+D?
A-B+C-D+
43. What are the different ways to implement list?
Simple array implementation of list
Linked list implementation of list
44. What are the advantages in the array implementation of list?
a) Print list operation can be carried out at the linear time
b) Fint Kth operation takes a constant time
45. What is a linked list?
Linked list is a kind of series of data structures, which are not necessarily
adjacent in memory. Each structure contain the element and a pointer to a record containing its
successor
46. What is the need for the header?
Header of the linked list is the first element in the list and it stores the number of
elements in the list. It points to the first data element of the list.
47. List three examples that uses linked list?
 Polynomial ADT
 Radix sort
 Multi lists
50. Define stack ADT with example.
A stack is a list with a restriction that insertions and deletions can be performed in only one
position namely the end of the list called the top.
e.g.: undo statement in text editor.
Pile of bills in a hotel.
51. State the operations on stack. Define them and give the diagrammatic representation.
Push
Pop
Top
Push: Push is performed by inserting at the top of stack.
Pop: pop deletes the most recently inserted element.
Top: top operation examines the element at the top of the stack and returns its value.
52. Write the routine for push and pop of linked list.
/*routine for push*/
Void Push(ElementType X,Stack S)
Stack S
Pop(S) Push(X,S)
Top(S)
{
PtrToNode TmpCell;
TmpCell=malloc(sizeof(struct Node));
If(TmpCell==NULL)
FatalError(“out of space!!!”);
else
{
TmpCell->Element=x;
TmpCell->Next=s->Next;
S->Next=TmpCell;
}
}
/*Routine for pop*/
Void Pop(stack S)
{
PtrToNode FirstCell;
If(IsEmpty(S))
Error(“Empty stack”);
Else
{
FirstCell=S->Next;
S->Next=S->Next->Next;
free(firstCell);
}
}
53. What is the purpose of top and pop?
Top operation examines the element in the top of the list and returns its value. Pop operation
deletes the element at the top of the stack and decrements the top of the stack pointer by one.
54. State the disadvantages of linked list implementation of stack.
1. Calls to malloc and free functions are expensive.
2. Using pointers is expensive.
55. State the applications of stack.
1. Balancing parentheses.
2. Postfix Expression.
i. Infix to postfix conversion
3. Function calls.
56. Write the algorithm for balancing symbols.
1. Make an empty stack.
2. Read characters until end of file.
3. If the character is an opening symbol, then push it onto the stack.
4. If it is a closing symbol Then If the stack is empty Report an error
Otherwise pop the stack
5.If the symbol popped is not the corresponding openinig symbol Then
Report an error
6. If the stack is not empty at the end of file Then
Report an error
57. Give the Features of balancing symbols.
1. It is clearly linear.
2. Makes only one pass through the input.
3. It is online and quite fast.
4. It must be decided what to do when an error is reported.
58. Convert the given infix to postfix.
(j*k)+(x+y)
Ans:jk* xy++
59. Convert into postfix and evaluate the following expression.
(a+b*c)/d
a=2 b=4 c=6 d=2
Ans:
Post fix: abc*+d/
Evaluation:
2 4 6 * +2/ =13
60. Write the features of representing calls in a stack.
1. When a function is called the register values and return address are saved.
2. After a function has been executed the register values are resumed on returning
to the calling statement.
3. The stack is used for resuming the register values and for returning to the
calling statement.
4. The stack overflow leads to fatal error causing loss of program and data.
5. Recursive call at the last line of the program is called tail recursion and also
leads to error.
61. Define queue with examples.
Queue is a list in which insertion is done at one end called the rear and deletion is performed
at another called front.
e.g: Ticket counter.
Phone calls waiting in a queue for the operator to receive.
62. List the operations of queue.
Two operations
1. Enqueue-inserts an element at the end of the list called the rear.
2. Dequeue-delets and returns the element at the start of the list called as the front.
63. What are the prerequisites for implementing the queue ADT using array?
For each queue data structure the following prerequisites must be satisfied
1. Keep an array queue[ ].
2. The positions front and rear represents the ends of the queue.
3. The number of elements that are actually in the queue is kept trasck of using ‘size’.
64. How the enqueue and dequeue operations are performed in queue.
To enqueue an element X:
increment size and rear
set queue[rear]=x
To dequeue an element set the return value to queue[front].
Decrement size
Increment front
65. Write the routines for enqueue operation in queue.
Void Enqueue(Element type X,Queue Q)
{
If(IsFull(Q))
Error(“Full queue”);
Else
{
Q->Size++;
Q->Rear=Succ(Q->Rear,Q);
Q->Array[Q->Rear]=X;
}
}
66. Write the routines for dequeue operation in queue.
Void Dequeue(Queue Q)
{
If(IsEmpty(Q))
Error(“Empty Queue”);
Else
Q->front++;
}
67. List the Applications of queue?
• Graph Algorithm
• Priority Algorithm
• Job scheduling
• Categorizing Data
68. Define priority queue with diagram and give the operations.
Priority queue is a data structure that allows at least the following two operations.
1. Insert-inserts an element at the end of the list called the rear.
2. DeleteMin-Finds, returns and removes the minimum element in the priority Queue.
Operations:
Insert
DeleteMin
69. Give the applications of priority queues.
There are three applications of priority queues
1. External sorting.
2. Greedy algorithm implementation.
3. Discrete even simulation.
4. Operating systems.
Priority Queue H
DeleteMin(H) Insert(H)
70. How do you test for an empty queue?
To test for an empty queue, we have to check whether READ=HEAD where REAR is a
pointer pointing to the last node in a queue and HEAD is a pointer that pointer to the dummy
header. In the case of array implementation of queue, the condition to be checked for an empty
queue is READ<FRONT.