120 likes | 197 Views
Today’s Agenda. Sequential Access List Cursor Implementation. Random Access List Elements in a list are stored in contiguous memory locations. E.g. Array Sequential Access List Elements are not necessary in contiguous memory locations.
E N D
Today’s Agenda Sequential Access List Cursor Implementation
Random Access List • Elements in a list are stored in contiguous memory locations. • E.g. Array • Sequential Access List • Elements are not necessary in contiguous memory locations. • E.g. Arrays (using cursors) , linked list (using pointers)
cursorDef.h • typedef int Element; • typedef struct • { • Element ele; • int next; • }location; • typedef struct • { • int head; • int size; • }seqList; • typedef location store[max_len];
Declare global variables • store st; • int nextFreeLoc = 0;
seqList createEmptySeqList() { seqList sl; sl.head = -999; sl.size = 0; return sl; }
Insert at the end seqList insertElement (seqList sl,Element e) { int j; if(sl.head == -999) // insert in empty list { sl.head = nextFreeLoc++; st[sl.head].ele = e; st[sl.head].next = -1; } else { //insert in non-empty list j = sl.head; while(st[j].next != -1) j = st[j].next; st[j].next = nextFreeLoc; st[nextFreeLoc].ele = e; st[nextFreeLoc].next = -1; nextFreeLoc++; } sl.size++; return sl; }
Delete Element from a seq List seqList deleteElement (seqList sl,Element e) { int i,curr,prev; curr = sl.head; prev = sl.head; while(curr != -1) { if(st[curr].ele == e) { if(curr != sl.head) { st[prev].next = st[curr].next;//deletion break; } else { sl.head = st[sl.head].next; break; } } else { prev = curr; curr = st[curr].next; } } sl.size--; return sl; }
Common part in insertInOrder() int curr,prev; curr = sl.head; prev = sl.head; if(st[curr].ele <= e) { prev = curr; curr = st[curr].next; }
Insert in order (when Seq List is Empty) if(sl.head == -999)//seq list is empty { sl.head = nextFreeLoc++; st[sl.head].ele = e; st[sl.head].next = -1; sl.size++; }
Insert before head element if(curr == sl.head) { //insert before head temp = sl.head; sl.head = nextFreeLoc++; st[sl.head].ele = e; st[sl.head].next = temp; flag = 1; break; }
insert in between two nodes if(curr != -1)//insert in between two nodes { st[prev].next = nextFreeLoc; st[nextFreeLoc].next = curr; st[nextFreeLoc].ele = e; nextFreeLoc++; flag =1; break; }
Insert at the last if(st[prev].ele <= e) { st[prev].next = nextFreeLoc; st[nextFreeLoc].ele = e; st[nextFreeLoc].next = -1; nextFreeLoc++; }