90 likes | 175 Views
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 ; };
E N D
Today’s Agenda Sequential List Linked List Implementation
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;
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);
seqList createSeqList() { seqList sl; sl.head = NULL; sl.size = 0; return sl; }
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; }
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; }
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; } }
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; }
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; }