BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI SECOND SEMESTER

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI SECOND SEMESTER 2015 - 2016 COURSE : COMPUTER PROGRAMMING (CS F111) COMPONENT : ...
Author: Shavonne Fisher
1 downloads 0 Views 350KB Size
BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI SECOND SEMESTER 2015 - 2016 COURSE

: COMPUTER PROGRAMMING (CS F111)

COMPONENT

: Tutorial# 18 (SOLUTIONS)

DATE

: 19-MAY-2016

Answer 1a (search) & 1d (delete) – Singly Linked List. #include #include #include struct node; typedef struct node NODE; typedef NODE *LINK; typedef NODE *NODEPTR; struct node { int data; LINK next; }; typedef struct { LINK head; int size; } LIST; NODEPTR createNode(int item) { NODEPTR node = (NODEPTR) malloc(sizeof(NODE)); assert(node); node->data = item; node->next = NULL; } void initList(LIST *list) { NODEPTR node = createNode(0); list->head = node; list->size = 0; } void insert (LIST *list, int item) { NODEPTR newNode = createNode(item); NODEPTR temp = list->head; while (temp->next ) temp = temp->next; temp->next = newNode; list->size++; } void display(LIST *list) { NODEPTR temp = list->head->next; printf ("List has %d element(s) --: ", list->size ); while (temp ) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } CS F111 COMPUTER PROGRAMMING

Page 1 of 8

NODEPTR search(LIST *list, int item) { NODEPTR temp = list->head->next; printf ("List has %d element(s)\n", list->size ); if (!list->size) return NULL; /* nothing to search */ while (temp ) { if (item == temp->data) return temp; temp = temp->next; } return NULL; /* element not found */ } int delete(LIST *list, int item) { NODEPTR prev = list->head, temp = prev->next; printf ("List has %d element(s)\n", list->size ); if (!list->size) return 0; /* nothing to delete */ while (temp ) { if (item == temp->data) { prev->next = temp->next; free(temp); list->size--; return 1; } prev = temp; temp = temp->next; } return 0; /* element not found */ } main() { int op, item, quit = 0; LIST list; initList(&list); do { printf("\n1.Insert\n2.Search\n3.Delete\n4.Display\n5.Quit\n"); printf("Enter option : "); scanf("%d", &op); switch(op) { case 1: printf("Enter item to inserted: "); scanf("%d", &item); insert(&list, item); break; case 2: printf("Enter item to be searched: "); scanf("%d", &item); printf ("%d %s\n", item, search(&list, item) ? "Found" : "Not Found"); break; case 3: printf("Enter item to be deleted: "); scanf("%d", &item); printf ("%d %s\n", item, delete(&list, item) ? "Deleted" : "Not Found"); break; case 4: display(&list); break; case 5: quit = 1; break; default: printf("Illegal Option\n"); break; } /* of switch */ getchar(); CS F111

COMPUTER PROGRAMMING

Page 2 of 8

} while ( !quit); }

Answer 1b (Search) & 1e (Delete) – Circular Linked Lists #include #include #include struct node; typedef struct node NODE; typedef NODE *LINK; typedef NODE *NODEPTR; struct node { int data; LINK next; }; typedef struct { LINK head; int size; } LIST; NODEPTR createNode(int item) { NODEPTR node = (NODEPTR) malloc(sizeof(NODE)); assert(node); node->data = item; node->next = NULL; } void initList(LIST *list) { NODEPTR node = createNode(0); list->head = node; node->next = list->head; list->size = 0; } void insert (LIST *list, int item) { NODEPTR newNode = createNode(item); NODEPTR head = list->head, temp = head; while (temp->next != head) temp = temp->next; temp->next = newNode; newNode->next = head; list->size++; } void display(LIST *list) { NODEPTR head = list->head, temp = head->next; printf ("List has %d element(s) --: ", list->size ); while (temp != head) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } NODEPTR search(LIST *list, int item) { CS F111

COMPUTER PROGRAMMING

Page 3 of 8

NODEPTR head = list->head, temp = head->next; printf ("List has %d element(s)\n", list->size ); if (!list->size) return NULL; /* nothing to search */ while (temp != head ) { if (item == temp->data) return temp; temp = temp->next; } return NULL; /* element not found */ } int delete(LIST *list, int item) { NODEPTR head = list->head, prev = head, temp = head->next; printf ("List has %d element(s)\n", list->size ); if (!list->size) return 0; /* nothing to delete */ while (temp != head ) { if (item == temp->data) { prev->next = temp->next; free(temp); list->size--; return 1; } prev = temp; temp = temp->next; } return 0; /* element not found */ } main() { int op, item, quit = 0; LIST list; initList(&list); do { printf("\n1.Insert\n2.Search\n3.Delete\n4.Display\n5.Quit\n"); printf("Enter option : "); scanf("%d", &op); switch(op) { case 1: printf("Enter item to inserted: "); scanf("%d", &item); insert(&list, item); break; case 2: printf("Enter item to be searched: "); scanf("%d", &item); printf ("%d %s\n", item, search(&list, item) ? "Found" : "Not Found"); break; case 3: printf("Enter item to be deleted: "); scanf("%d", &item); printf ("%d %s\n", item, delete(&list, item) ? "Deleted" : "Not Found"); break; case 4: display(&list); break; case 5: quit = 1; break; default: printf("Illegal Option\n"); break; } /* of switch */ CS F111

COMPUTER PROGRAMMING

Page 4 of 8

getchar(); } while ( !quit); }

Answer 1c (Search) and 1f (Delete) – Doubly Linked List #include #include #include struct node; typedef struct node NODE; typedef NODE *LINK; typedef NODE *NODEPTR; struct node { LINK prev; int data; LINK next; }; typedef struct { LINK head; int size; } LIST; NODEPTR createNode(int item) { NODEPTR node = (NODEPTR) malloc(sizeof(NODE)); assert(node); node->data = item; node->next = node->next = NULL; } void initList(LIST *list) { NODEPTR node1 = createNode(0), node2 = createNode(0); list->head = node1; node1->next = node2; node2->prev = node1; list->size = 0; } void insert (LIST *list, int item) { NODEPTR newNode = createNode(item); NODEPTR prev, temp = list->head; while (temp->next ) temp = temp->next; prev = temp->prev; prev->next = newNode; newNode->prev = prev; newNode->next = temp; temp->prev = newNode; list->size++; } void display(LIST *list) { NODEPTR temp = list->head->next; printf ("List has %d element(s)\n", list->size ); printf ("Display fwd --: "); while (temp->next ) { printf("%d ", temp->data); temp = temp->next; CS F111

COMPUTER PROGRAMMING

Page 5 of 8

} printf("\n"); printf ("Display rev --: "); temp = temp->prev; while (temp->prev) { printf("%d ", temp->data); temp = temp->prev; } printf("\n"); } NODEPTR search(LIST *list, int item) { NODEPTR temp = list->head->next; printf ("List has %d element(s)\n", list->size ); if (!list->size) return NULL; /* nothing to search */ while (temp->next) { if (item == temp->data) return temp; temp = temp->next; } return NULL; /* element not found */ } int delete(LIST *list, int item) { NODEPTR prev = list->head, temp = prev->next, next; printf ("List has %d element(s)\n", list->size ); if (!list->size) return 0; /* nothing to delete */ while (temp->next ) { if (item == temp->data) { prev = temp->prev; next = temp->next; prev->next = temp->next; next->prev = temp->prev; free(temp); list->size--; return 1; } temp = temp->next; } return 0; /* element not found */ } main() { int op, item, quit = 0; LIST list; initList(&list); do { printf("\n1.Insert\n2.Search\n3.Delete\n4.Display\n5.Quit\n"); printf("Enter option : "); scanf("%d", &op); switch(op) { case 1: printf("Enter item to inserted: "); scanf("%d", &item); insert(&list, item); break; case 2: printf("Enter item to be searched: "); scanf("%d", &item); printf ("%d %s\n", item, search(&list, item) ? "Found" : "Not Found"); break; CS F111

COMPUTER PROGRAMMING

Page 6 of 8

case 3: printf("Enter item to be deleted: "); scanf("%d", &item); printf ("%d %s\n", item, delete(&list, item) ? "Deleted" : "Not Found"); break; case 4: display(&list); break; case 5: quit = 1; break; default: printf("Illegal Option\n"); break; } /* of switch */ getchar(); } while ( !quit); }

Answer 2 #include main(int argc, char *argv[]) { FILE *f; char c; int space=0,newline=0,semi=0; f=fopen(argv[1],"r"); if(f==NULL) { printf("File opening error\n"); exit(0); } c=fgetc(f); while(c!=EOF) { if(c==' ') space++; else if(c==';') semi++; else if(c=='\n') newline++; c=fgetc(f); } printf("\n\nSpaces=%d\nSemi Colon=%d\nNewline=%d\n\n",space,semi,newline); fclose(f); }

Answer 3 #include typedef struct { char stu_id[13], stu_name[50]; int stu_phone, stu_age; float stu_cgpa; }student; main() { student s[100]; int i; FILE *f; f=fopen("stu.txt","w"); if(f==NULL) CS F111

COMPUTER PROGRAMMING

Page 7 of 8

{ printf("File opening error\n"); exit(0); } for(i=0;i