0% found this document useful (0 votes)
11 views17 pages

u78oz-00s43 (1)

Linear data structures are organized sequentially, allowing for efficient data storage and access, with examples including arrays, linked lists, stacks, and queues. They offer simplicity, efficient memory utilization, ease of traversal, and predictable performance, making them versatile for various applications. Additionally, abstract data types like stacks and queues are defined, highlighting their operations and implementations using arrays and linked lists.

Uploaded by

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

u78oz-00s43 (1)

Linear data structures are organized sequentially, allowing for efficient data storage and access, with examples including arrays, linked lists, stacks, and queues. They offer simplicity, efficient memory utilization, ease of traversal, and predictable performance, making them versatile for various applications. Additionally, abstract data types like stacks and queues are defined, highlighting their operations and implementations using arrays and linked lists.

Uploaded by

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

Unit-I

Definitioin oi iinearf data strfuc tucrfes :

A linear data structure is a type of data structure where elements are


arranged in a sequential order, one after the other. Each element is
connected to its previous and next element, forming a linear sequence.
This arrangement allows for straightforward traversal, insertion, and
deletion operations.

 Linear data structures are essential in as they provide a simple and


efcient way to store and access data.
 They are used in various algorithms and applications, ranging from
simple data storage to complex problem-solving techniques.
 Examples of linear data structures include arrays, linked lists,
stacks, and queues. Each of these structures has its unique
properties and use cases, but they all share the common trait of
linear organization.
Impoirftan e oi iinearf data strfuc tucrfes:

1. Simpii ity

The linear arrangement in linear data structures is straightforward, easy


to implement, understand and use. It is also easier to manipulate or
modify them .

2. Effi ient Memoirfy Utiiiiatioin

Linear data structures efciently utilize memory space, as elements are


stored sequentially. For example, in arrays, memory allocation is
contiguous, which minimizes the overhead of memory management. Also
in linked lists allow dynamic memory allocation and can grow or shrink as
needed, thus optimizing memory usage.

3. Ease oi Trfaverfsai

Linear data structures facilitate easy and straightforward traversal of


elements, making operations like searching, sorting, and updating more
manageable. Since elements are arranged in a sequence, moving from
one element to the next is simple and direct.

4. Sequcentiai A ess

Linear data structures provide efcient sequential access to elements,


ideal for algorithms requiring ordered data processing. Operations that
require accessing elements in a specifc order beneft from the sequential
nature of linear data structures.
5. Prfedi tabie Perf oirfman e

The time complexity of basic operations in linear data structures is


predictable and often constant or linear, making performance analysis
straightforward. This further simplifes the analysis and optimization of
algorithms. For example, accessing an element in an array has a constant
time complexity (O(1)), while traversing a linked list has a linear time
complexity (O(n)).

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.

7. Effi ient Soirfting and Searf hing

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.

8. Coimpatibiiity with Harfdwarfe

Linear data structures align well with the underlying hardware


architecture, such as CPU caching and memory access patterns, resulting
in optimized performance. For example, the contiguous memory allocation
in arrays aligns with how modern CPUs cache data, leading to faster
access times and improved performance. Linked lists, while non-
contiguous, still beneft from sequential access patterns that match typical
memory usage scenarios.

9. Ease oi Impiementatioin

Many programming languages ofer built-in support for linear data


structures, simplifying their implementation and use. Languages like C,
C++, Java, and Python provide built-in array and linked list structures,
along with libraries and functions to manipulate them. This built-in support
makes it easier for developers to use these data structures without
implementing them from scratch.

10. Orfderf Prfeserfvatioin


Linear data structures preserve the order of elements, which is essential
in many applications where the sequence of data matters such as task
scheduling.

11. Fiexibiiity

Linear data structures can be adapted to various use cases, providing


flexibility in application design. For instance, arrays can be used for static
data storage with fxed size, while linked lists ofer dynamic size
adjustments. This flexibility allows developers to choose the most
appropriate linear data structure based on the specifc needs of their
application.

Abstrfa t Data Type:

An abstract data type, sometimes abbreviated ADT, is a logical description


of how we view the data and the operations that are allowed without
regard to how they will be implemented. This means that we are
concerned only with what data is representing and not with how it will
eventually be constructed. By providing this level of abstraction, we are
creating an encapsulation around the data. The idea is that by
encapsulating the details of the implementation, we are hiding them from
the user’s view. This is called information hiding. The implementation of
an abstract data type, often referred to as a data structure, will require
that we provide a physical view of the data using some collection of
programming constructs and primitive data types.

Stack :

A Stack is linear data structure. A stack is a list of elements in which an


element may be inserted or deleted only at one end, called the top of the
stack. Stack principle is LIFO (last in, frst out). Which element inserted
last on to the stack that element deleted frst from the stack. As the items
can be added or removed only from the top i.e. the last item to be added
to a stack is the frst item to be removed. Real life examples of stacks are:

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.

