Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Jagroop
- Search Drive
- Drive
- .
- Folder Path
- My Drive
- eece 2160
- lab 3
- NEW
- Folders and views
- My Drive
- Shared with me
- Google Photos
- Recent
- Starred
- Trash
- 494 MB used
- .
- Image
- addPerson.PNG
- Image
- assgn5.PNG
- Image
- breakpoint.PNG
- Image
- findPerson.PNG
- Image
- gdb list.PNG
- Google Docs
- Lab Number: 3
- Unknown File
- person
- C
- person.c
- C
- person.c
- Unknown File
- personDebug
- C
- personDebug.c
- Image
- pre lab.png
- Google Docs
- Pre-Lab
- Image
- printList.PNG
- Image
- removePerson.PNG
- All selections cleared
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int ID = 1;
- struct List list;
- struct Person
- {
- // Unique identifier for the person
- int id;
- // Information about person
- char name[20];
- int age;
- // Pointer to next person in list
- struct Person *next;
- };
- struct List
- {
- // First person in the list. A value equal to NULL indicates that the
- // list is empty.
- struct Person *head;
- // Current person in the list. A value equal to NULL indicates a
- // past-the-end position.
- struct Person *current;
- // Pointer to the element appearing before 'current'. It can be NULL if
- // 'current' is NULL, or if 'current' is the first element in the list.
- struct Person *previous;
- // Number of persons in the list
- int count;
- };
- // Give an initial value to all the fields in the list.
- void ListInitialize(struct List *list)
- {
- list->head = NULL;
- list->current = NULL;
- list->previous = NULL;
- list->count = 0;
- }
- // Move the current position in the list one element forward. If last element
- // is exceeded, the current position is set to a special past-the-end value.
- void ListNext(struct List *list)
- {
- if (list->current)
- {
- list->previous = list->current;
- list->current = list->current->next;
- }
- }
- // Move the current position to the first element in the list.
- void ListHead(struct List *list)
- {
- list->previous = NULL;
- list->current = list->head;
- }
- // Get the element at the current position, or NULL if the current position is
- // past-the-end.
- struct Person *ListGet(struct List *list)
- {
- return list->current;
- }
- // Set the current position to the person with the given id. If no person
- // exists with that id, the current position is set to past-the-end.
- void ListFind(struct List *list, int id)
- {
- ListHead(list);
- while (list->current && list->current->id != id)
- ListNext(list);
- }
- // Insert a person before the element at the current position in the list. If
- // the current position is past-the-end, the person is inserted at the end of
- // the list. The new person is made the new current element in the list.
- void ListInsert(struct List *list, struct Person *person)
- {
- // Set 'next' pointer of current element
- person->next = list->current;
- // Set 'next' pointer of previous element. Treat the special case where
- // the current element was the head of the list.
- if (list->current == list->head)
- list->head = person;
- else
- list->previous->next = person;
- // Set the current element to the new person
- list->current = person;
- }
- // Remove the current element in the list. The new current element will be the
- // element that appeared right after the removed element.
- void ListRemove(struct List *list)
- {
- // Ignore if current element is past-the-end
- if (!list->current)
- return;
- // Remove element. Consider special case where the current element is
- // in the head of the list.
- if (list->current == list->head)
- list->head = list->current->next;
- else
- list->previous->next = list->current->next;
- // Free element, but save pointer to next element first.
- struct Person *next = list->current->next;
- free(list->current);
- // Set new current element
- list->current = next;
- }
- void PrintPerson(struct Person *person)
- {
- printf("Person with ID %d:\n", person->id);
- printf("\tName: %s\n", person->name);
- printf("\tAge: %d\n\n", person->age);
- }
- struct Person * makePerson()
- {
- struct Person *p = malloc(sizeof(struct Person));
- printf("\nYou selected \"Add a Person\".");
- printf("\nEnter name: ");
- char name[20];
- scanf("%s", name);
- printf("\nEnter an age: ");
- int age;
- scanf("%d", &age);
- p->age = age;
- strcpy(p->name, name);
- p->id = ID;
- ID++;
- return p;
- }
- void findPerson()
- {
- printf("\nYou selected \"Find a person\".");
- printf("\nEnter an Id: ");
- int userId;
- scanf("%d", &userId);
- printf("\n");
- ListFind(&list, userId);
- struct Person *p = ListGet(&list);
- if(p)
- {
- PrintPerson(p);
- }
- else
- {
- printf("\nCannot find specified user");
- }
- }
- void removePerson()
- {
- printf("\nYou selected \"Remove a person\".");
- printf("\nEnter an Id: ");
- int userId;
- scanf("%d", &userId);
- printf("\n");
- ListFind(&list, userId);
- struct Person *p = ListGet(&list);
- if(p)
- {
- ListRemove(&list);
- printf("Removed a person with Id: %d\n", userId);
- }
- else
- {
- printf("\nCannot find specified user");
- }
- }
- void printList()
- {
- ListHead(&list);
- while (list.current)
- {
- struct Person *p = ListGet(&list);
- PrintPerson(p);
- ListNext(&list);
- }
- }
- int main()
- {
- int cond = 1;
- ListInitialize(&list);
- while(cond) { // Loop the menu
- int choice;
- printf("\n------------------------------------\nMain menu:\n\n1. Add a Person\n2. Find a person\n3. Remove a person\n4. Print the List\n5. Exit\n\nSelect an option:");
- scanf("%d", &choice); // Read in user choice
- switch(choice){
- case 1:
- {
- struct Person *p = makePerson();
- ListInsert(&list, p);
- break;
- }
- case 2:
- {
- findPerson();
- break;
- }
- case 3:
- removePerson();
- break;
- case 4:
- printList();
- break;
- case 5:
- printf("\nYou selected \"Exit\".\n");
- cond = 0; // Break out of while loop
- break;
- default:
- break;
- }
- printf("\n\n\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement