Last active
November 12, 2018 00:15
-
-
Save cms-codes/b438f2bb88e24c9e4fef476c37913510 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* Lab6 ******* | |
* Course: PROG1955-1 | |
* Title: Lab6 | |
* Purpose: Store the last name and first name of students in a dynamic | |
structure using a linked list | |
* Date: 2018-11-10 | |
* Author: | |
* File: Lab6.c | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct studentRecord | |
{ | |
char *firstName; | |
char *lastName; | |
struct studentRecord *nextPtr; | |
}; | |
typedef struct studentRecord StudentRecord; | |
StudentRecord * createRecord(char *firstName, char *lastName); | |
void printRecord(StudentRecord *r); | |
int insertRecord(StudentRecord **headPtr, StudentRecord *r); | |
void listRecords(StudentRecord **headPtr); | |
StudentRecord * createRecord(char *firstName, char *lastName) | |
{ | |
StudentRecord *r = malloc(sizeof(StudentRecord)); | |
r->firstName = firstName; | |
r->lastName = lastName; | |
r->nextPtr = NULL; | |
return r; | |
} | |
void printRecord(StudentRecord *r) | |
{ | |
printf("First name: %s, Last name: %s\n", r->firstName, r->lastName); | |
} | |
int insertRecord(StudentRecord **headPtr, StudentRecord *r) | |
{ | |
StudentRecord *previousPtr = NULL; | |
StudentRecord *currentPtr = *headPtr; | |
// Find the last item in the list | |
while (currentPtr != NULL) | |
{ | |
previousPtr = currentPtr; | |
currentPtr = currentPtr->nextPtr; | |
} | |
if (previousPtr == NULL) | |
// This is the first record to be added to the list | |
{ | |
r->nextPtr = *headPtr; | |
*headPtr = r; | |
puts("first!"); | |
} | |
else | |
{ | |
// Append the new item to the end of the list, with the | |
// last item becoming the second-last item. | |
previousPtr->nextPtr = r; | |
r->nextPtr = currentPtr; | |
} | |
return 1; | |
} | |
void listRecords(StudentRecord **headPtr) | |
{ | |
StudentRecord *currentPtr = NULL; | |
currentPtr = *headPtr; | |
if (currentPtr == NULL) | |
// No records in this list | |
{ | |
puts("Empty."); | |
return; | |
} | |
do | |
{ | |
printRecord(currentPtr); | |
currentPtr = currentPtr->nextPtr; | |
} while (currentPtr != NULL); | |
puts("End of list."); | |
return; | |
} | |
int main() | |
{ | |
char first1[] = "Chris"; | |
char last1[] = "Niceguy"; | |
char first2[] = "Jason"; | |
char last2[] = "Otherguy"; | |
char *f1 = first1; | |
char *l1 = last1; | |
char *f2 = first2; | |
char *l2 = last2; | |
// This is the pointer to the first record in our list | |
StudentRecord *headPtr = NULL; | |
// This are record pointers for testing, all records will be created in the loop | |
StudentRecord *newRecord1 = NULL; | |
StudentRecord *newRecord2 = NULL; | |
newRecord1 = createRecord(f1, l1); | |
insertRecord(&headPtr, newRecord1); | |
newRecord2 = createRecord(f2, l2); | |
insertRecord(&headPtr, newRecord2); | |
listRecords(&headPtr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment