Last active
July 9, 2019 20:56
-
-
Save chrisdel101/24685c2781a2c31fd44918364f166b1f to your computer and use it in GitHub Desktop.
c list excercise
This file contains hidden or 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
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#define NAME_LENGTH 45 | |
#define HASHTABLE_SIZE 65536 | |
typedef struct Node | |
{ | |
char name[NAME_LENGTH]; | |
struct Node *next; | |
} Node; | |
Node *transverseAdd(Node *head, char *name) | |
{ | |
Node *tempList = head; | |
Node *newNode = malloc(sizeof(Node)); | |
int length = 0; | |
if (newNode == NULL) | |
{ | |
exit(0); | |
} | |
strcpy(newNode->name, name); | |
newNode->next = NULL; | |
// empty list | |
if (tempList == NULL) | |
{ | |
tempList = newNode; | |
return tempList; | |
} | |
else | |
{ | |
while (tempList->next != NULL) | |
{ | |
// // check if matches current | |
if (strcmp(tempList->name, name) > 0 || strcmp(tempList->name, name) == 0) | |
{ | |
Node *tempNode = tempList; | |
tempList = newNode; | |
newNode->next = tempNode; | |
return tempList; | |
} | |
tempList = tempList->next; | |
} | |
if (strcmp(tempList->name, name) > 0) | |
{ | |
Node *tempNode = tempList; | |
tempList = newNode; | |
newNode->next = tempNode; | |
return tempList; | |
} | |
} | |
while (tempList->next != NULL) | |
{ | |
tempList = tempList->next; | |
} | |
tempList->next = newNode; | |
return head; | |
} | |
bool emailChecker(char *name, char *email) | |
{ | |
char gmail[] = "gmail"; | |
if (strstr(email, gmail)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
void display(Node *head) | |
{ | |
Node *start = head; | |
while (start) | |
{ | |
printf("name %s\n", start->name); | |
start = start->next; | |
} | |
printf("\n"); | |
} | |
int main(void) | |
{ | |
int T, data; | |
scanf("%d", &T); | |
Node *list = NULL; | |
char name[20]; | |
char email[50]; | |
while (T-- > 0) | |
{ | |
memset(name, 0, sizeof(name)); | |
memset(email, 0, sizeof(email)); | |
scanf("%s %s", name, email); | |
list = transverseAdd(list, name); | |
} | |
display(list); | |
} |
working input
6 riya [email protected] julia [email protected] julia [email protected] julia [email protected] samantha [email protected] tanya [email protected]
correct output
julia julia riya samantha tanya
Should this have 3 julia
s in the output?
input
12 riya [email protected] julia [email protected] julia [email protected] julia [email protected] samantha [email protected] tanya [email protected] riya [email protected] julia [email protected] julia [email protected] julia [email protected] samantha [email protected] tanya [email protected]
expected output
julia julia julia julia riya riya samantha samantha tanya tanya
Actual
output samantha output samantha output tanya output tanya
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
working input
correct output
input
expected output
Actual