Lecture 2 Basic Data Structures (Arrays and LinkedLists)
Lecture 2 Basic Data Structures (Arrays and LinkedLists)
Data Structures
Module Name- Basic Data
Structures
Class - Day 2
EditEdit
MasterMaster
Topic Name: Basic Data
texttext
Structures and Stacks
stylesstyles
Today’s Agenda
● Data Structures
● Studying basic ADT: List
● Implementations of Lists (Arrays and Linked List)
Data Structures
● This statement can be easily justified with the difference in speeds of the
operations like reading an element or adding an element when the data is
arranged as an Array List and when it is arranged as a Linked List.
Data Structures
● A data structure is a specialized format for storing and processing data which
provides us with an efficient way to use and modify our data.
○ Functionalities
○ Internal Design
○ Applications
Data Structures
● The terms data structures and Abstract Data Types are generally confused
with each other, but they are not the same.
● Data Structures like array lists and linked lists implement this interface.
Studying basic ADTs: List
● List ADT
○ A sequence of “n” elements of same type (n>=0) :
● List ADT
○ Define: END(L) (returns position following the last element or An)
○ Operations on List ADT
■ INSERT (x, p, L) :
● Inserts “x” at position “p” in List L (shifts elements to right)
■ LOCATE (x, L) :
● Returns position “p” in List L if element x is found at “p” or returns END(L)
■ RETREIVE (p, L) :
● Returns element at position “p” in List L and UNDEFINED if p = END(L) or p
is not a position in L
■ DELETE (p, L) :
● Removes an element at position “p” of List L
● Undefined if p = END(L) or p is not a valid position
Studying basic ADT: List
● List ADT
○ Operations on List ADT
■ NEXT (p, L) :
● Returns the POSITION following “p” in the list
● END(L) if p = n (last position of the list)
● Undefined if p = END(L)
● Undefined is “p” is not a position in L
■ PREVIOUS (p, L) :
● Returns the POSITION preceding “p” in the list
● Undefined if p = 1
● Undefined is “p” is not a position in L
■ MAKENULL (L) :
● Causes L to become empty list and returns position END(L)
■ FIRST (L) :
● Returns first position in List L
● END(L) if list is empty
Studying basic ADT: List
● List ADT
○ Operations on List ADT
■ PRINTLIST (L) :
● Print elements in the order of occurrence
Implementation of List ADT: Arrays
0 1 2 3 4 5 n-1
A1 A2 A3 A4 A5 A6 ..... An
IntegerListNode IntegerListNode
IntegerListNode
A1 A2 A3
An
.....
head
list1 (object)
Implementation of List ADT: Linked List
Singly Linked list O(1) (FIRST) O(1) (at FIRST) O(1) (at FIRST) O(1) (at FIRST)
Implementation of List ADT: Linked List
Let’s now look at the various operations that can be performed on our newly
learnt data structure:
● pop(6);
● pop(5);
● pop(4);
● pop(3);
● pop(2);
Operations of Stack
Let’s see the visualization of the push and pop commands in these two slides:
● push(2);
● push(3);
● push(4);
● push(5);
● push(6);
Operations of Stack
There is a Stack class in Java which implements stack data structure. The class
provides the following functions:
`
● push(object element)- inserts the element onto the top of the stack
● search(object element)- searches for the element in the stack and returns
its location in the stack, If the element is not present then it returns ‘-1’
java.util package has Stack class
equals() method and toString() method belong to Object class
equals() method compares two OBJECT REFERENCES for EQUALITY using the “==“ operator
equals() method and toString() method belong to Object class
equals() method compares two OBJECT REFERENCES for EQUALITY using the “==“ operator
Operations of Stack
● Overflow: Trying to push an element into a stack that is already at its max
capacity
Operations of Stack
● Congratulations, you have completed the basics of stack data structure. Let’s
put this knowledge to use now in an attempt to solve a very common and
important question, Parentheses Matching.
● One of the things that must happen for a program to be well-formed is that all
its parentheses should match, i.e. if there’s an open parenthesis anywhere in
the program, there must also be a corresponding closing parenthesis.
Matching Parentheses Problem
We can use a very basic filter to separate a few of the non-matching parenthesis:
● Start scanning from the left and one by one, scan each character of the string.
● If the value is 0, the string is well formed and does not have matching
parenthesis. If non zero(either positive or negative), we conclude that the
parenthesis are matching.
Matching Parentheses Problem
● The approach we just saw is very efficient when we have to deal with a
single type of bracket (either ‘(’ or ‘{’).
● But as we saw in examples earlier, there may be string with both types of
brackets, for eg. “({}){}”.
● The answer is NO. We will have to use multiple counters each of which
keeps the track of count of a particular type of bracket.
● The output will be true only if all the counters have 0 value after string
traversal.
● As we saw, the code with two counters became a little entangled with the
conditional statements.
● Moreover, this algorithm doesn’t work all the times. What about a string like
“({)}”? It is clear that brackets aren’t matching. But what does our logic say?
● we can use stacks to solve this problem for both single and multiple bracket
types.
Implementation of Stack
public T pop(){
this has if (this.list.size() > 0) {
the reference T e = list.get(list.size() - 1);
to currently list.remove(list.size() - 1);
calling object return e;
}
throw new EmptyStackException(); }
Ignore the
errors
Matching Parentheses Problem
The use of stacks really simplifies the algorithm for us as we don’t have to
maintain any counter. So, let’s see how the approach actually works:
Opening Parenthesis : PUSH “(“ into stack
1. Initialise stack S to be empty.
Closing Parenthesis : POP “(“ from the stock
2. Scan the string from left to right. As you go symbol by symbol, whenever you
meet an open parenthesis, push ’(’ into S.
4. If you reach the end of string, and S is empty now, it means that the string is
well-formed; so return true. Otherwise, return false.
Matching Parentheses Problem
Let’s see the approach to solve the multiple bracket problems using stacks now:
4. If you meet a close parenthesis, pop a symbol from S. If it’s not an open
parenthesis (e.g. if it’s an open brace), return false. Also, if S is already empty,
it means error; so return false.
Matching Parentheses Problem
5. If you meet a close brace, pop a symbol from S. If it’s not an open brace
(e.g.
if it’s an open parenthesis), return false. Also, if S is already empty, it means
error; so return false.
6. If you reach the end of string, and S is empty, it means that the string is
well-formed; so return true. Otherwise, return false.
Let’s see a few sample inputs and outputs to understand the algorithm better.
Matching Parentheses Problem
for(char c : parens.toCharArray()) {
//STEP-2
if(c == '(' || c == '{') {
stack.push(c); }
//STEP-3
catch(EmptyStackException e) {
return false; }}
//STEP-5
if(stack.isEmpty()) {
return true;}
As an assignment, can you modify this code to take care of both square( “[ ]” ) and
angular( “< >” ) as well?
Let’s look at a very common example of stacks in our daily life, the back button on
our internet browsers:
● Every time you visit a website or the hyperlinks within a website, the contents
are pushed onto a stack and the stack keeps on increasing as you visit more
and more webpages from your current webpage.
● So, when the back button is pressed, we just pop the last webpage pushed
into the stack.
● The undo feature in text editors and some other softwares uses the same
core logic using stacks.
Applications of Stack
● So, the program stack first finds a reference to the function f(), pushes it into
the stack.
Applications of Stack
● Then, it finds the reference to function g(), then pushes it into the stack.
Eventually, it finds a reference to function h(), and pushes it into the stack.
Applications of Stack
● When, h() is solved, it is popped from stack. Then g() is solved and popped
and finally f() is popped.
Applications of Stack
Now, let’s see some examples where we use stacks in real life:
● Now, let’s the example of how an internet browser implements this stack
functionality.
○ Whenever you visit a website, it is added on top of a stack.
○ When you push the back button, the website at the top of the stack is
popped off, and you come at the site which was the previous visited
website.
● File versioning is very essential in today’s life because all the important
files(including this presentation) have many people working on the same
file.
● All the major collaborative softwares like google docs, dropbox use stacks
as their core data structure and each version of the file is a node in the
stack.
Applications of Stacks
OR
Thank You!
Happy learning!