2.introduction toDS - Algorithms - Day2 PDF
2.introduction toDS - Algorithms - Day2 PDF
• Lists:
– Very useful data structure.
– are a way to store many different values under a single variable.
– Every item Node in this list is numbered with an index.
– Unlike counting things that exist in the real world, index variables
always begin with the number 0.
– List Operations:
• Add : add a new node
• Set : update the contents of a node
• Remove: remove a node
• ISEmpty : reports whether the list is empty
• ISFull : reports whether the list is full
• Initialize : create/initialize the list
• Destroy : delete the content of the list.
• Lists Types:
– Array List:
• allocate the memory for all its elements in one block of memory.
• The size of the array is fixed.
Person p;
Person P[10];
Person *p;
// also here you need to know the length early
P= new Person[10];
• Lists Types:
– Array List:
• Given any index , the node with that index can be accessed in constant
time, does not depend on the size of the array.
• Add from the end or insert with shift up process for all other nodes.
• When a node is removed , subsequent nodes must be shifted down, so
removals near the start of the list take longer than removals near the middle
or end.
– Each node does not necessarily follow the previous one physically in
the memory.
– Each node contains both its data and pointer to next node in the list.
– Has pointer to the first node and another for the last node.
– The list can be read in either direction that simplifies sorting the list.
– Because either a forward link or a backward link can read the entire
list, if one link becomes invalid, the list can be reconstructed using the
other link. This is meaningful only in the case of equipment failure.
– Deleting Nodes
• Case 1: delete a node.
• Case 2: deleting all nodes (removing the list).
– Display All
class Employee
{
char name[20];
float salary;
float oTime;
int code;
public:
Employee *pNext;
Employee *pPervious;
Employee() { code = o; name=”no name” ; salary = oTime = 0; pNext=NULL; pPrevious =NULL;}
Employee(int c, char *n, float s, float o)
{ code = c; name= n ; salary =s ; oTime =o; pNext=NULL; pPrevious =NULL;}
// setters and getters.
void printEmployee();
};
– List Definition:
class LinkedList
{
protected:
Employee *pStart;
Employee *pEnd;
public:
LinkedList() {pStart=pEnd=NULL;}
~LinkedList() { freeList();}
// Setters and getters for pStart and pEnd;
void addList(Employee *pItem);
void InsertList(Employee *pItem);
Employee* searchList(int Code)
int DeleteList(int Code);
void freeList();
void displayAll();
~LinkedList() { freeList();}
};
– Display All
Employee * pItem;
pItem = pStart;
while(pItem)
{
pltem->printEmployee();
pltem = pltem ->pNext;
}
}
• 1st Assignment :
1. Implement Doubly Linked list with all operations.
2. Bonus: Sort Doubly Linked List.
3. Search: Implement Circular Doubly Linked Lists operations.