0% found this document useful (0 votes)
4 views20 pages

Unit1 Notes 20210129105526

Unit 1 of the Advanced Data Structures course covers complexity analysis, abstract data types (ADT), and various data structure implementations including arrays and linked lists. It discusses asymptotic and amortized complexity, the concept of interfaces in Java and C++, and provides examples of array and linked list operations. Additionally, it introduces the iterator concept for traversing collections in Java.

Uploaded by

jgpntrip2023
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)
4 views20 pages

Unit1 Notes 20210129105526

Unit 1 of the Advanced Data Structures course covers complexity analysis, abstract data types (ADT), and various data structure implementations including arrays and linked lists. It discusses asymptotic and amortized complexity, the concept of interfaces in Java and C++, and provides examples of array and linked list operations. Additionally, it introduces the iterator concept for traversing collections in Java.

Uploaded by

jgpntrip2023
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/ 20

Department of Computer Science and Engineering

PES UNIVERSITY
UE20CS502: Advanced Data Structures (4-0-0-4-4)

UNIT 1

Ms. Kusuma K V

www.pes.edu
Unit 1 : Complexity, Amortized Analysis, Abstract Data Types(ADT)
Asymptotic Complexity Notations, Amortized Complexity Analysis of Stacks,
Binary Counters, and Dynamic Tables, Concept of interface and
implementation, Array as an ADT: Different types of Array Implementations.
List Interface & List implementations, Concept of Iterator: Operations on Lists
and Arrays – traverse, search, replace, reverse, copy.

Asymptotic Complexity Notations


Section 3.1 from “Introduction to Algorithms”, T. H Cormen, C E Leiserson, R L
Rivest and C Stein, Prentice-Hall of India, 3rd Edition, 2010.

Amortized Complexity Analysis of Stacks, Binary Counters, and Dynamic


Tables
Chapter 17 from “Introduction to Algorithms”, T. H Cormen, C E Leiserson, R L
Rivest and C Stein, Prentice-Hall of India, 3rd Edition, 2010.

Concept of interface and implementation


Interface and Implementation separation in Java
An interface in Java says what the user of a class implementing this interface
can do. It specifies the method signatures and has no implementation. So
these methods are abstract and public. A class implementing the interface
should override every method of the interface to become concrete.
interface Interface
{
void method1();
void method2();
}
class Concrete implements Interface {
private int myMember;

public void method1()


{
// Your implementation
}

www.pes.edu
public void method2()
{
// Your implementation
}
};

Example:
//Displayable.java
package interfacedemoads;

public interface Displayable


{
public void area();
}

//Rectangle.java
package interfacedemoads;

public class Rectangle implements Displayable


{
private double length,breadth;
public Rectangle(double l,double b)
{
length=l;
breadth=b;
}
@Override
public void area()
{
System.out.println("Area of rectangle="+length*breadth);
}
}

www.pes.edu
//Circle.java
package interfacedemoads;

public class Circle implements Displayable {


private double radius;
public Circle(double r)
{
radius=r;
}
@Override
public void area()
{
System.out.println("Area of circle="+3.142*radius*radius);
}
}

// InterfaceDemoADS.java
package interfacedemoads;

public class InterfaceDemoADS {


public static void main(String[] args) {
Rectangle r=new Rectangle(2,3.5);
Circle c=new Circle(1);

r.area();
c.area();
}
}
Interface and Implementation separation in C++
In C++ we do not have the concept of interface separately. Abstraction may be
provided by using abstract class. A class having at least one pure virtual
function becomes an abstract class. Classes inheriting the Abstract Base Class
should override all the pure virtual functions to become concrete. Otherwise
the derived class also becomes abstract. Pure virtual function is a virtual
function declared in the class using =0 just before the semicolon. The =0
may appear only on the declaration of a virtual function in the class.

www.pes.edu
class Interface {
public:
virtual void method1() = 0; // "=0" part makes this method pure virtual, and
//also makes this class abstract
virtual void method2()=0;
};

class Concrete : public Interface {


private:
int myMember;
public:
Concrete(){}
~Concrete(){}
void method1();
void method2();
};
/ /Provide implementation for the first method
void Concrete::method1() {
// Your implementation
}
// Provide implementation for the second method
void Concrete::method2() {
// Your implementation
}

