0% found this document useful (0 votes)
39 views5 pages

Dsa Midterm

A linked list is a linear data structure where each element (called a node) contains a data field and a pointer to the next node. Nodes are not stored contiguously in memory. There are two types: single linked lists where each node only points forward, and double linked lists where each node points both forward and backward. Operations like insertion, deletion, and searching can be performed by traversing the list and adjusting the pointers between nodes. Linked lists allow dynamic memory allocation and are well-suited for insertion/deletion compared to arrays.

Uploaded by

Aubrey Sanchez
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)
39 views5 pages

Dsa Midterm

A linked list is a linear data structure where each element (called a node) contains a data field and a pointer to the next node. Nodes are not stored contiguously in memory. There are two types: single linked lists where each node only points forward, and double linked lists where each node points both forward and backward. Operations like insertion, deletion, and searching can be performed by traversing the list and adjusting the pointers between nodes. Linked lists allow dynamic memory allocation and are well-suited for insertion/deletion compared to arrays.

Uploaded by

Aubrey Sanchez
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/ 5

LINKED LIST maintain the link of the previous 1.

Firstly create a new node


node. A node can be inserted in a (say new_node).
A linear data structure which looks
Doubly Linked List in four ways:
like a chain of nodes, where each 2. Now insert the data in the
node is a different element. Unlike • Add a node at the front in a new node.
Arrays, Linked List elements are not Doubly Linked List: The
3. Point the next
stored at a contiguous location. new node is always added
of new_node to the next
before the head of the
of prev_node.
given Linked List. The task
can be performed by using 4. Point the next
the following 5 steps of prev_node to new_node.
TERMINOLOGIES OF LINKED LIST • Firstly, allocate a new node 5. Point the previous
Node Structure: A node in a linked (say new_node). of new_node to prev_node.
list typically consists of two • Now put the required data 6. Change the pointer of the
components: in the new node. new node’s previous
• Data: It holds the actual pointer to new_node.
• Make the next
value or data associated of new_node point to the
with the node. current head of the doubly
• Next Pointer: It stores the linked list.
memory address (reference) • Make the previous of the
of the next node in the current head point
sequence. to new_node. Add a node before a given node in
a Doubly Linked List:
Head and Tail: The linked list is • Lastly, point head
accessed through the head node, to new_node. Let the pointer to this given node
which points to the first node in the be next_node. This can be done
list. The last node in the list points using the following 6 steps:
to NULL or nullptr, indicating the
1. Allocate memory for the
end of the list. This node is known
new node, let it be
as the tail node.
called new_node.
TYPES OF LINKED LIST
Add a node between two nodes: It 2. Put the data in new_node.
1. SINGLE LINKED LIST is further classified into the
3. Set the previous pointer of
following two parts:
A single linked list is a linear data this new_node as the
structure in which the elements are • Add a node after a given previous node of
not stored in contiguous memory node in a Doubly Linked the next_node.
locations and each element is List
4. Set the previous pointer of
connected only to its next element
• Add a node before a given the next_node as
using a pointer.
node in a Doubly Linked the new_node.
List
5. Set the next pointer of
Add a node after a given node in a this new_node as
Doubly Linked List: We are given a the next_node.
2. DOUBLE LINKED LIST pointer to a node as prev_node,
6. Now set the previous
and the new node is inserted after
Inserting a new node in a doubly pointer of new_node.
the given node. This can be done
linked list is very similar to inserting using the following 6 steps: • If the previous node
new node in linked list. There is a of the new_node is
little extra work required to
not NULL, then set to form a circle. In a circular linked 2. Deletion: Removing a node
the next pointer of list, the first node and the last node from a linked list requires
this previous node are connected to each other which adjusting the pointers of the
as new_node. forms a circle. There is no NULL at neighboring nodes to bridge
the end. the gap left by the deleted
• Else, if the prev of
node. Deletion can be
new_node is NULL, TWO TYPES FOR CIRULAR LINKED
performed at the beginning,
it will be the new LIST
end, or any position within
head node.
Circular single linked list: In a the list.
circular Singly linked list, the last
3. Searching: Searching for a
node of the list contains a pointer to
specific value in a linked list
the first node of the list. We
involves traversing the list
traverse the circular singly linked list
from the head node until
until we reach the same node
Add a node at the end in a Doubly the value is found or the
where we started. The circular
Linked List: end of the list is reached.
singly linked list has no beginning or
The new node is always added after end. No null value is present in the WHY LINKED LIST DATA IS
the last node of the given Linked next part of any of the nodes. IMPORTANT
List. This can be done using the
• Dynamic Data
following 7 steps:
structure: The size of
1. Create a new node memory can be allocated or
(say new_node). • Circular Double linked de-allocated at run time
list: has properties of both based on the operation
2. Put the value in the new
double linked list and insertion or deletion.
node.
circular linked list in which • Ease of
3. Make the next pointer two consecutive elements Insertion/Deletion: The
of new_node as null. are linked or connected by insertion and deletion of
the previous and next elements are simpler than
4. If the list is empty,
pointer and the last node arrays since no elements
make new_node as the
points to the first node by need to be shifted after
head.
the next pointer and also insertion and deletion, Just
5. Otherwise, travel to the end the first node points to the the address needed to be
of the linked list. last node by the previous updated.
pointer.
6. Now make the next pointer • Efficient Memory
of last node point Utilization: As we know
to new_node. Linked List is a dynamic data
7. Change the previous pointer structure the size increases
of new_node to the last or decreases as per the
TYPE OF LINKED LIST OPERATIONS: requirement so this avoids
node of the list.
1. Insertion: Adding a new the wastage of memory.
node to a linked list involves • Implementation: Various
adjusting the pointers of the advanced data structures
existing nodes to maintain can be implemented using a
the proper sequence. linked list like a stack,
CIRCULAR LINKED LIST Insertion can be performed queue, graph, hash maps,
at the beginning, end, or etc.
The circular linked list is a linked any position within the list
list where all nodes are connected
LINKED LIST VS ARRAY PUSH OPERATION return stack[top]

Adds an item to the stack. If the end procedure


stack is full, then it is said to be
isEmpty OPERATION
an Overflow
Returns true if the stack is empty,
begin
else false.
STACKED AND QUEUE TOPIC if stack is full
begin
WHAT IS STACKED? return
if top < 1
A stack is a linear data structure in endif
return true
which the insertion of a new else
element and removal of an existing else
element takes place at the same increment top
return false
end represented as the top of the stack[top] assign value
stack. end procedure
end else
To implement the stack, it is SIZE OPERATION
required to maintain the pointer to end procedure
Returns the size of the stack.
the top of the stack, which is the
last element to be inserted begin
because we can access the POP OPERATION
return stack
elements only on the top of the
Removes an item from the stack.
stack. end procedure
The items are popped in the
reversed order in which they are TYPES OF STACK
pushed. If the stack is empty, then it
is said to be Fixed Size Stack:
an Underflow condition. As the name suggests, a fixed size
begin stack has a fixed size and cannot
grow or shrink dynamically. If the
if stack is empty stack is full and an attempt is made
to add an element to it, an overflow
LIFO return
error occurs. If the stack is empty
This strategy states that the endif and an attempt is made to remove
element that is inserted last will an element from it, an underflow
else
come out first. error occurs.
store value of stack[top]
BASIC OPERATIONS ON STACK Dynamic Size Stack:
decrement top
push() to insert an element into the A dynamic size stack can grow or
stack return value shrink dynamically. When the stack
is full, it automatically increases its
pop() to remove an element from end else size to accommodate the new
the stack
end procedure element, and when the stack is
top() Returns the top element of empty, it decreases its size. This
the stack. TOP OPERATION type of stack is implemented using a
Returns the top element of the linked list, as it allows for easy
isEmpty() returns true if stack is
stack. resizing of the stack.
empty else false.
begin APPLICATIONS OF STACK
size() returns the size of stack.
Infix to Postfix /Prefix conversion
Infix – A+B The pop operation is implemented FIFO
by decrementing the index of the
Postfix – AB+ This strategy states that the
top element and returning the value
element that is inserted first will
Prefix- +AB stored at that index.
come out first
In a linked list-based
OPERATIONS
implementation, the push
operation is implemented by ENQUEUE OPERATION
creating a new node with the new
Redo-undo features at many places element and setting the next Enqueue() operation in Queue adds
like editors, photoshop pointer of the current top node to (or stores) an element to the end of
the new node. The pop operation is the queue.
Forward and backward features in
implemented by setting the next The following steps should be
web browsers
pointer of the current top node to taken to enqueue (insert) data into
Used in many algorithms like Tower the next node and returning the a queue
of Hanoi, tree traversals, stock value of the current top node.
span problems, and histogram Step 1: Check if the queue is full.
In conclusion, a Stack is a linear data
problems. Step 2: If the queue is full, return
structure that operates on the LIFO
Backtracking is one of the algorithm principle and can be implemented overflow error and exit.
designing techniques. To get back using an array or a linked list. The Step 3: If the queue is not full,
from a current state we need to basic operations that can be increment the rear pointer to point
store the previous state for that performed on a stack include push, to the next empty space.
purpose we need a stack. pop, and peek.
Step 4: Add the data element to the
IN GRAPH ALGORITHMS Stacks are commonly used in queue location, where the rear is
computer science for a variety of pointing.
In Memory management, any
applications, including the
modern computer uses a stack as Step 5: return success.
evaluation of expressions, function
the primary management for a
calls, and memory management.
running purpose. Each program that
is running in a computer system has There are two ways to implement a
its own memory allocations. stack –

