Data Structure - Unit I - ABSTRACT DATA TYPES (ADT)
ABSTRACT
DATA TYPES (ADT) AND LISTS
1. ABSTRACT DATA TYPES (ADT)
An Abstract Data Type (ADT)
is a logical specification of a data type which defines:
- The data objects
- The operations that can be performed on those
objects
ADT does not specify the implementation
details. It only describes what operations are allowed, not how
they are carried out.
Need for ADT
- To separate logic from implementation
- To improve software reliability
- To support data abstraction
Components of ADT
- Data
– Values stored
- Operations
– Actions on data
- Interface
– Interaction with user/program
ADT Operations:
- Create()
- Insert(position, element)
- Delete(position)
- Search(element)
- Traverse()
Advantages of ADT
- Implementation independent
- Easier debugging
- Improves modularity
- Supports code reusability
2. LIST ADT
A List ADT is a linear
collection of elements arranged in a sequential order, where:
- Each element has a unique position
- Duplicate elements are allowed
Characteristics
- Linear structure
- Ordered elements
- Dynamic or static size
Operations of List ADT
- Creation
- Insertion
- Deletion
- Searching
- Traversal
- Merging
3.
ARRAY-BASED IMPLEMENTATION OF LIST ADT
Memory Representation
- Uses contiguous memory
- Accessed using index
Example:
Index:
0 1
2 3
Data
: 10 20 30 40
Insertion Algorithm
- Check overflow
- Shift elements to the right
- Insert element
Deletion Algorithm
- Check underflow
- Shift elements to the left
- Reduce size
MERGE OPERATION IN LIST
Combining two lists into a single
list.
Sorted Merge Algorithm
- Compare first elements
- Insert smaller element
- Move pointer
- Repeat until end
TRAVERSAL OF LIST
Visiting every element once.
Traversal Algorithm
- Start from head
- Print data
- Move to next
- Stop at NULL / first node
APPLICATIONS OF LISTS
- Polynomial manipulation
- Dynamic memory allocation
- Operating systems
- Undo/Redo operations
- Music playlist
Advantages
- Faster access
- Simple structure
- Efficient traversal
Disadvantages
- Fixed size
- Memory wastage
- Costly insert/delete
4. LINKED
LIST IMPLEMENTATION OF LIST ADT
Definition
A linked list is a dynamic
linear data structure consisting of nodes, where:
- Each node contains data
- Each node has link(s) to other nodes
4.1 SINGLY LINKED LIST
Node Structure
|
Data | Next |
Insertion Operations
Insertion at Beginning
- Create new node
- Set new->next = head
- Update head
Insertion at End
- Traverse to last node
- Set last->next = new
- new->next = NULL
Deletion Operations
Delete First Node
- temp = head
- head = head->next
- free(temp)
Delete Last Node
- Traverse to second last
- Set next = NULL
- Free last node
Advantages
- Dynamic size
- Efficient insert/delete
- No memory wastage
Disadvantages
- Extra memory for pointer
- No backward traversal
4.2 CIRCULAR LINKED LIST
Definition
A circular linked list is a
linked list in which:
- Last node points to the first node
- No NULL pointers
Diagram (Representation)
[10]
→ [20] → [30]
↑ ↓
←–––––––––––––––
Advantages
- No NULL pointer checks
- Efficient traversal
- Suitable for round-robin scheduling
Applications
- CPU scheduling
- Multiplayer games
- Buffer management
4.3 DOUBLY LINKED LIST
Node Structure
|
Prev | Data | Next |
Insertion
- Insert at beginning
- Insert at end
- Insert at position
Deletion
- Easy deletion of any node
- No need for previous node traversal
Advantages
- Bidirectional traversal
- Efficient deletion
Disadvantages
- Extra memory
- Complex implementation
5.
POLYNOMIAL MANIPULATION USING LINKED LIST
Polynomial Representation
Each node stores:
|
Coefficient | Exponent | Next |
Example:
5x³
+ 4x² + 3
5.1 Polynomial Insertion
Algorithm:
- Create node
- Compare exponents
- Insert in descending order
- Combine like terms
5.2 Polynomial Deletion
Steps:
- Traverse list
- Locate exponent
- Adjust links
- Free node
5.3 Polynomial Traversal
Steps:
- Start from head
- Print coefficient and exponent
- Move to next
5.4 Polynomial Addition (Merge
Operation)
Algorithm
- Compare exponents of two polynomials
- If same, add coefficients
- Insert result into new list
- Copy remaining terms
5.5 Polynomial Multiplication
Algorithm
- Multiply each term of P1 with P2
- Insert into result list
- Combine like terms
Comments
Post a Comment