0% found this document useful (0 votes)
0 views

Data Structures

The document provides an overview of various data structures including Linear Lists, Linked Lists, Stacks, and Queues, detailing their operations such as Find, Search, Delete, and Insert. Each data structure is accompanied by code snippets that illustrate how these operations are implemented in C++. It also includes error handling for operations that exceed bounds or memory limits.

Uploaded by

hikalandsomeone
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)
0 views

Data Structures

The document provides an overview of various data structures including Linear Lists, Linked Lists, Stacks, and Queues, detailing their operations such as Find, Search, Delete, and Insert. Each data structure is accompanied by code snippets that illustrate how these operations are implemented in C++. It also includes error handling for operations that exceed bounds or memory limits.

Uploaded by

hikalandsomeone
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/ 37

Data Structures

List & Stack & Queue


Data Structures

Linear List
1-Find ()
Length=6
5 3 7 2 4 6
bool LinearList<T>::Find(int k, T& x) const
{
if (k < 1 || k > length) return false; Find(9,4) or Find(0,4)
x = element[k - 1];
return true;
}
5 3 7 2 4 6

Find(2,X)

Retuen X
2-Search ()
Length=6
5 3 7 2 4 6

template<class T>
Sreach(7)
int LinearList<T>::Search(const T& x) const
{ 5 3 7 2 4 6 if = 7 False
for (int i = 0; i < length; i++)
if (element[i] == x) return ++i;
return 0; 5 3 7 2 4 6 if = 7 False
}

5 3 7 2 4 6 if = 7 True

Return Index
Length=6
3-Delete () 5 3 7 2 4 6

Delete(3)
template<class T>
LinearList<T>& LinearList<T>::Delete(int k, T& x) 5 3 7 2 4 6 if = 3 False
{
if (Find(k, x))
{ 5 3 7 2 4 6 if = 3 True
for (int i = k; i < length; i++)
element[i-1] = element[i]; element[i-1]
5 7 7 2 4 6
length--; = element[i]
return *this;
} element[i-1]
5 7 2 2 4 6
else = element[i]
throw OutOfBounds();
} element[i-1]
5 7 2 4 4 6
= element[i]
length-- 5 7 2 4 6
element[i-1]
5 7 2 4 6 6
= element[i]
Length=5 Max Size = 6
4-Insert () 5 3 7 2 4

Insert(9,4) or Insert(0,4) if (k < 1 || k > 6)


Retuen False
template<class T>
LinearList<T>& LinearList<T>::Insert(int k, const T& x) Insert(6,4) length == MaxSize
{ Retuen False
if (k < 0 || k > length) throw OutOfBounds(); Insert(1,9)
if (length == MaxSize) throw NoMem(); element[i+1]
5 3 7 2 4
= element[i]
for (int i = length-1; i >= k; i--)
element[i+1] = element[i]; element[i+1]
element[k] = x; 5 3 7 2 4 4
= element[i]
length++;
return *this;
element[i+1]
} 5 3 7 2 2 4
= element[i]

element[i+1]
5 3 7 7 2 4
= element[i]

element[k] = x 5 9 3 7 2 4 element[i+1]
5 3 3 7 2 4
= element[i]
Data Structures
Null
Linked List
1-Find () Length=5

5 2 7 4 6
template<class T>
bool Chain<T>::Find(int k, T& x) const
if (k < 1)
{ Find(0,X)
return false
if (k < 1) return false;
ChainNode<T> *current = first; Find(4,X)
int index = 1;
while (index < k && current) 5 2 7 4 6
{
if index = 4 False
current = current->link;
index++;
} 5 2 7 4 6
if (current)
{ if index = 4 False
x = current->data;
return true;
5 2 7 4 6
}
return false;
if index = 4 True
}
X=4
Return True
2-Search () 5 2 7 4 6

Search(4)

template<class T>
5 2 7 4 6
int Chain<T>::Search(const T& x) const
{
if Value = 4 False
ChainNode<T> *current = first;
int index = 1;
5 2 7 4 6
while (current && current->data != x)
{
if Value = 4 False
current = current->link;
index++;
} 5 2 7 4 6

if (current) return index;


return 0; if Value = 4 False
}
5 2 7 4 6

if Value = 4 True

Return Index
3-Delete () Delete -> First Length=5

