Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define STR_MAX_LEN 20
- typedef struct personNode {
- char name[STR_MAX_LEN];
- int age;
- struct personNode* next;
- } personNode;
- void myFgets(char str[], int n);
- void printList(personNode* head);
- void freeList(personNode* head);
- void insertAtEnd(personNode** head, personNode* newNode);
- int listLength(personNode* head);
- personNode* createPersonNode();
- personNode* getList(personNode* head, int size);
- personNode* copyList(personNode* head);
- int main()
- {
- int peoples_number = 0;
- personNode* head = NULL;
- personNode* duplicate = NULL;
- printf("How many people in the list? ");
- scanf("%d", &peoples_number);
- getchar();
- head = getList(head, peoples_number);
- printList(head);
- printf("List length: %d\n", listLength(head));
- duplicate = copyList(head);
- printf("New list:\n");
- printList(duplicate);
- freeList(head);
- freeList(duplicate);
- getchar();
- return 0;
- }
- /* Inserts the given node into the end of the head (main) node
- * input: head of the main node, new node
- * output: -
- */
- void insertAtEnd(personNode** head, personNode* newNode)
- {
- if (!*head)
- {
- *head = newNode;
- }
- else
- {
- personNode* p = *head;
- while (p->next)
- {
- p = p->next;
- }
- p->next = newNode;
- }
- }
- /* Creates new node of person
- * input: -
- * output: new node of person struct type
- */
- personNode* createPersonNode()
- {
- personNode* newPersonNode = (personNode*)malloc(sizeof(personNode));
- strcpy(newPersonNode->name, "No name");
- newPersonNode->age = 0;
- newPersonNode->next = NULL;
- return newPersonNode;
- }
- /* Custom fgets with built-in '\n' remove
- * input: string to in-put, string size
- * output: -
- */
- void myFgets(char str[], int n)
- {
- fgets(str, n, stdin);
- str[strcspn(str, "\n")] = 0;
- }
- /* Prints the given list
- * input: head of the main node (list)
- * output: -
- */
- void printList(personNode* head)
- {
- personNode* current_node = head;
- while (current_node != NULL)
- {
- printf("Name: %s, Age: %d\n", current_node->name, current_node->age);
- current_node = current_node->next;
- }
- }
- /* Counts the length of the given list
- * input: head of the main node (list)
- * output: length of the given list
- */
- int listLength(personNode* head)
- {
- if (!head) // if list empty
- {
- return 0;
- }
- else
- {
- return 1 + listLength(head->next);
- }
- }
- /* Return duplicate of the given list
- * input: head of the main node (list)
- * output: NEW; ALLOCATED in memory list duplicate of the given 'head' node (list)
- */
- personNode* copyList(personNode* head)
- {
- if (!head) // if list empty
- {
- return NULL;
- }
- else
- {
- // Allocate the memory for new Node
- // in the heap and set its data
- personNode* newNode = (personNode*)malloc(sizeof(personNode));
- strcpy(newNode->name, head->name);
- newNode->age = head->age;
- // Recursively set the next pointer of
- // the new Node by recurring for the
- // remaining nodes
- newNode->next = copyList(head->next);
- return newNode;
- }
- }
- /* Asks the user to input the name and age according the given number of peoples
- * input: head of the main node, number of peoples
- * output: new node of personNode struct type
- */
- personNode* getList(personNode* head, int size)
- {
- int i = 0;
- personNode* newNode = NULL;
- for (i = 0; i < size; i++)
- {
- newNode = createPersonNode();
- printf("Enter name: ");
- myFgets(newNode->name, STR_MAX_LEN);
- printf("Enter age: ");
- scanf("%d", &(newNode->age));
- getchar();
- insertAtEnd(&head, newNode);
- }
- return head;
- }
- /* Frees the memory that the node used
- * input: the head of the main node (list)
- * output: -
- */
- void freeList(personNode* head)
- {
- personNode* tmp;
- while (head)
- {
- tmp = head;
- head = head->next;
- free(tmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement