1 / 9

Today’s Agenda

Today’s Agenda. Sequential List Linked List Implementation. dataDef.h. typedef struct { int idno ; char name[max]; float cgpa ; } student; typedef struct node Node ; typedef Node *Link; // or typedef struct node *Link; struct node { student s; Link next ; };

leroy
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 List Linked List Implementation

  2. dataDef.h typedefstruct { intidno; char name[max]; float cgpa; }student; typedefstruct node Node; typedef Node *Link; // or typedefstruct node *Link; struct node { student s; Link next; }; typedefstruct { Link head; int size; }seqList;

  3. seqListOps.h #include“dataDef.h" extern seqList createSeqList(); extern Link createNode(student s); extern seqList insertElement(seqList sl,student s); extern void printList(seqList sl); seqList removeElement(seqList sl,float cg);

  4. seqList createSeqList() { seqList sl; sl.head = NULL; sl.size = 0; return sl; }

  5. Link createNode(student s) { Link newnode; newnode = (Link)malloc(sizeof(Node)); newnode->s.idno = s.idno; strcpy(newnode->s.name,s.name); newnode->s.cgpa = s.cgpa; newnode->next = NULL; return newnode; }

  6. seqList insertElement(seqList sl,student s) { Link newnode, curr; newnode = createNode(s); if(sl.head == NULL) //check for empty list { sl.head = newnode; sl.size++; } else { curr = sl.head; while(curr->next != NULL) // insert at the end of the list curr = curr->next; curr->next = newnode; sl.size++; } return sl; }

  7. void printList(seqList sl) { Link temp; temp = sl.head; while(temp != NULL) { printf("%d\t%s\t%f\n",temp->s.idno,temp->s.name,temp->s.cgpa); temp = temp->next; } }

  8. seqListremoveElement(seqListsl,float cg) { Link curr,prev; curr = sl.head; prev = sl.head; if(curr == NULL) { printf("Error Empty List!....\n"); return sl; } while(curr != NULL) { if(curr->s.cgpa >= cg) { prev = curr; curr = curr->next; } else { prev->next = curr->next; printf("student %s is having less than 7.5 cgpa = %f and so deleting \n",curr->s.name,curr->s.cgpa); return sl; } } printf("No student with cgps less than 7.5 is present in the list\n"); return sl; }

  9. int main() { seqListsl; inti; float cg = 7.5; student s[5] ={{123,"xyz",7.5}, {456,"abc",8.9}, {789,"mnr",9.5}, {654,"akd",8.8}, {543,"pqr",7.7}}; sl = createSeqList(); // sl = removeElement(sl,cg); for(i=0;i<5;i++) sl = insertElement(sl,s[i]); printf("printing after inserting\n"); printList(sl); sl = removeElement(sl,cg); printf("printing after deleting\n"); printList(sl); return 0; }

More Related