Array as an ADT: Different types of Array Implementations


In computer science, an abstract data type (ADT) is a mathematical model for
data types, where a data type is defined by its behavior (semantics) from the
point of view of a user of the data, specifically in terms of possible values,
possible operations on data of this type, and the behavior of these operations.
This contrasts with data structures, which are concrete representations of
data, and are the point of view of an implementer, not a user.
Formally, an ADT may be defined as "a class of objects whose logical behavior
is defined by a set of values and a set of operations".

www.pes.edu
• The array is a basic abstract data type that holds an ordered collection of
items accessible by an integer index
• These items can be anything from primitive types such as integers to
more complex types like instances of classes
• Since it's an ADT, it doesn't specify an implementation, but is almost
always implemented by an array (data structure) or dynamic array
• Operations supported:
• Read an Element
• Write an Element
List Interface and List Implementations
A list is an abstract data type that represents a countable number of ordered
values, where the same value may occur more than once.

ArrayList
//arList.h
#define MAX 50
typedef struct arlist {
int a[MAX];
int last;
}ARLIST;

//init: initialize the array


void init(ARLIST *pal);

//append: insert element ele to the list pointed to by pointer pal


//returns 1 on success, 0 on failure
int append(ARLIST *pal,int ele);

//delLast: delete last element in the array List and


//(return back implicitly) i.e., store the deleted element in the pointer pe
//return back (explicitly) the success (1) or failure status (0)
int deleteLast(ARLIST *pal,int *pe);

//display: displays all the elements in the list


void disp(ARLIST *pal);

www.pes.edu
//arListimpl.c
#include"arList.h"
#include<stdio.h>

void init(ARLIST *pal)


{
pal->last=-1;
}

int append(ARLIST *pal,int ele)


{
if(pal->last==MAX-1)
return 0;
pal->last++;
pal->a[pal->last]=ele;
return 1;
}

int deleteLast(ARLIST *pal,int *pe)


{
if(pal->last==-1)
return 0;
*pe=pal->a[pal->last];
pal->last--;
return 1;
}

void disp(ARLIST *pal)


{
if(pal->last==-1)
printf("List is empty\n");
for(int i=0;i<=pal->last;i++)
printf("%d\t",pal->a[i]);
}

www.pes.edu
//arList.c
#include"arList.h"
#include<stdio.h>

int main() {
ARLIST lstobj;
int choice,ele,status;

init(&lstobj);

do{
printf("1.Append\t2.DeleteLast\t3.Display\n");
scanf("%d",&choice);

switch(choice)
{
case 1:printf("Enter an element\n");
scanf("%d",&ele);
status=append(&lstobj,ele);
if(status==1)
printf("%d is successfully inserted\n",ele);
else
printf("append failed\n");
break;
case 2:status=deleteLast(&lstobj,&ele);
if(status==1)
printf("%d deleted successfully\n",ele);
else
printf("delete failed\n");
break;
case 3:disp(&lstobj);
break;
}
}while(choice<4);
return 0;
}

www.pes.edu
Linked List
//llist.h
typedef struct node
{
int info;
struct node *next;
}NODE;

typedef struct llist


{
NODE* head;
}LLIST;

void init(LLIST *pl);


int insertFirst(LLIST *pl,int e);
int insertLast(LLIST *pl,int e);
int delFirst(LLIST *pl,int *pe);
int delLast(LLIST *pl,int *pe);
void disp(LLIST *pl);
void destroyList(LLIST *pl);
int countNodes(LLIST *pl);
int insertAtPos(LLIST *pl,int e,int pos);

//llistImpl.c
#include"llist.h"
#include<stdlib.h>
#include<stdio.h>

void init(LLIST *pl)


{
pl->head=NULL;
}

www.pes.edu
int insertFirst(LLIST *pl,int e)
{
NODE *temp;

temp=(NODE*)malloc(sizeof(NODE));
if(temp==NULL)
return 0;

temp->info=e;
temp->next=pl->head;
pl->head=temp;
return 1;
}

