Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PROGRAM: Modify contents of phonebook
- AUTHOR: Tinashe Chapfunga and Brandon Le
- DATE: November 10, 2018
- PURPOSE: Modify contents of phonebook
- LEVEL OF DIFFICULTY: 10
- CHALLENGES: Nodes and array calling
- HOURS SPENT: 6
- */
- #include <stdio.h>
- #include "header.h"
- int ExistingAreaCodeNums[5] = {613, 416, 647, 519, 905};
- char *ExistingAreaCodeDesc[5] = {"Ottawa", "Toronto", "Toronto", "Windsor", "Niagra"};
- /* return number of nodes in the list. */
- int Length (struct node* head) {
- int count = 0;
- struct node* current = head;
- while (current != NULL) {
- count++;
- current = current->next;
- }
- return count;
- }
- /* Prints all the nodes on a list */
- void PrintList(struct node* head) {
- struct node* current = head;
- if (current == NULL) {
- printf("Linked list is empty!\n");
- return;
- }
- printf("The list is: ");
- while (current != NULL) {
- printf("%d -> ", current->data);
- current = current->next;
- }
- printf("\n");
- return;
- }
- /*Given an int and a reference to the head pointer, add a new node at the head of the list. */
- void Add(struct node** headRef, int new) {
- struct node* head;
- head = (struct node*) malloc(sizeof(struct node));
- head->data = new;
- head->next = *headRef;
- *headRef = head;
- printf("New node added to the list.\n");
- return;
- }
- /*Takes a non-empty list, deletes the head node, and returns the head node's data.*/
- int Delete(struct node** headRef) {
- struct node current;
- int val = 0;
- if (*headRef == NULL) {
- printf("List is empty. Nothing to delete.\n");
- return val;
- }
- current = *headRef;
- val = current->data;
- headRef = current->next;
- free(current);
- printf("The head is deleted.\n");
- return val;
- }
- /* Deallocates all of its memory and sets its head pointer to NULL*/
- void ZeroList(struct node** headRef) {
- struct node p;
- if (headRef == NULL) {
- printf("List is empty, nothing to free\n");
- return;
- }
- while (headRef != NULL) {
- p = *headRef;
- *headRef = p->next;
- free(p);
- }
- printf("All nodes have been deleted.\n");
- return;
- }
- /* PROGRAM: Modify contents of phonebook
- AUTHOR: Tinashe Chapfunga and Brandon Le
- DATE: November 10, 2018
- PURPOSE: Modify contents of phonebook
- LEVEL OF DIFFICULTY: 10
- CHALLENGES: Nodes and array calling
- HOURS SPENT: 6
- */
- #include <stdlib.h>
- typedef struct Area{
- int areaCode;
- char areaName[20];
- struct Area *nextArea;
- } Area;
- typedef struct PhEntry{
- int areaCode;
- int phoneNumber;
- char firstName[20];
- char lastName[20];
- struct PhEntry *nextPhEntry;
- } PhEntry;
- int ExistingAreaCodeNums[5];
- char *ExistingAreaCodeDesc[5];
- int Length(struct node* head);
- void PrintList(struct node* head);
- void Add(struct node** headRef, int new);
- int Delete(struct node** headRef);
- void ZeroList(struct node** headRef);
- /* PROGRAM: Modify contents of phonebook
- AUTHOR: Tinashe Chapfunga and Brandon Le
- DATE: November 10, 2018
- PURPOSE: Modify contents of phonebook
- LEVEL OF DIFFICULTY: 10
- CHALLENGES: Nodes and array calling
- HOURS SPENT: 6
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include "header.h"
- int main(){
- char userInput = '0';
- while(userInput != 'q'){
- printf("Choose one of the following options:");
- printf("Press [1] to Enter Area information:");
- printf("Press [2] to Enter PhoneBook Entry:");
- printf("Press [3] to Modify an existing PhoneBook Entry:");
- printf("Press [4] to Delete an existing PhoneBook Entry:");
- printf("Press [5] to Delete an unused Area using doubly link list:");
- printf("Press [q] to quit:");
- scanf("%s", &userInput);
- switch(userInput){
- case 1 :
- int areaCode;
- int areaCodeCheck;
- char *areaDesc;
- printf("You pressed: 1");
- printf("Enter Area Code: ");
- scanf("%d", &areaCode);
- printf("\n");
- areaCodeCheck = areaCode/100;
- if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
- printf("Enter Area Description: ");
- scanf("%s", &areaDesc);
- }
- else{
- printf("Invalid Area Code.");
- }
- break;
- case 2 :
- int areaCode;
- int areaCodeCheck;
- int phoneNum;
- printf("You pressed: 2");
- printf("Enter Area Code: ");
- scanf("%d", &areaCode);
- printf("\n");
- areaCodeCheck = areaCode/100;
- if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
- printf("Enter 7-digit phone number: ");
- scanf("%d", &phoneNum);
- }
- else{
- printf("Invalid Area Code.");
- }
- break;
- case 3 :
- int areaCode;
- int areaCodeCheck;
- int phoneNum;
- char *lastName;
- printf("You pressed: 3");
- printf("Enter Area Code: ");
- scanf("%d", &areaCode);
- printf("\n");
- areaCodeCheck = areaCode/100;
- if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
- printf("Enter 7-digit phone number: ");
- scanf("%d", &phoneNum);
- printf("Enter last name: ");
- scanf("%s", &lastName);
- }
- else{
- printf("Invalid Area Code.");
- }
- break;
- case 4 :
- int areaCode;
- int areaCodeCheck;
- int phoneNum;
- char *lastName;
- printf("You pressed: 4");
- printf("Enter Area Code: ");
- scanf("%d", &areaCode);
- printf("\n");
- areaCodeCheck = areaCode/100;
- if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
- printf("Enter 7-digit phone number: ");
- scanf("%d", &phoneNum);
- printf("Enter last name: ");
- scanf("%s", &lastName);
- }
- else{
- printf("Invalid Area Code.");
- }
- break;
- case 5 :
- int areaCode;
- int areaCodeCheck;
- char *areaDesc;
- int phoneNum;
- char *lastName;
- printf("You pressed: 3");
- printf("Enter Area Code: ");
- scanf("%d", &areaCode);
- printf("\n");
- areaCodeCheck = areaCode/100;
- if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
- printf("Enter Area Description: ");
- scanf("%s", &areaDesc);
- }
- else{
- printf("Invalid Area Code.");
- }
- break;
- default:
- printf("Invalid Input");
- break;
- }
- }
- }
- CC = gcc
- CC_FLAGS = -g -ansi -pedantic -Wall
- FILES = main.c2, header.c, header.h
- OUT_EXE = Assignment 2
- build: $(FILES)
- $(CC) $(CC_FLAGS) -o $(OUT_EXE) $(FILES)
- clean:
- rm -f *.o core *.exe *~
- rebuild: clean build
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement