Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Last active July 9, 2019 20:56
Show Gist options
  • Save chrisdel101/24685c2781a2c31fd44918364f166b1f to your computer and use it in GitHub Desktop.
Save chrisdel101/24685c2781a2c31fd44918364f166b1f to your computer and use it in GitHub Desktop.
c list excercise
#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);
}
@hawkinsw
Copy link

hawkinsw commented Jul 9, 2019

Can you send me sample input/output for failure?

@chrisdel101
Copy link
Author

chrisdel101 commented Jul 9, 2019

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

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
julia
julia
riya
riya
samantha
samantha
tanya
tanya

Actual

output samantha
output samantha
output tanya
output tanya

@hawkinsw
Copy link

hawkinsw commented Jul 9, 2019

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 julias 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