CSC2103 Lecture 3
CSC2103 Lecture 3
CSC2103 –Data Structures 6, 56, 34, 23, 16, 4, 8, 1, 30, 41, 37, 52, 2
& Algorithms
1 2
○ 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
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
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
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
15 16
Stack Implementation Stack Implementation
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
17 18
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
CSC2103 - Data Structures & Algorithms CSC2103 - Data Structures & Algorithms
25 26
○ 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
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
33 34
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
35 36
Linked List Linked List
37 38
CSCI203 - Algorithms and Data Structures CSCI203 - Algorithms and Data Structures
37 38
○ 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
41 42
○ 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
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
Exercise Answer
52
51 52
Answer –Shell Sort
53
53