Unit1 Notes 20210129105526
Unit1 Notes 20210129105526
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.
www.pes.edu
public void method2()
{
// Your implementation
}
};
Example:
//Displayable.java
package interfacedemoads;
//Rectangle.java
package interfacedemoads;
www.pes.edu
//Circle.java
package interfacedemoads;
// InterfaceDemoADS.java
package interfacedemoads;
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;
};
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;
www.pes.edu
//arListimpl.c
#include"arList.h"
#include<stdio.h>
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;
//llistImpl.c
#include"llist.h"
#include<stdlib.h>
#include<stdio.h>
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;
}
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;
}
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;
}
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 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;
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
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;
//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);
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;
llist.addFirst(0); //addFirst
llist.addLast(3); //addLast
System.out.println(llist);
//remove first
int a=llist.removeFirst();
System.out.println(llist);
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