I have received the following two errors as well as an additional one "'return': cannot convert from 'link *' to 'link'" for these lines of code. The other two errors are listed in the subject of the question. I am not sure how to fix the problem. Any suggestions are helpful.
I am trying to code the card game War, and this is what I have so far. I am testing the code at each interval and have ran into problems at the code lines listed below
link NEWnode(Card card, link next) {
link x;
x = malloc(sizeof *x); //allocate memory
if (x == NULL) {
printf("Out of memory. \n");
exit(EXIT_FAILURE);
}
x->next = next;
x->card = card;
return x;
}
The entire code is as follows:
#include <stdio.h>
#define DECKSIZE 52
typedef int Card;
int rank(Card c) {
return c % 13;
}
// allow for multi-deck war
int suit(Card c) {
return (c % 52) / 13;
}
// representing the cards
void showcard(Card c) {
switch (rank(c)) {
case 0: printf("Deuce of "); break;
case 1: printf("Three of "); break;
case 2: printf("Four of "); break;
case 3: printf("Five of "); break;
case 4: printf("Six of "); break;
case 5: printf("Seven of "); break;
case 6: printf("Eight of "); break;
case 7: printf("Nine of "); break;
case 8: printf("Ten of "); break;
case 9: printf("Jack of "); break;
case 10: printf("Queen of "); break;
case 11: printf("King of "); break;
case 12: printf("Ace of "); break;
}
switch (suit(c)) {
case 0: printf("Clubs\n"); break;
case 1: printf("Diamonds\n"); break;
case 2: printf("Hearts\n"); break;
case 3: printf("Spades\n"); break;
}
}
// testing the code
// representing the deck and hands (with linked lists because need direct access to top and bottom cards, draw cards from top, won cards go to bottom)
typedef struct node* link;
struct node {
Card card;
link next;
};
link Atop, Abot;
link Btop, Bbot;
// showing a hand
void showpile(link pile) {
link x;
for (x = pile; x != NULL; x = x->next)
showcard(x->card);
}
int countpile(link pile) {
link x;
int cnt = 0;
for (x = pile; x != NULL; x = x->next)
cnt++;
return cnt;
}
// Creating the 52 card Deck
#include <stdlib.h> //for malloc()
link NEWnode(Card card, link next) {
link x;
x = malloc(sizeof *x); //allocate memory
if (x == NULL) {
printf("Out of memory. \n");
exit(EXIT_FAILURE);
}
x->next = next;
x->card = card;
return x;
}
link makepile(int N) {
link x = NULL;
Card c;
for (c = N - 1; c >= 0; c--)
x = NEWnode(c, x);
return x;
}
// testing the code
int main(void) {
link deck;
deck = makepile(DECKSIZE);
showpile(deck);
return 0;
}