1 / 12

Today’s Agenda

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.

Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda Sequential Access List Cursor Implementation

  2. 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)

  3. cursorDef.h • typedef int Element; • typedef struct • { • Element ele; • int next; • }location; • typedef struct • { • int head; • int size; • }seqList; • typedef location store[max_len];

  4. Declare global variables • store st; • int nextFreeLoc = 0;

  5. seqList createEmptySeqList() { seqList sl; sl.head = -999; sl.size = 0; return sl; }

  6. 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; }

  7. 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; }

  8. Common part in insertInOrder() int curr,prev; curr = sl.head; prev = sl.head; if(st[curr].ele <= e) { prev = curr; curr = st[curr].next; }

  9. 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++; }

  10. 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; }

  11. 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; }

  12. Insert at the last if(st[prev].ele <= e) { st[prev].next = nextFreeLoc; st[nextFreeLoc].ele = e; st[nextFreeLoc].next = -1; nextFreeLoc++; }

More Related