template<class T> 5 2 7 4 6
Chain<T>& Chain<T>::Delete(int k, T& x)
{
Delete(9,4) or Delete(0,4) if (k < 1 || k > 5)
if (k < 1 || !first)
Retuen False
throw OutOfBounds();
ChainNode<T> *p = first; First
if (k == 1) If Delete -> First
first = first->link;
else 5 2 7 4 6
{
ChainNode<T> *q = first;
for (int index = 1; index < k - 1 && q; index++)
q = q->link; 5 2 7 4 6
if (!q || !q->link)
throw OutOfBounds();
p = q->link;
q->link = p->link; 5 2 7 4 6
}
x = p->data;
delete p;
return *this; 2 7 4 6
}
3-Delete ()
5 2 7 4 6
template<class T>
Chain<T>& Chain<T>::Delete(int k, T& x)
{ Delete (4,X)
if (k < 1 || !first)
throw OutOfBounds();
ChainNode<T> *p = first; 5 2 7 4 6
if (k == 1)
first = first->link;
else
{ 5 2 7 4 6
ChainNode<T> *q = first;
for (int index = 1; index < k - 1 && q; index++)
q = q->link;
if (!q || !q->link)
throw OutOfBounds(); 5 2 7 4 6
p = q->link;
q->link = p->link;
}
x = p->data;
delete p; 5 2 7 6
return *this;
}
3-Delete () Delete -> End

5 2 7 4 6

End

5 2 7 4 6

5 2 7 4
Data Structures

Linear Stack
1-Stack() constructor
Max Size = 6 Max Top = 6-1
template<class T>
Stack<T>::Stack(int MaxStackSize)
{
// Stack constructor.
MaxTop = MaxStackSize - 1;
stack = new T[MaxStackSize];
Top = -1
top = -1;
}
2-Top()
Max Size = 6
template<class T>
T Stack<T>::Top() const
5 3 2 0
{
// Return top element.
if (IsEmpty())
throw OutOfBounds(); // Top fails IsEmpty() False
else
Top
return stack[top];
}
return stack[top]
3-Add ()
Max Size = 6
template<class T>
Stack<T>& Stack<T>::Add(const T& x)
5 3 2 0
{
// Add x to stack.
if (IsFull()) throw NoMem();
Add(0) Top IsFall() False
stack[++top] = x;
return *this;
} 5 3 2 0 0

Top
4-Delete ()
template<class T> Max Size = 6
Stack<T>& Stack<T>::Delete(T& x)
{ 5 3 2 0 0
// Delete top element and put in x.
if (IsEmpty()) throw OutOfBounds();
x = stack[top--]; // NOT x = stack[--top]; Delete(X) Top IsEmpty() False
return *this;
}
5 3 2 0 0

Top
Data Structures
Null
Linked Stack
1-Stack() destructor
Top Length=5

5 3 2 0 0

Next
template<class T> Next

LinkedStack<T>::~LinkedStack() 5 3 2 0 0
2 0 0
{
Top
Node<T> *next; Top

while (top) 3 2 0 0
0 0
{
Next
next = top->link; Next
delete top; 3 2 0 0
0 0
top = next;
Top
} Top
} 2 0 0
0

Next

0 Null

Top

Null
2-IsFull()
Top Length=5
template<class T>
bool LinkedStack<T>::IsFull() const 5 3 2 0 0
{
//Is the stack full?
try p
{
Node<T> *p = new Node<T>; New Node 0 Delete p
delete p;
return false;
}
catch (NoMem) {return true;} Return True (Fall)
}
3-Top()
template<class T>
T LinkedStack<T>::Top() const
{ Top Length=5
// Return top element.
5 3 2 0 0
if (IsEmpty()) throw OutOfBounds();
return top->data;
}
Return 5
4-Add () Top Length=5 Max Size =6

5 3 2 0 0

template<class T>
LinkedStack<T>& LinkedStack<T>::Add(const T& x) Top
{
// Add x to stack. 5 3 2 0 0

Node<T> *p = new Node<T>;


p->data = x; p
p->link = top;
Top
top = p;
return *this; 5 3 2 0 0
}

p
6
Top

5 3 2 0 0

6 Top

6 5 3 2 0 0
5-Delete () Top Length=5 Max Size =6

5 3 2 0 0

template<class T>
LinkedStack<T>& LinkedStack<T>::Delete(T& x)
{ Top
// Delete top element and put it in x.
p 5 3 2 0 0
if (IsEmpty()) throw OutOfBounds();
x = top->data;
Node<T> *p = top;
Top
top = top->link;
delete p; p 5 3 2 0 0
return *this;
}
Top