Stack also helps in implementing  Using array


function call in computers. The last
 Using linked list
called function is always completed
first. WHAT IS QUEUE? DEQUEUE OPERATION

Stacks are also used to implement A queue is a linear data structure Step 1: Check if the queue is empty.
the undo/redo operation in text that is open at both ends and the Step 2: If the queue is empty, return
editor. operations are performed in First In the underflow error and exit.
First Out (FIFO) order.
IMPLEMENTATION OF STACKS Step 3: If the queue is not empty,
We define a queue to be a list in access the data where the front is
A stack can be implemented using
which all additions to the list are pointing.
an array or a linked list.
made at one end, and all deletions
In an array-based implementation, from the list are made at the other Step 4: Increment the front pointer
the push operation is implemented end. The element which is first to point to the next available data
by incrementing the index of the pushed into the order, the delete element.
top element and storing the new operation is first performed on that. Step 5: The Return success.
element at that index.
This is a special type of queue program, there may be multiple
where the last position is connected requests to be kept in a queue, or
back to the first position. Here also one task may create other tasks,
the operations are performed in which must be done in turn by
FIFO order. keeping them in a queue.

Double-Ended Queue (Dequeue): • It has a single resource and


PEEK() or FRONT() OPERATION multiple consumers.
In a double-ended queue the
This operation returns the element insertion and deletion operations, • It synchronizes between
at the front end without removing both can be performed from both slow and fast devices.
it. ends.
• In a network, a queue is
REAR() OPERATION Priority Queue: used in devices such as a
router/switch and mail
This operation returns the element A priority queue is a special queue
queue.
at the rear end without removing it. where the elements are accessed
based on the priority assigned to • Variations: dequeue,
isEmpty OPERATION them. priority queue and double-
This operation returns a boolean ended priority queue.
value that indicates whether the Queues are used for what
queue is purpose?
empty or not. In addition to making your data
isFull OPERATION persistent, queues reduce
errors that occur when different
This operation returns a boolean parts of your system are down.
value that indicates whether the
queue is full or not. What is better, a stack or a
queue?
TYPES OF QUEUE
If you want things to come out
Input Restricted Queue: in the order you put them in,
This is a simple queue. In this type IMPLEMENTATIONS OF QUEUE use a queue. Stacks are useful
of queue, the input can be taken when you want to reorder
Queue can be implemented using things after putting them in.
from only one end but deletion can
following data structures:
be done from any of the ends.
• Implementation of Queue
using Structure in C/C++

• Implementation of Queue
Output Restricted Queue: using Arrays

This is also a simple queue. In this • Implementation of Queue


type of queue, the input can be using Linked List
taken from both ends but deletion APPLICATIONS OF QUEUE
can be done from only one end.
Application of queue is common. In
a computer system, there may be
queues of tasks waiting for the
C
printer, for access to disk storage, or
ircular Queue:
even in a time-sharing system, for
use of the CPU. Within a single

You might also like