int insertLast(LLIST *pl,int e)


{
NODE *temp,*p=pl->head;
temp=(NODE*)malloc(sizeof(NODE));
if(temp==NULL)
return 0;

temp->info=e;
temp->next=NULL;

if(pl->head==NULL)
pl->head=temp;
else
{
while(p->next!=NULL)
p=p->next;

p->next=temp;
}
return 1;
}

www.pes.edu
int delFirst(LLIST *pl,int *pe)
{
NODE *p=pl->head;

if(p==NULL)
return 0;
pl->head=p->next;
*pe=p->info;
free(p);
return 1;
}

int delLast(LLIST *pl,int *pe)


{
NODE *p,*q=NULL;
p=pl->head;

if(p==NULL)
return 0;

//Single Node
if(p->next==NULL)
{
pl->head=NULL;

*pe=p->info;
free(p);
return 1;
}

//Multiple Node
while(p->next!=NULL)
{
q=p;
p=p->next;
}

www.pes.edu
q->next=NULL;
*pe=p->info;
free(p);
return 1;
}

void disp(LLIST *pl)


{
NODE *p=pl->head;
if(p==NULL)
printf("Empty List\n");
else
{
while(p!=NULL)
{
printf("%d ",p->info);
p=p->next;
}
}
}

void destroyList(LLIST *pl)


{
NODE *p=pl->head;
NODE *q=NULL;

while(p!=NULL)
{
q=p;
p=p->next;
// printf("%d deleted\n",q->info);
free(q);
}
pl->head=NULL;
}

www.pes.edu
int countNodes(LLIST *pl)
{
NODE *p=pl->head;
int count=0;

while(p!=NULL)
{
count++;
p=p->next;
}
return count;
}

int insertAtPos(LLIST *pl,int e,int pos)


{
NODE *temp,*p,*q;

int n=countNodes(pl);

if(pos>n || pos<0)
return 0;

temp=(NODE*)malloc(sizeof(NODE));
temp->info=e;
temp->next=NULL;

if(pos==0)
{
temp->next=pl->head;
pl->head=temp;
}
else
{
p=pl->head;
for(int i=0;i<pos;i++)
{

www.pes.edu
q=p;
p=p->next;
}

temp->next=p;
q->next=temp;
}
return 1;
}

//llistc.c
#include"llist.h"
#include<stdio.h>

int main()
{
LLIST lobj;
int choice,ele,status,pos;

init(&lobj);
do
{
printf("\n1.InsertFirst 2.InsertLast 3.DelFirst 4.DelLast 5.Disp
6.InsertAtPos\n");
printf("Enter your choice\n");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("Enter the element\n");
scanf("%d",&ele);

status=insertFirst(&lobj,ele);
if(status==1)
printf("%d is inserted successfully\n",ele);
else

www.pes.edu
printf("Insertion Failed\n");

break;
case 2: printf("Enter the element\n");
scanf("%d",&ele);

status=insertLast(&lobj,ele);
if(status==1)
printf("%d is inserted successfully\n",ele);
else
printf("Insertion Failed\n");

break;
case 3: status=delFirst(&lobj,&ele);
if(status==1)
printf("%d is deleted successfully\n",ele);
else
printf("Deletion Failed\n");

break;

case 4: status=delLast(&lobj,&ele);
if(status==1)
printf("%d is deleted successfully\n",ele);
else
printf("Deletion Failed\n");
break;
case 5:disp(&lobj);
break;

case 6: printf("Enter the element\n");


scanf("%d",&ele);
printf("Enter the position\n");
scanf("%d",&pos);
if(insertAtPos(&lobj,ele,pos))
printf("%d is inserted successfully\n",ele);

www.pes.edu
else
printf("Insertion Failed\n");
break;
}
}while(choice<7);

destroyList(&lobj);
return 0;
}
Array List and Linked List
Random Access
• Array List is better than Linked List

Random Insertion/Deletion
• Linked List is better than Array List

Size
• Fixed in Array List and Dynamic in Linked List

Concept of Iterator: Operations on Lists and Arrays – traverse, search,


