Basics Data Structure & Array Implementation
ADT
Data Structure Sec 06
Prepared by: Abdullah Emam
Fayoum international Technological University, Department of Information
Technology
Data Structures
Definition: A data structure is a way of organizing and
storing data so that it can be accessed and modified
efficiently.
Purpose: To process data logically and optimize
performance in programs.
Classification of Data Structures
 Primitive Data Structures
• Integer, Float, Character, Boolean
 Non-Primitive Data Structures
• Linear: Arrays, Linked Lists, Stacks, Queues
• Non-Linear
• Hash
Arrays
 Fixed-size, indexed collection of elements of the same type.
 Pros: Fast access using indices (O(1)).
 Cons: Static size, costly insert/delete.
 Example:
int arr[5] = {10, 20, 30, 40, 50};
Linked Lists
Dynamic data structure consisting of nodes pointing
to the next node.
Types: Singly, Doubly, Circular
Pros: Dynamic size, efficient insert/delete.
Cons: Slow access time (O(n)).
Example Node Structure:
class Node {
int data;
Node* next;
};
Stack (LIFO)
 Last-In, First-Out structure.
 Operations: push(), pop(), top(), isEmpty()
 Used in: Function calls, expression parsing.
stack<int> s;
s.push(10);
Queue (FIFO)
Student s1;
s1.name = "Alice";
s1.age = 20;
Practical Applications of Data Structures
➢ Real-World Examples:
• Trees: File system directories.
• Graphs: Social network connections.
• Hash Tables: Database indexing.
• Heaps: Task scheduling systems.
➢ Case Study:
❑ Implementing a Contact List:
• Array: Suitable for a fixed number of contacts with quick index-based access.
• Linked List: Suitable for a dynamic list where contacts are frequently added or
removed.
• Hash Table: Suitable for quick search operations based on contact names.
Introduction to ADT (Abstract Data Type)
ADT (Abstract Data Type)
• ADT is a logical description of how data is viewed
and the operations on it.
• Does not depend on implementation.
• Examples of ADTs: List, Stack, Queue, Tree
List ADT:
• A collection of ordered elements.
• Elements can be inserted or deleted at specific
positions.
• Types: Singly List, Doubly List, Circular List, Array-
based List
Array-Based List ADT Overview
• Uses a fixed-size array to store elements.
• Elements are contiguously stored in memory.
• Efficient random access, but costly
insertions/deletions in the middle.
Operations in Array List ADT
❖Create Structure of Array
❖Insert(pos, value): Add element at given
index
❖Remove(pos): Remove element at index
❖Search(value): Find index of element
❖Display(): Print all elements
❖isEmpty() / isFull(): Check list status
Structure of Array-Based List
class ArrayADT {
private:
int* arr;
int capacity;
int size;
arr: Pointer to dynamic array
capacity: Maximum number of
elements the list can store
size: Current number of elements
Constructor & Destructor
ArrayADT(int cap) {
capacity = cap;
size = 0;
arr = new int[capacity];
}
~ArrayADT() {
delete[] arr;
}
Explanation:
Allocates dynamic memory
using new.
Frees memory in destructor.
Check if the array is empty Or full
// Check if the array is empty
bool isEmpty() {
return size == 0;
}
// Check if the array is full
bool isFull() {
return capacity == size;
}
Insert Operation
void insert(int value, int pos) {
if (isFull()) {
cout << "Array is fulln";
return;
}
if (size >= capacity || pos < 0 || pos > size) {
cout << "Insert failed.n";
return;
}
for (int i = size - 1; i >= pos; i--)
{ arr[i + 1] = arr[i];}
arr[pos] = value;
size++;
}
Remove Operation
void remove(int pos) {
if (isEmpty()) {
cout << "Array is emptyn";
return;
}
if (pos < 0 || pos >= size) {
cout << "Delete failed.n";
return;
}
for (int i = pos; i < size - 1; i++)
arr[i] = arr[i + 1];
size--;
}
Search Operation
int search(int value) {
for (int i = 0; i < size; i++) {
if (arr[i] == value)
return i;
}
return -1;
} Explanation:
•Returns index of first occurrence or -1 if
not found.
Update Operation
void update(int pos, int value) {
if (pos < 0 || pos >= size) {
cout << "Update failed.n";
return;
}
arr[pos] = value;
}
Explanation:
•Directly updates value at given
index.
Get Operation
int get(int pos) {
if (pos < 0 || pos >= size) {
cout << "Invalid position.n";
return -1;
}
return arr[pos];
}
Explanation:
•Returns value at a given index.
Display Operation
void display() {
cout << "Array: ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
Explanation:
•Traverses and prints all elements.
Main Function Example
int main() {
ArrayADT a(10);
a.insert(10, 0);
a.insert(20, 1);
a.insert(15, 1);
a.display();
a.remove(1);
a.display();
int index = arr.Search(15);
if (index != -1) cout << "Element found at index " << index << "n";
else cout << "Element not foundn";
cout << (arr.isEmpty() ? "Array is emptyn" : "Array is not emptyn");
cout << (arr.isFull() ? "Array is fulln" : "Array is not fulln");
return 0;
}
Complete Code
class ArrayADT {
private:
int* arr;
int capacity;
int size;
public:
ArrayADT(int cap) {
capacity = cap;
size = 0;
arr = new int[capacity];
}
~ArrayADT() {
delete[] arr;
}
void insert(int value, int pos) {
if (size >= capacity || pos < 0 || pos > size)
{ cout << "Insert failed.n";
return; }
for (int i = size - 1; i >= pos; i--)
{ arr[i + 1] = arr[i];}
arr[pos] = value;
size++;}
void remove(int pos) {
if (pos < 0 || pos >= size) {
cout << "Delete failed.n";
return;
}
for (int i = pos; i < size - 1; i++)
arr[i] = arr[i + 1];
size--;
}
int search(int value) {
for (int i = 0; i < size; i++) {
if (arr[i] == value)
return i;
}
return -1;
}
void update(int pos, int value) {
if (pos < 0 || pos >= size) {
cout << "Update failed.n";
return;
}
arr[pos] = value;
}
int get(int pos) {
if (pos < 0 || pos >= size) {
cout << "Invalid position.n";
return -1;
}
return arr[pos];
}
void display() {
cout << "Array: ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
ArrayADT a(10);
a.insert(10, 0);
a.insert(20, 1);
a.insert(15, 1);
a.display();
a.remove(1);
a.display();
cout << "Index of 20: " << a.search(20) <<
endl;
a.update(1, 99);
a.display();
cout << "Element at index 1: " << a.get(1)
<< endl;
cout << (arr.isEmpty() ? "Array is emptyn" :
"Array is not emptyn");
cout << (arr.isFull() ? "Array is fulln" :
"Array is not fulln");
return 0;
}
Sec 06 Basics Data Structure & Array Implementation .pdf

Sec 06 Basics Data Structure & Array Implementation .pdf

  • 1.
    Basics Data Structure& Array Implementation ADT Data Structure Sec 06 Prepared by: Abdullah Emam Fayoum international Technological University, Department of Information Technology
  • 2.
    Data Structures Definition: Adata structure is a way of organizing and storing data so that it can be accessed and modified efficiently. Purpose: To process data logically and optimize performance in programs.
  • 3.
    Classification of DataStructures  Primitive Data Structures • Integer, Float, Character, Boolean  Non-Primitive Data Structures • Linear: Arrays, Linked Lists, Stacks, Queues • Non-Linear • Hash
  • 4.
    Arrays  Fixed-size, indexedcollection of elements of the same type.  Pros: Fast access using indices (O(1)).  Cons: Static size, costly insert/delete.  Example: int arr[5] = {10, 20, 30, 40, 50};
  • 5.
    Linked Lists Dynamic datastructure consisting of nodes pointing to the next node. Types: Singly, Doubly, Circular Pros: Dynamic size, efficient insert/delete. Cons: Slow access time (O(n)). Example Node Structure: class Node { int data; Node* next; };
  • 6.
    Stack (LIFO)  Last-In,First-Out structure.  Operations: push(), pop(), top(), isEmpty()  Used in: Function calls, expression parsing. stack<int> s; s.push(10);
  • 7.
    Queue (FIFO) Student s1; s1.name= "Alice"; s1.age = 20;
  • 8.
    Practical Applications ofData Structures ➢ Real-World Examples: • Trees: File system directories. • Graphs: Social network connections. • Hash Tables: Database indexing. • Heaps: Task scheduling systems. ➢ Case Study: ❑ Implementing a Contact List: • Array: Suitable for a fixed number of contacts with quick index-based access. • Linked List: Suitable for a dynamic list where contacts are frequently added or removed. • Hash Table: Suitable for quick search operations based on contact names.
  • 9.
    Introduction to ADT(Abstract Data Type)
  • 10.
    ADT (Abstract DataType) • ADT is a logical description of how data is viewed and the operations on it. • Does not depend on implementation. • Examples of ADTs: List, Stack, Queue, Tree List ADT: • A collection of ordered elements. • Elements can be inserted or deleted at specific positions. • Types: Singly List, Doubly List, Circular List, Array- based List
  • 11.
    Array-Based List ADTOverview • Uses a fixed-size array to store elements. • Elements are contiguously stored in memory. • Efficient random access, but costly insertions/deletions in the middle.
  • 12.
    Operations in ArrayList ADT ❖Create Structure of Array ❖Insert(pos, value): Add element at given index ❖Remove(pos): Remove element at index ❖Search(value): Find index of element ❖Display(): Print all elements ❖isEmpty() / isFull(): Check list status
  • 13.
    Structure of Array-BasedList class ArrayADT { private: int* arr; int capacity; int size; arr: Pointer to dynamic array capacity: Maximum number of elements the list can store size: Current number of elements
  • 14.
    Constructor & Destructor ArrayADT(intcap) { capacity = cap; size = 0; arr = new int[capacity]; } ~ArrayADT() { delete[] arr; } Explanation: Allocates dynamic memory using new. Frees memory in destructor.
  • 15.
    Check if thearray is empty Or full // Check if the array is empty bool isEmpty() { return size == 0; } // Check if the array is full bool isFull() { return capacity == size; }
  • 16.
    Insert Operation void insert(intvalue, int pos) { if (isFull()) { cout << "Array is fulln"; return; } if (size >= capacity || pos < 0 || pos > size) { cout << "Insert failed.n"; return; } for (int i = size - 1; i >= pos; i--) { arr[i + 1] = arr[i];} arr[pos] = value; size++; }
  • 17.
    Remove Operation void remove(intpos) { if (isEmpty()) { cout << "Array is emptyn"; return; } if (pos < 0 || pos >= size) { cout << "Delete failed.n"; return; } for (int i = pos; i < size - 1; i++) arr[i] = arr[i + 1]; size--; }
  • 18.
    Search Operation int search(intvalue) { for (int i = 0; i < size; i++) { if (arr[i] == value) return i; } return -1; } Explanation: •Returns index of first occurrence or -1 if not found.
  • 19.
    Update Operation void update(intpos, int value) { if (pos < 0 || pos >= size) { cout << "Update failed.n"; return; } arr[pos] = value; } Explanation: •Directly updates value at given index.
  • 20.
    Get Operation int get(intpos) { if (pos < 0 || pos >= size) { cout << "Invalid position.n"; return -1; } return arr[pos]; } Explanation: •Returns value at a given index.
  • 21.
    Display Operation void display(){ cout << "Array: "; for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } Explanation: •Traverses and prints all elements.
  • 22.
    Main Function Example intmain() { ArrayADT a(10); a.insert(10, 0); a.insert(20, 1); a.insert(15, 1); a.display(); a.remove(1); a.display(); int index = arr.Search(15); if (index != -1) cout << "Element found at index " << index << "n"; else cout << "Element not foundn"; cout << (arr.isEmpty() ? "Array is emptyn" : "Array is not emptyn"); cout << (arr.isFull() ? "Array is fulln" : "Array is not fulln"); return 0; }
  • 23.
    Complete Code class ArrayADT{ private: int* arr; int capacity; int size; public: ArrayADT(int cap) { capacity = cap; size = 0; arr = new int[capacity]; } ~ArrayADT() { delete[] arr; } void insert(int value, int pos) { if (size >= capacity || pos < 0 || pos > size) { cout << "Insert failed.n"; return; } for (int i = size - 1; i >= pos; i--) { arr[i + 1] = arr[i];} arr[pos] = value; size++;} void remove(int pos) { if (pos < 0 || pos >= size) { cout << "Delete failed.n"; return; } for (int i = pos; i < size - 1; i++) arr[i] = arr[i + 1]; size--; } int search(int value) { for (int i = 0; i < size; i++) { if (arr[i] == value) return i; } return -1; }
  • 24.
    void update(int pos,int value) { if (pos < 0 || pos >= size) { cout << "Update failed.n"; return; } arr[pos] = value; } int get(int pos) { if (pos < 0 || pos >= size) { cout << "Invalid position.n"; return -1; } return arr[pos]; } void display() { cout << "Array: "; for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } }; int main() { ArrayADT a(10); a.insert(10, 0); a.insert(20, 1); a.insert(15, 1); a.display(); a.remove(1); a.display(); cout << "Index of 20: " << a.search(20) << endl; a.update(1, 99); a.display(); cout << "Element at index 1: " << a.get(1) << endl; cout << (arr.isEmpty() ? "Array is emptyn" : "Array is not emptyn"); cout << (arr.isFull() ? "Array is fulln" : "Array is not fulln"); return 0; }