Previous Lesson
○ Demonstrate the actions of Shell Sort for
the list of numbers given below:
CSC2103 –Data Structures 6, 56, 34, 23, 16, 4, 8, 1, 30, 41, 37, 52, 2
& Algorithms
CSC2103 - Data Structures & Algorithms
1 2
Data Structures Data Structure
○ A data structure is a systematic way of ○ Data structures are used in almost every
organizing a collection of data. program or software system.
○ A static data structure is one whose ○ Specific data structures are essential
capacity is fixed at creation. ingredients of many efficient algorithms,
● E.g.: array and make possible the management of
○ A dynamic data structure is one whose huge amounts of data, such as large
capacity is variable, so it can expand or databases and internet indexing services.
contract at any time. ○ Some formal design methods and
● E.g.: linked list, binary tree. programming languages emphasize data
○ For each data structure we need algorithms structures, rather than algorithms, as the
for insertion, deletion, searching, etc. 3 key organizing factor in software design. 4
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
3 4
Stack
○ A stack is a last-in-first-out sequence of
elements.
○ Elements can be added and removed only at
one end (the top of the stack).
Stack ○ The depth of stack is the number of
elements it contains. (Fixed or Dynamic)
○ An empty stack has depth zero.
CSC2103 - Data Structures & Algorithms
5 6
Stack Stack
○ Stack Applications
○ Illustration of Stack
○ Word processor – undo
○ Interpreter
Initially: After After adding After adding ● maintains a stack containing intermediate
removing a ASP.Net 2001 results during evaluation of complicated
book:
expressions
2001
C++ ASP. Net ASP>Net ○ Parser
Pascal Pascal Pascal Pascal
Java Java Java Java ● maintains a stack containing symbols
encountered during parsing.
7 8
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
7 8
Stack Stack implementation
○ Stack Requirements ○ Implementation using an array in Java
● It must be possible to make a stack empty. ○ Example :
● It must be possible to add (‘push’) an element public class Stack {
to the top of a stack.
static final int max = 1000;
● It must be possible to remove (‘pop’) the
topmost element from a stack. int top;
● It must be possible to test whether a stack is int arr[] = new int[max];
empty.
● It should be possible to access the topmost Stack(){
(‘peek’) element in a stack without removing
it. top = -1;
}
9 10
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
9 10
Stack Implementation Stack Implementation
○ Push operation ○ Pop operation
public void push(int x) { public int pop() {
if ((top >= max-1)) { if(top < 0) {
System.out.println("Stack is Full!"); System.out.println("Stack is empty!");
} return 0;
else { }
arr[++top] = x; else {
int x = arr[top--];
System.out.println(x + " is pushed into
stack"); System.out.println(x+ " is popped out");
} return x;
}
}
} 11 12
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
11 12
Stack Implementation Stack
public static void main(String args[]) { ○ Stack using Java collections framework
Stack s = new Stack(); import java.util.Stack;
s.push(100); public static void main(String args[]) {
s.push(200); Stack<Integer> s = new Stack<Integer>();
s.push(100);
s.push(300); s.push(200);
s.pop(); s.push(300);
} s.push(400);
s.pop();
System.out.print(s);
}
13 14
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
13 14
Stack implementation Stack Implementation
○ Implementation using an array in C++ ○ Push operation
void myStack::push(int temp)
○ Example : {
class myStack top++;
{ if(top<max)
{
private : a[top]=temp;
int top; }
int a[max]; else
public : {
myStack(); cout<<"STACK IS FULL"<<endl;
top--;
void push(int temp);
int pop(); }
}; }
15 16
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
15 16
Stack Implementation Stack Implementation
○ POP operation void main()
int myStack::pop() {
{ myStack s;
if(top==-1) s.push(3);
{
cout<<"STACK IS EMPTY!!!"<<endl;
cout<<"3 is Pushed\n";
return NULL; s.push(10);
} cout<<"10 is Pushed\n";
else s.push(17);
{ cout<<"17 is Pushed\n\n";
int data=a[top];
a[top]=NULL;
top--;
cout<<s.pop()<<" is Popped\n";
return data; cout<<s.pop()<<" is Popped\n";
} cout<<s.pop()<<" is Popped\n";
} }
17 18
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
17 18
STL -Library Stack using STL
○ Standard Template Library (STL) ○ Requirements
○ It is part of the C++ Standard Library #include<stack>
describing containers, iterators, and ○ Stack initialization
algorithms. stack<string> s1;
○ It provides many of the basic algorithms ○ Operations (few)
and data structures of computer science. s1.push();
https://www.geeksforgeeks.org/the-c- s1. pop();
standard-template-library-stl/ s1. empty()
s1. size()
19 20
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
19 20
Stack using STL
void main()
{
stack<string> s1;
s1.push("Red");
s1.push("Green");
s1.push("Blue");
cout<<"The total elements in the stack : "
Queues
<<s1.size()
<<endl;
while(!s1.empty())
{
cout<<s1.top()<<endl;
s1.pop();
}
21
}
CSC2103 - Data Structures & Algorithms
21 22
Queue Queue
○ Queue is a linear data structure in ○ A queue can be implemented using an
which data can be added to one array queue[1..N], where N is the size of
end and retrieved from the other. the array.
○ (FIFO, First In First Out) data ○ In a queue, items are added at the rear
structure and deleted from the front.
○ Queue Operations ● Two variables are used to track the indexes of
● Enqueue – add an element at the the rear and the front of the queue.
rear. ○ An empty queue has both variables (Front
● Dequeue – remove an element at the and Rear) equal to 0.
front
23 24
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
23 24
Queue Queue
○ When an item is added to a queue Queue implementation using Java
public class Queue {
● If the queue is empty: set both “Rear” and
“Front” to 1 and put the item at index 1 final int max = 1000 ;
● If the queue is not empty: increment “Rear” int[] arr;
and put the item at index “Rear”. int front;
○ When an item is deleted from a queue int rear;
● Increment “Front”.
● If “Front” is greater than the index of the public Queue(){
arr = new int[max];
○ last cell in the array, set “Front” to 1.
front = 0;
○ o if the queue becomes empty, set rear = 0;
○ “Rear”=“Front”=0. 25 } 26
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
25 26
Queue Dequeue operation
○ Enqueue operation
○ Dequeue operation
public void enque(int x ) {
if(rear >= max -1 )
public void deque(){
System.out.println("Queue is full!");
int value;
else if(front==-1) {
{ System.out.println("Queue is empty!");
arr[rear] = x; }
rear++; value=arr[front];
System.out.println(x + " is added to the queue"); arr[front]=0;
System.out.println(value+ " is removed");
}
if(front==rear-1) {
front=-1;
if ((front == -1)) { rear=0;
front = 0; }
} else
} front++;
27 } 28
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
27 28
main method Queue
class myQ
public static void main(String args[]) { {
Queue q = new Queue(); private:
q.enque(100); int a[max];
q.enque(200); int front;
q.enque(300); int rear;
public:
q.deque();
myQ();
}
void enQ(int);
int deQ();
29 }; 30
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
29 30
Queue Dequeue operation
○ Enqueue operation ○ Dequeue Operation
int deQ()
void myQ::enQ(int value)
{
{
int value;
if(rear == max - 1)
cout<<"Queue is FULL"<<endl;
if(front==-1)
a[rear] = value; {
rear ++; cout<<"QUEUE EMPTY!";
return NULL;
if(front == -1) }
front =0;
value=a[front];
} 31 a[front]=0; 32
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
31 32
Queue
if(front==rear-1)
{
front=-1;
rear=0; Linked List
}
else
front++;
return value;
}
33
CSC2103 - Data Structures & Algorithms
33 34
Linked List Linked List
○ Lists ○ We will discuss the concepts of List using
○ Stack and Queue are lists data structure with Linked List.
certain constraints specified:
● Stack
○ LIFO
● Queue
○ FIFO
○ A general list is a collection of items arranged in
some order, but without the constraints of Stack
or Queue; i.e., a general list allow items to be
inserted into or deleted from the list at any point
in the list. 35 36
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
35 36
Linked List Linked List
○ Linked List consists of a series of nodes,
each containing some data.
○ Each node also contains a pointer pointing
to the next node in the list.
○ There is an additional separate pointer
which points to the first node, and the
pointer in the last node simply points to
"NULL".
37 38
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
37 38
Linked List Linked List
○ The first task is to define a node. To do ○ The head and current pointers are
maintained to point to the first and to
this, we can associate a string with a
move along the list.
pointer using a structure definition:
node *head=NULL;
struct Node { node *current;
int data;
Node * next;
};
39 40
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
39 40
Linked List Linked List
○ Add new node else
{ temp2 = head;
void add_node()
{ // We know the list is not empty
node *temp1, *temp2; while(temp2->next != NULL)
temp1 = new node; {
cout << "Please enter a data: "; temp2 = temp2->next;
cin >> temp1->data;
temp1->next = NULL; }
temp2->next = temp1;
if (head == NULL)
{ }
head = temp1;
41
} 42
current = head;
} CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
41 42
Linked List Linked List
○ To insert a node in a specific position ○ Removing the head node from the list
void delete_head_node()
{
node *temp;
temp = head;
head = head->next;
delete temp;
}
43 44
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
43 44
Linked List Linked List
○ Moving forward direction ○ Moving backwards
void move_current_back ()
void move_current_on() { if (current == head)
{ cout << "You are at the start of the list" << endl;
else
if (current->next == NULL)
{ node *previous;
cout << "You are at the end of the list." previous = head;
<< endl;
else while (previous->next != current)
current = current->next; { previous = previous->next;
} }
current = previous;
}
45 } 46
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
45 46
Double Linked List Linked List Using Java
○ Java supports ArrayList and LinkedList
○ A list may also have a pointer back to
the previous node in the list. This list is public static void main(String args[]) {
known as Doubly Linked List. ArrayList list = new ArrayList();
// List list = new LinkedList();
list.add(100);
list.add(200);
list.add(300);
list.remove(1);
System.out.println(list);
47 } 48
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
47 48
Linked List Linked List
○ STL supports doubly linked list using ○ Access the values in the list using iterator
● #include<list> preprocessor for (nums_iter=nums.begin(); nums_iter != nums.end();
○ Declare list of integer numbers nums_iter++)
list<int> nums; {
*nums_iter = *nums_iter + 3; // Modify each element.
○ Can access elements in the list via iterator
cout << (*nums_iter) << endl;
list<int>::iterator nums_iter;
}
○ Few operations with list data strucure cout << endl;
nums.push_back (0);
nums.push_back (4);
nums.push_front (7);
49 50
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
49 50
Answer –Shell Sort
Exercise Answer
52
CSC2103 - Data Structures & Algorithms
51 52
Answer –Shell Sort
53
CSC2103 - Data Structures & Algorithms
53