replace, reverse, copy
Linked List in Java
• Linked List is a part of the Collection framework present in java.util
package.
• This class is an implementation of the LinkedList data structure which is
a linear data structure where the elements are not stored in contiguous
locations and every element is a separate object with a data part and
address part. The elements are linked using pointers and addresses.
• Each element is known as a node. Due to the dynamicity and ease of
insertions and deletions, they are preferred over the arrays.
• It also has few disadvantages like the nodes cannot be accessed directly
instead we need to start from the head and follow through the link to
reach to a node we wish to access.

www.pes.edu
• ‘Iterator’ is an interface which belongs to collection framework. It allows
us to traverse the collection, access the data element and remove the
data elements of the collection
• java.util package has public interface Iterator and contains three
methods:
o boolean hasNext() : It returns true if Iterator has more element to
iterate.
o Object next() : It returns the next element in the collection until
the hasNext() method returns true
o void remove() : It removes the current element in the collection
• The iterator() method can be used to get an Iterator for any collection

//ArrayListOpns.java
import java.util.Collections;
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorDemoADS {


public static void main(String a[]){
ArrayList<String> arrl = new ArrayList<String>();

//adding elements to the end


arrl.add("First");
arrl.add("Second");
arrl.add("Third");
arrl.add("Random");

//The iterator() method can be used to get an Iterator for any collection
Iterator<String> itr = arrl.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}

www.pes.edu
//Reverse: reverses the order of the elements in a List
Collections.reverse(arrl);
System.out.println(arrl);
ArrayList<String> arrl1=new ArrayList<>();
arrl1.add("a");
arrl1.add("b");

//Copy: takes two arguments, a destination List and a source List, and copies
//the elements of the source into the destination, overwriting its contents. The
//destination List must be at least as long as the source. If it is longer, the
//remaining elements in the destination List are unaffected.
Collections.copy(arrl, arrl1);
System.out.println(arrl);
System.out.println(arrl1);

/*Search: takes a List and an element to search for (the "search key"). If the
List contains the search key, its index is returned. If not, the return value is (-
(insertion point) - 1), where the insertion point is the point at which the value
would be inserted into the List, or the index of the first element greater than
the value or list.size() if all elements in the List are less than the specified value.
This admittedly ugly formula guarantees that the return value will be >= 0 if
and only if the search key is found.*/
int pos=Collections.binarySearch(arrl, "a");
if(pos<0)
System.out.println("Not Found");
else
System.out.println("Found at "+pos);

//Replace: replaces all occurrences of one specified value with another.


Collections.replaceAll(arrl, "a", "A");
System.out.println(arrl);
}
}
Output:
First
Second

www.pes.edu
Third
Random
[Random, Third, Second, First]
[a, b, Second, First]
[a, b]
Found at 0
[A, b, Second, First]

//LinkedListOpns.java
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListDemo {


public static void main(String[] args) {
LinkedList<Integer> llist = new LinkedList<>();

//adding elements to the end


llist.add(1);
llist.add(2);
System.out.println(llist);

llist.addFirst(0); //addFirst
llist.addLast(3); //addLast
System.out.println(llist);

//remove first
int a=llist.removeFirst();
System.out.println(llist);

// Iterator to traverse the list


System.out.println("Using Iterator");
Iterator<Integer> itr=llist.iterator();
while(itr.hasNext())
System.out.print(itr.next()+" ");
System.out.println();

www.pes.edu
//reverse
Collections.reverse(llist);
System.out.println(llist);

//copy
LinkedList<Integer> llist1=new LinkedList<>();
llist1.add(10);llist1.add(20);
Collections.copy(llist, llist1);
System.out.println(llist);
System.out.println(llist1);

//search
int pos=Collections.binarySearch(llist, 20);
if(pos<0)
System.out.println("Not Found");
else
System.out.println("Found at "+pos);

//replace
Collections.replaceAll(llist, 2, 3);
System.out.println(llist);
}
}
Output:
[1, 2]
[0, 1, 2, 3]
[1, 2, 3]
Using Iterator
123
[3, 2, 1]
[10, 20, 1]
[10, 20]
Found at 1
[10, 20, 1]

www.pes.edu

You might also like