3 2 0 0
Data Structures

Linear Queue
1-Queue() constructor
template<class T>
Front Max Size = 6 Max
Queue<T>::Queue(int MaxQueueSize)
{ QueueSize = 6-1
// Create an empty queue whose capacity is Rear
MaxQueueSize.
MaxSize = MaxQueueSize + 1;
queue = new T[MaxSize];
front = rear = 0;
}
2-First()
Max Size = 6

Front Rear

5 3 2 0
template<class T>
T Queue<T>::First() const
Is Empty False
{
// Return first element of queue. Throw
Return (Front + 1) % MaxSize
OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds(); Return (0+1)%6
return queue[(front + 1) % MaxSize]; Front Rear
}
5 3 2 0

Return 5
3-Last()
Max Size = 6

Front Rear
template<class T>
T Queue<T>::Last() const 5 3 2 0
{
// Return last element of queue. Throw
Is Empty False
OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds();
Return Rear
return queue[rear];
}
Return 0
4-Add()
Max Size = 6

Front Rear

5 3 2 0
template<class T>
Queue<T>& Queue<T>::Add(const T& x)
{ Add(0)
// Add x to the rear of the queue. Throw
Is Full False
NoMem exception if the queue is full.
if (IsFull()) throw NoMem(); Front Rear
rear = (rear + 1) % MaxSize;
queue[rear] = x; 5 3 2 0

return *this;
} Front Rear

5 3 2 0 0
5-Delete()
Max Size = 6

Front Rear

5 3 2 0
template<class T>
Queue<T>& Queue<T>::Delete(T& x)
{ Delete (X)
// Delete first element and put in x. Throw
Is Empty False
OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds(); Front Rear
x = queue[front];
front = (front + 1) % MaxSize; 5 3 2 0

return *this;
} X = Front = 5

Front Rear

5 3 2 0
Data Structures
Null
Linked Queue
Length=5

1-Queue() destructor Front Rear

5 3 2 0 0

template<class T>
Next Rear
LinkedQueue<T>::~LinkedQueue()
{
5 3 2 0 0
// Queue destructor. Delete all nodes. Next Rear
Node<T> *next; Front Rear
2 0 0
while (front)
{ 3 2 0 0 Front Rear
next = front->link;
Next Rear 0 0
delete front;
front = next; Next
3 2 0 0 Rear
}
} Front Rear 0 0

2 0 0 Front Rear

Next Rear

0 Null

Front Rear

Null
2-IsFull()
template<class T> Length=5
bool LinkedQueue<T>::IsFull() const Front Rear
{
// Is the queue full? 5 3 2 0 0

Node<T> *p;
try
{ p
p = new Node<T>;
delete p; New Node 0 Delete p
return false;
}
catch (NoMem)
{ Return True (Fall)
return true;
}
}
3-First()
Length=5
template<class T> Front Rear
T LinkedQueue<T>::First() const
{ 5 3 2 0 0

// Return first element of queue. Throw


OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds(); Return 5
return front->data;
}
4-Last()
template<class T> Length=5
T LinkedQueue<T>::Last() const Front Rear
{
5 3 2 0 0
// Return last element of queue. Throw
OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds();
return rear->data; Return 0
}
Length=5
Front Rear
5-Add()
5 3 2 0

template<class T>
LinkedQueue<T>& LinkedQueue<T>::Add(const T& x) Add(0)
{ Front Rear
// Add x to rear of queue. Do not catch possible
NoMem exception thrown by new. 5 3 2 0

// create node for new element


Node<T> *p = new Node<T>; p
p->data = x;
p->link = 0;
Front Rear
// add new node to rear of queue
5 3 2 0
if (front)
rear->link = p; // queue not empty
else p
0
front = p; // queue empty
rear = p; Front Rear

return *this; 5 3 2 0
}

p 0

Front Rear

5 3 2 0 0
6-Delete() Front Rear

5 3 2 0 0
template<class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x)
{
Delete (X)
// Delete first element and put it in x. Throw
OutOfBounds exception if the queue is empty.
X=5
Front Rear
if (IsEmpty()) throw OutOfBounds(); p
5 3 2 0 0
// save element in first node
x = front->data; Front Rear
p
// delete first node 5 3 2 0 0
Node<T> *p = front;
front = front->link;
Front Rear
delete p;
3 2 0 0
return *this;
}
Thank You

You might also like