u78oz-00s43 (1)
u78oz-00s43 (1)
1. Simpii ity
3. Ease oi Trfaverfsai
4. Sequcentiai A ess
6. Verfsatiiity
Linear data structures can implement more complex data structures and
algorithms, serving as building blocks for stacks, queues, and hash tables.
Arrays and linked lists can be adapted and extended to create more
sophisticated data structures. For example, a stack can be implemented
using an array or a linked list, and queues can be efciently managed
using linked lists.
Linear data structures can be easily sorted and searched using well-known
algorithms, enhancing their utility in various applications. Algorithms like
quicksort and binary search work efciently with linear data structures,
leveraging their ordered nature to optimize performance. This makes
linear data structures suitable for frequent data retrieval and organization
applications.
9. Ease oi Impiementatioin
11. Fiexibiiity
Stack :
Operations on stack: The two basic operations associated with stacks are:
1. Push
2. 2. Pop
While performing push and pop operations the following test must
be conducted on the stack.
a) Stack is empty or not
b) stack is full or not
1. Pucsh : Push operation is used to add new elements in to the
stack. At the time of addition frst check the stack is full or not. If the
stack is full it generates an error message "stack overflow".
2. Poip: Pop operation is used to delete elements from the stack. At
the time of deletion frst check the stack is empty or not. If the stack
is empty it generates an error message "stack underflow".
All insertions and deletions take place at the same end, so the last
element added to the stack will be the frst element removed from
the stack. When a stack is created, the stack base remains fxed
while the stack top changes as elements are added and removed.
The most accessible element is the top and the least accessible
element is the bottom of the stack.
Representation of Stack (or) Implementation of stack:
The stack should be represented in two ways:
1. Stack using array
2. Stack using linked list
1. Stack using array: Let us consider a stack with 6 elements
capacity. This is called as the size of the stack. The number of
elements to be added should not exceed the maximum size of the
stack. If we attempt to add new element beyond the maximum size,
we will encounter a stack overflow condition. Similarly, you cannot
remove elements beyond the base of the stack. If such is the case,
we will reach a stack underflow condition.
1. pucsh():When an element is added to a stack, the operation is
performed by push(). Below Figure shows the creation of a stack and
addition of elements using push().
Initially top=-1, we can insert an element in to the stack, increment the top value
i.e top=top+1. We can insert an element in to the stack frst check the condition
is stack is full or not. i.e top>=size-1. Otherwise add the element in to the stack.
We can insert an element from the stack, decrement the top value i.e
top=top-1. We can delete an element from the stack frst check the
condition is stack is empty or not. i.e top==-1. Otherwise remove the
element from the stack.
Step 1: START
Step 2: if top==-1 then Write “Stack is Underfloww
Step 3: otherwise
3.1: print “deleted elementw
3.2: top=top-1;
Step 4: END
3. dispiay(): This operation performed display the elements in the
stack. We display the element in the stack check the condition is
stack is empty or not i.e top==-1.Otherwise display the list of
elements in the stack.
Aigoirfithm: prfoi educrfe poip():
Step 1: START
Step 2: if top==-1 then Write “Stack is Underfloww
Step 3: otherwise
3.1: print “Display elements arew
3.2: for top to 0 Print ‘stack[i]’
Step 4: END
Stack implementation using Array
#include<stdio.h>
#define MAX 5
int top = -1;
int stack[MAX];
void push()
{
int item;
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
printf("Enter the item to be pushed in stack : ");
scanf("%d",&item);
top=top+1;
stack[top] = item;
}
}
void pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
{
printf("Popped element is : %d\n",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack[i] );
}
}
void main()
{
int choice;
do {
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}
}
while(choice!=4); }
QUEUE ADT :
The operations for a queue are analogues to those for a stack; the
diference is that the insertions go at the end of the list, rather than the
beginning.
Again insert another element 33 to the queue. The status of the queue is:
Now, delete an element. The element deleted is the element at the front of the
queue. So the status of the queue is:
Now, insert new elements 44 and 55 into the queue. The queue status is:
Next insert another element, say 66 to the queue. We cannot insert 66 to
the queue as the rear crossed the maximum size of the queue (i.e., 5).
There will be queue full signal. The queue status is as follows:
Now it is not possible to insert an element 66 even though there are two
vacant positions in the linear queue. To overcome this problem the
elements of the queue are to be shifted towards the beginning of the
queue so that it creates vacant position at the rear end. Then the FRONT
and REAR are to be adjusted properly. The element 66 can be inserted at
the rear end. After this operation, the queue status is as follows:
Check if the queue is already full by comparing rear to max - 1. if so, then
return an overflow error If the item is to be inserted as the frst element in
the list, in that case set the value of front and rear to 0 and insert the
element at the rear end. Otherwise keep increasing the value of rear and
insert each element one by one having rear as the index.
Aigoirfithm toi deiete an eiement rfoim the quceuce
If, the value of front is -1 or value of front is greater than rear , write an
underflow message and exit. Otherwise, keep increasing the value of front
and return the item stored at the front end of the queue at each time.
If we want to go from city "A" to city "B", there can be many ways
of doing this. We can go by flight, by bus, by train and also by bicycle.
Depending on the availability and convenience, we choose the one which
suits us. Similarly, in computer science, there are multiple algorithms to
solve a problem. When we have more than one algorithm to solve a
problem, we need to select the best one. Performance analysis helps us to
select the best algorithm from multiple algorithms to solve a problem.
Generally, the performance of an algorithm depends on the following
elements...
1. Whether that algorithm is providing the exact solution for the
problem?
2. Whether it is easy to understand?
3. Whether it is easy to implement?
4. How much space (memory) it requires to solve the problem?
5. How much time it takes to solve the problem? Etc.,
When we want to analyse an algorithm, we consider only the space and
time required by that particular algorithm and we ignore all the remaining
elements.
What is Spa e oimpiexity?
When we design an algorithm to solve a problem, it needs some
computer memory to complete its execution. For any algorithm, memory
is required for the following purposes...
1. To store program instructions.
2. To store constant values.
3. To store variable values.
4. And for few other things like function calls, jumping statements etc,.
Space complexity of an algorithm can be defned as follows...
Generally, when a program is under execution it uses the computer
memory for THREE reasons. They are as follows...
1. Instrfuc tioin Spa e: It is the amount of memory used to store
compiled version of instructions.
2. Envirfoinmentai Sta k: It is the amount of memory used to store
information of partially executed functions at the time of function call.
3. Data Spa e: It is the amount of memory used to store all the variables
and constants.
What is Time oimpiexity?
Every algorithm requires some amount of computer time to execute
its instruction to perform the task. This computer time required is called
time complexity.
The time complexity of an algorithm can be defned as follows...
Generally, the running time of an algorithm depends upon the following...
1. Whether it is running on Singie processor machine
or Muciti processor machine.
2. Whether it is a 32 bit machine or 64 bit machine.
3. Read and Wrfite speed of the machine arithmetic operations, logical
operations, return value and assignment operations etc..
4. Inpuct data
To calculate the time complexity of an algorithm, we need to defne a
model machine.
Let us assume a machine with following confguration...
1. It is a Single processor machine
2. It is a 32 bit Operating System machine
3. It performs sequential execution
4. It requires 1 unit of time for Arithmetic and Logical operations
5. It requires 1 unit of time for Assignment and Return value
6. It requires 1 unit of time for Read and Write operations
Now, we calculate the time complexity of following example code by using
the above-defned model machine...
What is Asymptoiti Noitatioin?
Whenever we want to perform analysis of an algorithm, we need to
calculate the complexity of that algorithm. But when we calculate the
complexity of an algorithm it does not provide the exact amount of
resource required. So instead of taking the exact amount of resource, we
represent that complexity in a general form (Notation) which produces the
basic nature of that algorithm. We use that general form (Notation) for
analysis process.
Aigoirfithm 1 : 5n2 + 2n + 1
Aigoirfithm 2 : 10n2 + 8n + 3
Generally, when we analyze an algorithm, we consider the time
complexity for larger values of input data (i.e. 'n' value). In above two-
time complexities, for larger value of 'n' the term '2n + 1' in algorithm 1
has least signifcance than the term '5n2', and the term '8n + 3' in
algorithm 2
Has least signifcance than the term 10 n2 .
Here, for larger value of 'n' the value of most signifcant terms
( 5n2 and 10n2 ) is very larger than the value of least signifcant terms
( 2n + 1 and 8n + 3 ). So for larger value of 'n' we ignore the least
signifcant terms to represent overall time required by an algorithm. In
asymptotic notation, we use only the most signifcant terms to represent
the time complexity of an algorithm.
Majorly, we use THREE types of Asymptotic Notations and those are as
follows...
1. Big - Oh (O)
2. Big - Omega (Ω)
3. Big - Theta (Θ)
4. Big - Oh Noitatioin (O)
5. Big - Oh notation is used to defne the ucpperf boiucnd of an algorithm
in terms of Time Complexity.
That means Big - Oh notation always indicates the maximum time
required by an algorithm for all input values. That means Big - Oh
notation describes the worst case of