Aigoirfithm: Prfoi educrfe oirf pucsh():


Step 1: START
Step 2: if top>=size-1 then Write “ Stack is Overfloww
Step 3: Otherwise
3.1: read data value ‘x’
3.2: top=top+1;
3.3: stack[top]=x;
Step 4: END
2. Poip(): When an element is taken of from the stack, the operation is
performed by pop(). Below fgure shows a stack initially with three
elements and shows the deletion of elements using pop().

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.

Aigoirfithm: prfoi educrfe poip():

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 :

A queue is linear data structure and collection of elements. A queue is another


special kind of list, where items are inserted at one end called the rear and
deleted at the other end called the front. The principle of queue is a “FIFOw or
“First-in-frst-outw.

Queue is an abstract data structure. A queue is a useful data structure in


programming. It is similar to the ticket queue outside a cinema hall, where the
frst person entering the queue is the frst person who gets the ticket.

A real-world example of queue can be a single-lane one-way road, where the


vehicle enters frst, exits frst.

More real-world examples can be seen as queues at the ticket windows


and bus-stops and our college library.

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.

Operations on QUEUE: A queue is an object or more specifcally an


abstract data structure (ADT) that allows the following operations:

• Enqueue or insertion: which inserts an element at the end of the queue.

• Dequeue or deletion: which deletes an element at the start of the


queue.

Representation of Queue (or) Implementation of Queue:

The queue can be represented in two ways:


1. Queue using Array

2. Queue using Linked List

1. Queue using Array:

Let us consider a queue, which can hold maximum of fve elements.


Initially the queue is empty. Now, insert 11 to the queue. Then queue
status will be:

Next, insert 22 to the queue. Then the queue status is:

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:

Again, delete an element. The element to be deleted is always pointed to


by the FRONT pointer. So, 22 is deleted. The queue status is as follows:

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:

This difculty can overcome if we treat queue position with index 0 as a


position that comes after position with index 4 i.e., we treat the queue as
a circular queue.

Algorithm to insert any element in a queue

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.

Dispiay() - Dispiays the eiements oi a Quceuce We an ucse the


oiiioiwing steps toi dispiay the eiements oi a quceuce...

• Step 1 - Check whether queue is EMPTY.


• Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate
the function.
• Step 3 - If it is NOT EMPTY, then defne an integer variable 'i' and set 'i =
front'.
• Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value reaches to rear (i <= rear)
List ADT :
A Linked List is a linear data structure which looks like a chain of nodes,
where each node contains a data feld and a rfe erfen e(iink) to the
next node in the list. Unlike Arrays, Linked List elements are not stored
at a contiguous location.
Coimmoin Featucrfes oi Linked List:
 Noide: Each element in a linked list is represented by a node,
which contains two components:
 Data: The actual data or value associated with
the element.
 Next Poiinterf(oirf Link): A reference or pointer to
the next node in the linked list.
 Head: The frst node in a linked list is called the “head.w It
serves as the starting point for traversing the list.
 Taii: The last node in a linked list is called the “tail.w
Types oi Linked Lists:
 Singiy Linked List: In this type of linked list, every node
stores the address or reference of the next node in the list and
the last node has the next address or reference as NULL.
 For example: 1->2->3->4->NULL

Singly Linked List

 Doiucbiy Linked Lists: In a doubly linked list, each node has


two pointers: one pointing to the next node and one pointing to
the previous node. This bidirectional structure allows for
efcient traversal in both directions.

Doubly Linked Lists

 Cirf uciarf Linked Lists: A circular linked list is a type of


linked list in which the frst and the last nodes are also
connected to each other to form a circle, there is no NULL at the
end.

Circular Linked Lists

Types oi Linked List oiperfatioins:


 A essing Eiements: Accessing a specifc element in a
linked list takes O(n) time since nodes are stored in non
conitgous locations so random access if not possible.
 Searf hing: Searching of a node in linked list takes O(n) time
as whole list needs to travesed in worst case.
 Inserftioin: Insertion takes O(1) time if we are at the position
where we have to insert an element.
 Deietioin: Deletion takes O(1) time if we know the position of
the element to be deleted.
What is Perf oirfman e Anaiysis oi an aigoirfithm?

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

An Algorithm time complexity.


Big - Oh Notation can be defned as follows...
Big – Omega Noitatioin (Ω)
Big - Omega notation is used to defne the ioiwerf boiucnd of an
algorithm in terms of Time Complexity.
That means Big-Omega notation always indicates the minimum time
required by an algorithm for all input values. That means Big-Omega
notation describes the best case of an algorithm time complexity.
Big - Theta Noitatioin (Θ)
Big - Theta notation is used to defne the averfage boiucnd of an
algorithm in terms of Time Complexity.
That means Big - Theta notation always indicates the average time
required by an algorithm for all input values. That means Big - Theta
notation describes the average case of an algorithm time
complexity.

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