One of my functions is accidentally deleting elements from list - c

I am trying to make a "library" where there are alphabetically-sorted catalogs and each of them has books also in alphabetical order. As part of deletebook function(not included because it's not there yet), I wrote function that finds catalog of the book. The function works, but it deletes books in the process( I think) which is not good. How to change it so it doesn't delete them?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
char *title;
int number;
char *country;
struct book* new;
};
struct catalog
{
char *name;
struct catalog* next;
struct book* firstbook;
};
void printList(struct catalog *head)
{
struct catalog *temp = head;
while(temp != NULL)
{
struct book* book = temp->firstbook;
if(book == NULL)
{
printf("%s\n", temp->name);
}
while(book != NULL)
{
printf("%s ", temp->name);
printf("%s ", book->title);
printf("%d ", book->number);
printf("%s\n", book->country);
book = book->new;
}
temp = temp->next;
}
}
struct catalog *newcatalog(char *new_name)
{
struct catalog* new_node = (struct catalog*) malloc(sizeof(struct catalog));
new_node->name = malloc(strlen(new_name)+1);
strcpy(new_node->name, new_name);
new_node->next = NULL;
new_node->firstbook = NULL;
return new_node;
}
struct book *newbook(char *booktitle, int number, char *country)
{
struct book* newbook = (struct book*) malloc(sizeof(struct book));
newbook->title = malloc(strlen(booktitle)+1);
newbook->country = malloc(strlen(country)+1);
strcpy(newbook->title, booktitle);
strcpy(newbook->country, country);
newbook->number = number;
newbook->new = NULL;
return newbook;
}
struct catalog *findcatalog(struct catalog** head, char *catalogname)
{
struct catalog* current;
current = *head;
while(current != NULL)
{
if(!strcmp(current->name,catalogname))
{
return current;
}
current = current->next;
}
return NULL;
}
struct catalog *findbookcatalog(struct catalog** head, int number) //function that deletes a book when used
{
struct catalog* current;
current = *head;
while(current != NULL)
{
while(current->firstbook != NULL)
{
if(current->firstbook->number == number)
{
return current;
}
current->firstbook = current->firstbook->new;
}
current = current->next;
}
return NULL;
}
struct book *findbook(struct catalog** head, int number)
{
struct catalog* current = findbookcatalog(head, number);
struct book* booklocation;
while(current->firstbook != NULL)
{
booklocation = current->firstbook;
if(current->firstbook->number == number)
{
return booklocation;
}
current->firstbook = current->firstbook->new;
}
return NULL;
}
void sortedBookInsert(struct catalog** head, char *catalogname, char *booktitle, int number, char *country)
{
struct catalog* searched;
struct book* pom;
struct book* ksiazka = newbook(booktitle, number, country);
searched = findcatalog(head, catalogname);
if(searched->firstbook == NULL || strcmp(searched->firstbook->title, ksiazka->title)>0)
{
ksiazka->new =searched->firstbook;
searched->firstbook = ksiazka;
}
else
{ pom = searched->firstbook;
while(pom->new!= NULL && strcmp(searched->firstbook->title, ksiazka->title)< 0)
{
pom = pom->new;
}
ksiazka->new = pom->new;
pom->new = ksiazka;
}
}
void sortedInsert(struct catalog** head,char *name)
{
struct catalog* current;
struct catalog* new_node = newcatalog(name);
if(new_node == NULL)
{
return;
}
if (*head == NULL || strcmp((*head)->name, new_node->name) > 0)
{
new_node->next = *head;
*head = new_node;
}
else
{
current = *head;
while (current->next!=NULL && strcmp(current->next->name, new_node->name) < 0)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
int main()
{
struct catalog* head = NULL;
sortedInsert(&head, "Kappa");
sortedInsert(&head, "Pxntry");
sortedInsert(&head, "Sdafscx");
sortedInsert(&head, "Saxzxc");
sortedInsert(&head, "Zsdas");
sortedInsert(&head, "Zzzzzzzz");
sortedInsert(&head, "Country");
sortedBookInsert(&head, "Country", "PKP", 11111, "Germany");
sortedBookInsert(&head, "Country", "Polacy", 11112, "Italy");
sortedBookInsert(&head, "Country", "Bus", 11234, "France");
sortedBookInsert(&head, "Country", "Poltics", 14111, "Russia");
printList(head);
findbookcatalog(&head, 11112); // this will "eat" "Bus" and "PKP", so books that appear before Polacy
printf("\n");
printList(head);
return 0;
}
It should print my nested lists two times, two times the same, but it deletes all books that are before the searched book.
Result:
Country Bus 11234 France
Country PKP 11111 Germany
Country Polacy 11112 Italy
Country Poltics 14111 Russia
Kappa
Pxntry
Saxzxc
Sdafscx
Zsdas
Zzzzzzzz
Country Polacy 11112 Italy
Country Poltics 14111 Russia
Kappa
Pxntry
Saxzxc
Sdafscx
Zsdas
Zzzzzzzz

This code is removing books from a list:
current->firstbook = current->firstbook->new;
You probably meant to use something like this:
current_book = current->firstbook;
while (current_book) {
if (current_book->number == number) {
return current_book;
}
current_book = current_book->new;
}
I changed this function which fixes your current problem:
struct catalog *findbookcatalog(struct catalog** head, int number) //function that deletes a book when used
{
struct catalog* current;
current = *head;
struct book *current_book;
while(current != NULL)
{
current_book = current->firstbook;
while(current_book != NULL)
{
if(current_book->number == number)
{
return current;
}
current_book = current_book->new;
}
current = current->next;
}
return NULL;
}
I also changed this function. It has the same problem, you just haven't tested it yet.
struct book *findbook(struct catalog** head, int number)
{
struct catalog* current = findbookcatalog(head, number);
struct book* booklocation;
if (current != NULL) {
booklocation = current->firstbook;
while(booklocation != NULL)
{
if(booklocation->number == number)
{
return booklocation;
}
booklocation = booklocation->new;
}
}
return NULL;
}

Related

Adding a linked list to another linked list in C programming

I am abeginner trying to add a linked list to another linked list using c. the problem is that the program is entering an infinite loop and i don't know why.
And here's the following c code
typedef struct bookInfo
{
int code;
struct bookInfo *next;
} bookInfo;
typedef struct subscriber
{
int code;
struct bookInfo *books;
struct subscriber *next;
struct subscriber *prec;
} subscriber;
typedef bookInfo *Book;
typedef subscriber *Subscriber;
typedef Subscriber *SubscriberList;
void newBook(Book *bk, int val)
{
bookInfo *new_node = malloc(sizeof(bookInfo));
bookInfo *last = *bk;
new_node->code = val;
new_node->next = NULL;
if (*bk == NULL)
{
*bk = new_node;
}
else
{
while (last->next != NULL)
last = last->next;
last->next = new_node;
}
}
Subscriber Add_book(Subscriber S, Book Bk)
{
bookInfo *newNode = malloc(sizeof(bookInfo));
bookInfo *tmp;
newNode->next = NULL;
newNode->code = Bk->code;
if (S == NULL)
printf("\nl'abonnee est nulle");
else
{
if (S->books == NULL)
S->books = newNode;
else
{
tmp = S->books;
while (tmp != NULL)
tmp = tmp->next;
tmp->next = newNode;
printf("\nl'ajout du livre a ete effectue");
};
}
return S;
};
Hope you guys can help me and thank you. I don't know if the problem in the function newBook or what and here it's my main function
int main()
{
book_ref, sub_ref = NULL;
newSubscriber(&sub_ref);
bookInfo b1 = {20,NULL};
Add_book(sub_ref, &b1);
printf("\n%d : %d", sub_ref->code, sub_ref->books->code);
}
In your code,
while (tmp != NULL) tmp = tmp->next;
When this loop ends, tmp is NULL, so the next line will try accessing null pointer.
You should correct it as, while(tmp->next != NULL)
In order to remove the infinite loop, All i had to do was to define the pointer of books in the subscriber struct to NULL
void newBook(Book *bk, int val)
{
bookInfo *new_node = malloc(sizeof(bookInfo));
bookInfo *last = *bk;
new_node->code = val;
new_node->next = NULL;
new_node->books = NULL;
if (*bk == NULL)
{
*bk = new_node;
}
else
{
while (last->next != NULL)
last = last->next;
last->next = new_node;
}
}
Subscriber Add_book(Subscriber S, Book Bk)
{
bookInfo *newNode = malloc(sizeof(bookInfo));
bookInfo *tmp;
newNode->next = NULL;
newNode->code = Bk->code;
newNode->books = NULL;
if (S == NULL)
printf("\nl'abonnee est nulle");
else
{
if (S->books == NULL)
S->books = newNode;
else
{
tmp = S->books;
while (tmp != NULL)
tmp = tmp->next;
tmp->next = newNode;
printf("\nl'ajout du livre a ete effectue");
};
}
return S;
};
and everything worked.

Multiple data inside a single linked list node

Is it possible to have multiple data inside a single linked list node in C? And how do you input and access data with this?
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
char name[30];
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data, char string) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->name[30] = string;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void sortList() {
struct node *current = head, *index = NULL;
int temp;
if(head == NULL) {
return;
}
else {
while(current != NULL) {
index = current->next;
while(index != NULL) {
if(current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL) {
printf("%d - %s", current->data, current->name);
current = current->next;
}
printf("\n");
}
int main()
{
char string1[10] = "Aaron";
char string2[10] = "Baron";
char string3[10] = "Carla";
addNode(9, string1);
addNode(7, string2);
addNode(2, string3);
printf("Original list: \n");
display();
sortList();
printf("Sorted list: \n");
display();
return 0;
}
I don't understand why my code didn't work. I was trying to make use of single linked list where it can accept/input and print/output the number and the name at the same time.
What I want it to happen is to print the number and the name.
The output should be:
Carla - 2
Baron - 7
Aaron - 9
Please read my comments marked as // CHANGE HERE.
// CHANGE HERE: accept a character array as argument
void addNode(int data, char string[]) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
// CHANGE HERE: copy char array argument to name
strncpy(newNode->name, string, 30);
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void sortList() {
struct node *current = head, *index = NULL;
int temp;
char temp1[30];
if(head == NULL) {
return;
}
else {
while(current != NULL) {
index = current->next;
while(index != NULL) {
if(current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
// CHANGE HERE: swap the name along with data
strncpy(temp1, current->name, 30);
strncpy(current->name, index->name, 30);
strncpy(index->name, temp1, 30);
}
index = index->next;
}
current = current->next;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL) {
printf("%d - %s\n", current->data, current->name);
current = current->next;
}
printf("\n");
}

How to prevent the program to display the converted input, I'm wanting the program to display the original input

I want my code to display the original input 3 -> 2-> 1-> and not display the reversed one.
The output is displaying the 1-> 2-> 3-> . I want to see the original input without ruining the codes. How can I do that? Our professor taught us the concept of linked list and it is connected in this program to input a number and check if it is a palendrome
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
void insertNum(struct node** head, int number) {
struct node* temp = malloc(sizeof(struct node));
temp -> data = number;
temp -> next = *head;
*head = temp;
}
void display(struct node* head) {
struct node* printNode = head;
printf("displaying list1...\n");
printf("displaying the converted list..\n");
while (printNode != NULL) {
if (printNode->next)
printf("%d->",printNode->data);
else
printf("%d->NULL\n",printNode->data);
printNode=printNode->next;
}
}
struct node* reverseLL(struct node* head) {
struct node* reverseNode = NULL, *temp;
if (head == NULL) {
printf("\nThe list is empty.\n\n");
exit(0);
}
while (head != NULL) {
temp = (struct node*) malloc(sizeof(struct node));
temp -> data = head -> data;
temp -> next = reverseNode;
reverseNode = temp;
head = head -> next;
}
return reverseNode;
}
int check(struct node* LLOne, struct node* LLTwo) {
while (LLOne != NULL && LLTwo != NULL) {
if (LLOne->data != LLTwo->data)
return 0;
LLOne = LLOne->next;
LLTwo = LLTwo->next;
}
return (LLOne == NULL && LLTwo == NULL);
}
void deleteList(struct node** display) {
struct node* temp = *display;
while (temp != NULL) {
temp = temp->next;
free(*display);
*display = temp;
}
}
int main() {
int inputNum, countRun = 0, loop;
char choice;
struct node* reverseList;
struct node* head = NULL;
do {
printf("%Run number : %d\n", ++countRun);
printf("Enter 0 to stop building the list, else enter any integer\n");
printf("Enter list to check whether it is a palindrome... \n");
do {
scanf("%d", &inputNum);
if (inputNum == 0)
break;
insertNum(&head, inputNum);
} while (inputNum != 0);
display(head);
reverseList = reverseLL(head);
if ((check(head, reverseList)) == 1)
printf("\nPalindrome list.\n\n");
else
printf("\nNot palindrome.\n\n");
deleteList(&head);
} while (countRun != 2);
system("pause");
return 0;
}
Your insertNum inserts at the front of the list.
You need a version that appends at the back of the list:
void
appendNum(struct node **head, int number)
{
struct node *temp = malloc(sizeof(struct node));
struct node *cur;
struct node *prev;
temp->data = number;
// find tail of the list
prev = NULL;
for (cur = *head; cur != NULL; cur = cur->next)
prev = cur;
// append after the tail
if (prev != NULL)
prev->next = temp;
// insert at front (first time only)
else
*head = temp;
}
Here is the full code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void
appendNum(struct node **head, int number)
{
struct node *temp = malloc(sizeof(struct node));
struct node *cur;
struct node *prev;
temp->data = number;
// find tail of the list
prev = NULL;
for (cur = *head; cur != NULL; cur = cur->next)
prev = cur;
// append after the tail
if (prev != NULL)
prev->next = temp;
// insert at front (first time only)
else
*head = temp;
}
void
insertNum(struct node **head, int number)
{
struct node *temp = malloc(sizeof(struct node));
temp->data = number;
temp->next = *head;
*head = temp;
}
void
display(struct node *head)
{
struct node *printNode = head;
printf("displaying list1...\n");
printf("displaying the converted list..\n");
while (printNode != NULL) {
if (printNode->next)
printf("%d->", printNode->data);
else
printf("%d->NULL\n", printNode->data);
printNode = printNode->next;
}
}
struct node *
reverseLL(struct node *head)
{
struct node *reverseNode = NULL,
*temp;
if (head == NULL) {
printf("\nThe list is empty.\n\n");
exit(0);
}
while (head != NULL) {
temp = (struct node *) malloc(sizeof(struct node));
temp->data = head->data;
temp->next = reverseNode;
reverseNode = temp;
head = head->next;
}
return reverseNode;
}
int
check(struct node *LLOne, struct node *LLTwo)
{
while (LLOne != NULL && LLTwo != NULL) {
if (LLOne->data != LLTwo->data)
return 0;
LLOne = LLOne->next;
LLTwo = LLTwo->next;
}
return (LLOne == NULL && LLTwo == NULL);
}
void
deleteList(struct node **display)
{
struct node *temp = *display;
while (temp != NULL) {
temp = temp->next;
free(*display);
*display = temp;
}
}
int
main()
{
int inputNum, countRun = 0, loop;
char choice;
struct node *reverseList;
struct node *head = NULL;
do {
printf("%Run number : %d\n", ++countRun);
printf("Enter 0 to stop building the list, else enter any integer\n");
printf("Enter list to check whether it is a palindrome... \n");
do {
scanf("%d", &inputNum);
if (inputNum == 0)
break;
#if 0
insertNum(&head, inputNum);
#else
appendNum(&head, inputNum);
#endif
} while (inputNum != 0);
display(head);
reverseList = reverseLL(head);
if ((check(head, reverseList)) == 1)
printf("\nPalindrome list.\n\n");
else
printf("\nNot palindrome.\n\n");
deleteList(&head);
} while (countRun != 2);
system("pause");
return 0;
}

Problem with the print output from binary tree

I have to read data from file countries.txt. The file contains countries name and their ID, insert them in a linked list (sorted insert alph.). Linked list node contains name of country, ID of country and pointer to root of binary tree which contains cities of that country. I have to insert cities from other file (cities.txt).
I insert countries successfully but I cant insert cities.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct CITY{
char *name;
struct CITY *left;
struct CITY *right;
}nodeCity;
typedef struct COUNTRY{
char *name;
int id;
struct CITY *root;
struct COUNTRY *next;
}nodeCountry;
int listInsert(nodeCountry**, char*, int);
int printList(nodeCountry*);
nodeCity* findCity(nodeCountry*, int);
nodeCity* treeInsert(nodeCity*, char*);
int inorderPrint(nodeCity*);
int main()
{
nodeCountry *head;
int result, ID, k;
FILE *cities, *countries;
char buffer[1024];
nodeCity *root;
head = NULL;
countries = fopen("countries.txt", "r");
while(fscanf(countries, "%s %d", buffer, &ID) == 2)
result = listInsert(&head, buffer, ID);
cities = fopen("cities.txt", "r");
while(fscanf(cities, "%s %d", buffer, &ID) == 2)
{
root = findCity(head, ID);
root = treeInsert(root, buffer);
}
//result = printList(head);
scanf("%d", &k);
root = findCity(head, k);
result = inorderPrint(root);
return 0;
}
int inorderPrint(nodeCity *root)
{
int result;
if(root == NULL)
return NULL;
result = inorderPrint(root->left);
printf("%s\n", root->name);
result = inorderPrint(root->right);
return 1;
}
nodeCity* treeInsert(nodeCity *root, char *buffer)
{
printf("K");
if(root == NULL){
root = (nodeCity*)malloc(sizeof(nodeCity));
root->name = strdup(buffer);
root->left = NULL;
root->right = NULL;
}
else if(strcmp(buffer, root->name) > 0)
root->right = treeInsert(root->right, buffer);
else if(strcmp(buffer, root->name) < 0)
root->left = treeInsert(root->left, buffer);
return root;
}
nodeCity* findCity(nodeCountry *head, int ID)
{
while(head->id != ID)
head = head->next;
return head->root;
}
int listInsert(nodeCountry **head, char *buffer, int ID)
{
nodeCountry *current, *new_node;
if((*head) == NULL)
{
(*head) =
(nodeCountry*)malloc(sizeof(nodeCountry));
(*head)->name = strdup(buffer);
(*head)->id = ID;
(*head)->root = NULL;
(*head)->next = NULL;
return 1;
}
if(strcmp(buffer, (*head)->name) < 0)
{
new_node =
(nodeCountry*)malloc(sizeof(nodeCountry));
new_node->name = strdup(buffer);
new_node->id = ID;
new_node->root = NULL;
new_node->next = (*head);
(*head) = new_node;
return 1;
}
current = (*head);
while(current->next != NULL && strcmp(buffer, current->next->name) > 0)
current = current->next;
new_node = (nodeCountry*)malloc(sizeof(nodeCountry));
new_node->name = strdup(buffer);
new_node->id = ID;
new_node->next = current->next;
current->next = new_node;
new_node->root = NULL;
return 1;
}
int printList(nodeCountry *current)
{
while(current != NULL)
{
printf("%s\n", current->name);
current = current->next;
}
return 1;
}

Remove punctuation from string then put it back after replacement

My assignment was to read int input from two files. One contained a poem with misspelled words and the other contained a key with the misspelled word and the correct replacement right after.
I'm supposed to populate two linked lists with the information from each file and create a function that decodes the first file. I'm required to use pointers instead of char arrays in the linked list and at the end the program needs to print the first file with all corrections made.
I'm all good up until the decoder function needs to compare words with punctuation in them. How would i ignore punctuation without losing it in the final format.
Here's my decoder function:
LINK *decoder(TRANS *codet, LINK *head)
{
LINK *currentt;
currentt = head;
TRANS *current;
current = codet;
printf("Decoding...\n");
while (currentt != NULL)
{
current = codet;
while (1)
{
if ()
printf("Comparing %s with %s: \n", currentt->words, current->word1);
if (!strcmp(currentt->words, current->word1))
{
printf("Replacing...\n");
currentt->words = (char*)calloc(strlen(current->word2), sizeof(char));
strcpy(currentt->words, current->word2);
break;
}
current = current->next;
}
currentt = currentt->next;
}
return head;
}
Here's the rest of my code:
//Tristan Shepherd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node
{
char *words;
struct node *next;
};
struct codex
{
char *word1;
char *word2;
struct codex *next;
};
typedef struct node LINK;
typedef struct codex TRANS;
void printInsert(LINK *head)
{
printf("\n\nPrinting list: \n\n");
LINK *current;
current = head;
while (current != NULL) {
printf("%s ", current->words);
current = current->next;
}
}
void printCodex(TRANS *codet)
{
printf("\n\nPrinting code: \n\n");
TRANS *current;
current = codet;
while (current != NULL) {
printf("%s %s\n", current->word1, current->word2);
current = current->next;
}
}
void reverse(LINK **head)
{
struct node *prev = NULL;
struct node *current = *head;
struct node *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
LINK *insertList(char *wordt, LINK *head)
{
LINK *current, *temp;
temp = (LINK *)malloc(sizeof(LINK));
temp->words = (char*)calloc(strlen(wordt)+1, sizeof(char));
strcpy(temp->words, wordt);
if (head == NULL)
{
head = (LINK *)malloc(sizeof(LINK));
head = temp;
temp->next = NULL;
return head;
}
current = head;
if (strcmp(current->words, wordt))
{
temp->next = current;
head = temp;
return head;
}
current = head;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->words, wordt))
{
temp->next = current->next;
current->next = temp;
return head;
}
current = current->next;
}
}
TRANS *insertCodex(char *codeword, char *replace, TRANS *codet)
{
TRANS *current, *temp;
temp = (TRANS *)malloc(sizeof(TRANS));
temp->word1 = (char*)calloc(strlen(codeword)+1, sizeof(char));
temp->word2 = (char*)calloc(strlen(replace)+1, sizeof(char));
strcpy(temp->word1, codeword);
strcpy(temp->word2, replace);
if (codet == NULL)
{
codet = (TRANS *)malloc(sizeof(TRANS));
codet = temp;
temp->next = NULL;
return codet;
}
current = codet;
if (strcmp(current->word1, codeword) && strcmp(current->word2, replace))
{
temp->next = current;
codet = temp;
return codet;
}
current = codet;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->word1, codeword) && strcmp(current->next->word2, replace))
{
temp->next = current->next;
current->next = temp;
return codet;
}
current = current->next;
}
}
TRANS *scanCodex(FILE *code, TRANS *codet)
{
char *codeword = (char*)malloc(13*sizeof(char));
char *replace = (char*)malloc(13*sizeof(char));
while(1)
{
fscanf(code, "%s %s", codeword, replace);
if (feof(code)) break;
codet = insertCodex(codeword, replace, codet);
}
fclose(code);
return codet;
}
LINK *scanInsert(FILE *stream, LINK *head)
{
char *input = (char*)malloc(13*sizeof(char));
while (1)
{
fscanf(stream, "%s", input);
if(feof(stream)) break;
head = insertList(input, head);
}
fclose(stream);
return head;
}
LINK *decoder(TRANS *codet, LINK *head)
{
LINK *currentt;
currentt = head;
TRANS *current;
current = codet;
printf("Decoding...\n");
while (currentt != NULL)
{
current = codet;
while (1)
{
if ()
printf("Comparing %s with %s: \n", currentt->words, current->word1);
if (!strcmp(currentt->words, current->word1))
{
printf("Replacing...\n");
currentt->words = (char*)calloc(strlen(current->word2), sizeof(char));
strcpy(currentt->words, current->word2);
break;
}
current = current->next;
}
currentt = currentt->next;
}
return head;
}
int main (void)
{
FILE *stream = fopen("hw10data.txt", "r");
FILE *code = fopen("hw10codex.txt", "r");
LINK *head;
TRANS *codet;
head = NULL;
codet = NULL;
head = scanInsert(stream, head);
reverse(&head);
printInsert(head);
codet = scanCodex(code, codet);
printCodex(codet);
head = decoder(codet, head);
printInsert(head);
exit(0);
}
#David C. Rankin
Contents of the Files:
File 1:
Eye I
eye I
chequer checker
Pea P
Sea C
plane plainly
lee skip
four for
revue review
Miss Mistakes
Steaks skip
knot not
sea see
quays keys
whirred word
weight wait
Two To
two to
Weather Whether
write right
oar or
aweigh away
threw through
Your You're
shore sure
two to
no know
Its It's
vary very
weigh way
tolled told
sew so
bless blessed
freeze frees
yew you
lodes loads
thyme time
right write
stiles styles
righting writing
aides aids
rime rhyme
frays phrase
come composed
posed skip
trussed trusted
too to
bee be
joule jewel
cheque check
sum some
File 2:
Eye have a spelling chequer,
It came with my Pea Sea.
It plane lee marks four my revue,
Miss Steaks I can knot sea.
Eye strike the quays and type a whirred,
And weight four it two say,
Weather eye am write oar wrong,
It tells me straight aweigh.
Eye ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My chequer tolled me sew.
A chequer is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when eye rime.
Each frays come posed up on my screen,
Eye trussed too bee a joule.
The chequer pours over every word,
Two cheque sum spelling rule.
Here's my final code if you need it:
//Tristan Shepherd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node
{
char *words;
struct node *next;
};
struct codex
{
char *word1;
char *word2;
struct codex *next;
};
typedef struct node LINK;
typedef struct codex TRANS;
void delete(LINK **head, char *key)
{
LINK *temp = *head, *prev;
if (temp != NULL && !strcmp(temp->words, key))
{
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && strcmp(temp->words, key))
{
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printInsert(LINK *head, int aftersort)
{
printf("\n\nPrinting list: \n\n");
LINK *current;
current = head;
while (current != NULL)
{
if (aftersort)
{
printf("%s", current->words);
}
else
{
printf("%s ", current->words);
}
current = current->next;
}
}
void printCodex(TRANS *codet)
{
printf("\n\nPrinting codex: \n\n");
TRANS *current;
current = codet;
while (current != NULL)
{
printf("%s %s\n", current->word1, current->word2);
current = current->next;
}
}
void reverse(LINK **head)
{
struct node *prev = NULL;
struct node *current = *head;
struct node *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
LINK *insertList(char *wordt, LINK *head)
{
LINK *current, *temp;
temp = (LINK *)malloc(sizeof(LINK));
temp->words = (char*)calloc(strlen(wordt)+1, sizeof(char));
strcpy(temp->words, wordt);
if (head == NULL)
{
head = (LINK *)malloc(sizeof(LINK));
head = temp;
temp->next = NULL;
return head;
}
current = head;
if (strcmp(current->words, wordt))
{
temp->next = current;
head = temp;
return head;
}
current = head;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->words, wordt))
{
temp->next = current->next;
current->next = temp;
return head;
}
current = current->next;
}
}
TRANS *insertCodex(char *codeword, char *replace, TRANS *codet)
{
TRANS *current, *temp;
temp = (TRANS *)malloc(sizeof(TRANS));
temp->word1 = (char*)calloc(strlen(codeword)+1, sizeof(char));
temp->word2 = (char*)calloc(strlen(replace)+1, sizeof(char));
strcpy(temp->word1, codeword);
strcpy(temp->word2, replace);
if (codet == NULL)
{
codet = (TRANS *)malloc(sizeof(TRANS));
codet = temp;
temp->next = NULL;
return codet;
}
current = codet;
if (strcmp(current->word1, codeword) && strcmp(current->word2, replace))
{
temp->next = current;
codet = temp;
return codet;
}
current = codet;
while (current != NULL)
{
if (current->next == NULL || strcmp(current->next->word1, codeword) && strcmp(current->next->word2, replace))
{
temp->next = current->next;
current->next = temp;
return codet;
}
current = current->next;
}
}
TRANS *scanCodex(FILE *code, TRANS *codet)
{
char *codeword = (char*)malloc(13*sizeof(char));
char *replace = (char*)malloc(13*sizeof(char));
while(1)
{
fscanf(code, "%s %s", codeword, replace);
if (feof(code)) break;
codet = insertCodex(codeword, replace, codet);
}
fclose(code);
return codet;
}
LINK *scanInsert(FILE *stream, LINK *head)
{
char *input = (char*)malloc(13*sizeof(char));
while (1)
{
fscanf(stream, "%s", input);
if(feof(stream)) break;
head = insertList(input, head);
}
fclose(stream);
return head;
}
LINK *decoder(TRANS *codet, LINK *head)
{
LINK *currentt;
currentt = head;
TRANS *current;
current = codet;
char *temp = (char*)malloc(33*sizeof(char));
while (currentt != NULL)
{
int CorP = 0;
int punct = 0;
int t = 0;
current = codet;
while (1)
{
if (!strcmp(currentt->words, current->word1))
{
currentt->words = (char*)calloc(strlen(current->word2)+1, sizeof(char));
strcpy(currentt->words, current->word2);
strcat(currentt->words, " ");
if (punct == 1)
{
strtok(currentt->words, " ");
strcat(currentt->words, ".\n");
}
if (punct == 2)
{
strtok(currentt->words, " ");
strcat(currentt->words, ",\n");
}
if (!strcmp(currentt->words, "skip "))
{
delete(&head, currentt->words);
}
break;
}
current = current->next;
if (current == NULL)
{
strcpy(temp, currentt->words);
if (!strcmp(currentt->words, strtok(temp, ".")))
{
if(!strcmp(currentt->words, strtok(temp, ",")))
{
if(t == 1)
{
strcat(currentt->words, " ");
if (punct == 1)
{
strtok(currentt->words, " ");
strcat(currentt->words, ".\n");
}
if (punct == 2)
{
strtok(currentt->words, " ");
strcat(currentt->words, ",\n");
}
break;
}
t++;
}
else
{
strcpy(currentt->words, strtok(currentt->words, ","));
current = codet;
punct = 2;
}
}
else
{
strcpy(currentt->words, strtok(currentt->words, "."));
current = codet;
punct = 1;
}
current = codet;
}
}
currentt = currentt->next;
}
return head;
}
int main (void)
{
FILE *stream = fopen("hw10data.txt", "r");
FILE *code = fopen("hw10codex.txt", "r");
LINK *head;
TRANS *codet;
head = NULL;
codet = NULL;
head = scanInsert(stream, head);
reverse(&head);
printInsert(head, 0);
codet = scanCodex(code, codet);
printCodex(codet);
head = decoder(codet, head);
printInsert(head, 1);
exit(0);
}

Resources