Data structures 我必须重新开始吗?

Data structures 我必须重新开始吗?,data-structures,module,linked-list,Data Structures,Module,Linked List,请简单明了地告诉我。我试图实现一个带有哨兵节点的循环双链接列表,并提出了一些更高或更低的游戏,可以在玩家之间来回循环。它很好用。。。。除了我意识到我必须有两个单独的环模块文件(a.h和a.c),然后是一个单独的主文件。代码很长,在我意识到我的错误后,我还没有整理好它,因为这是毫无意义的。所以我不是要你读它或检查错误或任何事情。但如果你能略过一点,用1-10分来告诉我情况有多糟糕,我将非常感激。只是为了让我的头脑知道我将要做什么。。。谢谢 #include <stdio.h> #inc

请简单明了地告诉我。我试图实现一个带有哨兵节点的循环双链接列表,并提出了一些更高或更低的游戏,可以在玩家之间来回循环。它很好用。。。。除了我意识到我必须有两个单独的环模块文件(a.h和a.c),然后是一个单独的主文件。代码很长,在我意识到我的错误后,我还没有整理好它,因为这是毫无意义的。所以我不是要你读它或检查错误或任何事情。但如果你能略过一点,用1-10分来告诉我情况有多糟糕,我将非常感激。只是为了让我的头脑知道我将要做什么。。。谢谢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define PLAYERLIMIT 5

//Doubly linked list implementation
struct Node {
    char data[20];
    struct Node* next;
    struct Node* prev;
};
struct Node* sentinel; //global pointer to the sentinel node
struct Node* head;
struct Node* tail;

// Create sentinel node, originally it just points to itself

struct Node* MakeSentinel () {
struct Node* SentinelNode =
(struct Node*)malloc(sizeof(struct Node));
SentinelNode->next = SentinelNode->next;
SentinelNode->prev = SentinelNode->prev;
return SentinelNode;
}

//Creation of a node takes an int and returns a node
struct Node* GetNewNode (char *x) {
    struct Node* newNode =
    (struct Node*)malloc(sizeof(struct Node)); //created node in the dynamic memory
strcpy (newNode->data, x); //temp->data is same as (*temp).data
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

//Returning a pointer to newly created node, inserts next to sentinel
void InsertAtHead (char *x){
    struct Node* newNode = GetNewNode(x);
if (sentinel == NULL) {
    sentinel = MakeSentinel();
    head = newNode;
    sentinel->next = head;
    sentinel->prev = head;
    head->next = sentinel;
    head->prev = sentinel;
    return;
}
head->prev = newNode;
newNode->next = head;
newNode->prev = sentinel;
sentinel->prev = newNode;
head = newNode;
}

void PrintHead() {
//    printf("Sentinel prev is %s\n", sentinel->prev->data);
struct Node* temp = sentinel->prev;
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("The players in the game are\n\n");
while (temp != sentinel) {
    printf ("%s ", temp->data);
    temp = temp->next;

}
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}

//Removing a name from the linked list
void DeleteEntry (struct Node* del) {
    if ((del->next != sentinel) && (del->prev != sentinel)) {
del->next->prev = del->prev;
del->prev->next = del->next;
}
else if (del->next == sentinel) {
    del->next->next = del->prev;
    del->prev->next = del->next;
}
else if(del->prev == sentinel) {
    del->next->prev = del->prev;
    del->prev->prev = del->next;
}
//    printf("Sentinel prev is now%s\n", sentinel->prev->data);

return;
}



int main(int argc, char *argv[]) {

//Entering all player names into the doubly linked list
printf ("Enter the names of the players. Press Enter after each new player\n");
printf ("Must have 5 Players'.'\n\n");

int i = 1;
char name[20];


while ((i <= PLAYERLIMIT)) {

    printf("Player: ");
    scanf ("%s", name);
    InsertAtHead(name);
    i++;

}
PrintHead();

//Starting the Game

//Initialising Variables for the game
int nextCard;
int currentCard;
int score;
char oppositeGuess[20];
int userChoice;
int playGame = 1;
struct Node* CurrentPlayer = head;
struct Node* PlayerBefore;

//Setting up the random cards
int range;
srand(time(NULL));
range = (13 - 1) + 1;

nextCard = rand() % range + 2;
currentCard = rand() % range + 2;

while (playGame == 1) {

    //Change current card to past card before creating a new current card
    currentCard = nextCard;

    //generate a random int for card

    nextCard = rand() % range + 2;

    if (currentCard < 11) {
        printf("\nThe current card is a %d.\n", currentCard);
    }
    else if (currentCard == 11) {
        printf("\nThe current card is a jack.\n");
    }
    else if (currentCard == 12) {
        printf("\nThe current card is a queen.\n");
    }
    else if (currentCard == 13) {
        printf("\nThe current card is a king.\n");
    }
    else if (currentCard == 14) {
        printf("\nThe current card is an ace.\n");
    }

    printf ("***%s it is your go!***\n", CurrentPlayer->data);
    if (CurrentPlayer->prev != sentinel) {
        PlayerBefore = CurrentPlayer->prev;

    }
    else {
        PlayerBefore = sentinel->next;

    }
//        printf("\nThe CurrentPlayer is %s\n", CurrentPlayer->data);
//        printf("The PlayerBefore is %s\n\n", PlayerBefore->data);
    printf("Will the next card be higher(1) or lower(2)?\n");
    scanf("%d", &userChoice);
    printf("\n");
    printf ("***%s would you like to guess the opposite?***\n", PlayerBefore->data);
    scanf("%s", oppositeGuess);
    if (strncmp(oppositeGuess, "Yes", 4) == 0) {

        if (userChoice == 1) {
            if (currentCard < nextCard) {
                printf("\nSorry, %s was correct. You are out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", PlayerBefore->data);
                DeleteEntry(PlayerBefore);

            }
            else if (currentCard > nextCard) {
                printf ("Congratulations! player %s was wrong and is now out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);

            }
            else if (currentCard == nextCard){
                printf("\nCards were equal. Next players turn.\n");
            }
        }
        else if (userChoice == 2) {
            if (currentCard < nextCard) {
                printf("Congratulations! player %s was wrong and is now out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);

            }
            else if (currentCard > nextCard) {
                printf ("\nSorry, %s was correct. You are out!\n", CurrentPlayer->data);
//                    printf ("\n IM GONNA DELETE %s\n", PlayerBefore->data);
                DeleteEntry(PlayerBefore);

            }
            else if (currentCard == nextCard){
                printf("\nCards were equal. Next players turn.\n");
            }
        }
    }
    if (strncmp(oppositeGuess, "No", 4) == 0) {

        if (userChoice == 1) {
            if (currentCard > nextCard) {
                printf ("\nSorry you have guessed incorrectly, you are out!\n");
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);

            }

            else if (currentCard < nextCard) {
                printf("\nCongratualtions you were correct, next players turn.\n");
            }
            else if (currentCard == nextCard) {
                printf("\nThe cards are the same. Next players turn.\n");
            }
        }
        else if (userChoice == 2) {
            if (currentCard > nextCard) {
                printf ("\nCongratualtions you were correct, next players turn.\n");

            }
            else if (currentCard < nextCard) {
                printf("\nSorry you have guessed incorrectly, you are out!\n");
//                    printf ("\n IM GONNA DELETE %s\n", CurrentPlayer->data);
                DeleteEntry(CurrentPlayer);


            }
            else if (currentCard == nextCard) {
                printf("\nThe cards are the same. Next players turn.\n");
            }
        }
        else {
            printf("\nPlease enter a valid choice.\n");
        }
    }

        PrintHead();
    if (CurrentPlayer->next != sentinel) {
        CurrentPlayer = CurrentPlayer->next;
    }
    else {
        CurrentPlayer = sentinel->prev;
    }

    if ((CurrentPlayer->next == sentinel) && (CurrentPlayer->prev == sentinel)) {
        playGame = 0;
    }
}
printf("%s you are the Winner!\n", CurrentPlayer->data);
}
#包括
#包括
#包括
#包括
#包括
#定义PLAYERLIMIT 5
//双链表实现
结构节点{
字符数据[20];
结构节点*下一步;
结构节点*prev;
};
结构节点*sentinel//指向sentinel节点的全局指针
结构节点*头部;
结构节点*尾部;
//创建sentinel节点,最初它只指向自身
结构节点*MakeSentinel(){
结构节点*SentinelNode=
(结构节点*)malloc(sizeof(结构节点));
SentinelNode->next=SentinelNode->next;
SentinelNode->prev=SentinelNode->prev;
返回哨兵节点;
}
//节点的创建接受一个int并返回一个节点
结构节点*GetNewNode(char*x){
结构节点*newNode=
(结构节点*)malloc(sizeof(结构节点));//在动态内存中创建节点
strcpy(newNode->data,x);//temp->data与(*temp).data相同
newNode->prev=NULL;
newNode->next=NULL;
返回newNode;
}
//返回指向新创建节点的指针,在sentinel旁边插入
void insertatead(char*x){
结构节点*newNode=GetNewNode(x);
if(sentinel==NULL){
sentinel=MakeSentinel();
头=新节点;
哨兵->下一步=头部;
哨兵->前方=头部;
头部->下一步=哨兵;
头部->前方=哨兵;
返回;
}
head->prev=newNode;
新建节点->下一步=头部;
newNode->prev=sentinel;
sentinel->prev=newNode;
头=新节点;
}
无效打印头(){
//printf(“Sentinel prev是%s\n”,Sentinel->prev->data);
结构节点*temp=sentinel->prev;
printf(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”);
printf(“游戏中的玩家是\n\n”);
while(temp!=哨兵){
printf(“%s”,临时->数据);
温度=温度->下一步;
}
printf(“\n”);
printf(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”);
}
//从链接列表中删除名称
void DeleteEntry(结构节点*del){
如果((删除->下一步!=哨兵)&&(删除->上一步!=哨兵)){
del->next->prev=del->prev;
del->prev->next=del->next;
}
else if(del->next==sentinel){
删除->下一步->下一步=删除->上一步;
del->prev->next=del->next;
}
否则如果(del->prev==哨兵){
del->next->prev=del->prev;
del->prev->prev=del->next;
}
//printf(“Sentinel prev现在是%s\n”,Sentinel->prev->data);
返回;
}
int main(int argc,char*argv[]){
//将所有玩家姓名输入双链接列表
printf(“输入玩家的姓名。在每个新玩家之后按Enter键\n”);
printf(“必须有5名玩家”。\n\n”);
int i=1;
字符名[20];
而(一)数据,;
如果(CurrentPlayer->prev!=哨兵){
PlayerBefore=CurrentPlayer->prev;
}
否则{
PlayerBefore=哨兵->下一步;
}
//printf(“\n当前播放器为%s\n”,当前播放器->数据);
//printf(“PlayerBefore是%s\n\n”,PlayerBefore->data);
printf(“下一张卡是更高(1)还是更低(2)?\n”);
scanf(“%d”、&userChoice);
printf(“\n”);
printf(“***%s您想猜测相反的结果吗?***\n”,PlayerBefore->data);
scanf(“%s”,反义猜测);
如果(strncmp(相反猜测,“是”,4)=0){
if(userChoice==1){
如果(当前卡data);
//printf(“\n我要删除%s\n”,在->数据之前播放);
删除条目(播放之前);
}
否则如果(当前卡>下一卡){
printf(“恭喜!播放器%s出错,现在退出!\n”,CurrentPlayer->data);
//printf(“\n我要删除%s\n”,CurrentPlayer->data);
删除输入(当前播放器);
}
else if(当前卡==nextCard){
printf(“\n字符相等。下一个玩家回合。\n”);
}
}
else if(userChoice==2){
如果(当前卡data);
//printf(“\n我要删除%s\n”,CurrentPlayer->data);
删除输入(当前播放器);
}
否则如果(当前卡>下一卡){
printf(“\n目录%s是正确的。您已退出!\n”,CurrentPlayer->data);
//printf(“\n我要删除%s\n”,在->数据之前播放);
删除条目(播放之前);
}
else if(当前卡==nextCard){
printf(“\n字符相等。下一个玩家回合。\n”);
}
}
}
如果(strncmp(相反猜测,“否”,4)=0){
if(userChoice==1){
如果(当前卡>下一卡){
printf(“\n如果您猜错了,您就出局了!\n”);
//printf(“\n我要删除%s\n”,CurrentPlayer->data);
删除输入(当前播放器);
}
否则如果(当前卡下一卡){
printf(“\n估计您是对的,下一个玩家轮到。\n”);
//Doubly linked list implementation
struct Node {
  char data[20];
  struct Node* next;
  struct Node* prev;
};

struct Node* sentinel; //global pointer to the sentinel node
struct Node* head;
struct Node* tail;

// Create sentinel node, originally it just points to itself

struct Node* MakeSentinel () {
  struct Node* SentinelNode = (struct Node*)malloc(sizeof(struct Node));
  SentinelNode->next = SentinelNode->next;
  SentinelNode->prev = SentinelNode->prev;
  return SentinelNode;
}

//Creation of a node takes an int and returns a node
struct Node* GetNewNode (char *x) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //created node in the dynamic memory
    strcpy (newNode->data, x); //temp->data is same as (*temp).data
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

//Returning a pointer to newly created node, inserts next to sentinel
void InsertAtHead (char *x){
    struct Node* newNode = GetNewNode(x);
    if (sentinel == NULL) {
      sentinel = MakeSentinel();
      head = newNode;
      sentinel->next = head;
      sentinel->prev = head;
      head->next = sentinel;
      head->prev = sentinel;
      return;
    }

 head->prev = newNode;
 newNode->next = head;
 newNode->prev = sentinel;
 sentinel->prev = newNode;
 head = newNode;
}

void PrintHead() {
//    printf("Sentinel prev is %s\n", sentinel->prev->data);
  struct Node* temp = sentinel->prev;
  printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  printf("The players in the game are\n\n");
  while (temp != sentinel) {
    printf ("%s ", temp->data);
    temp = temp->next;

  }
  printf("\n");
  printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}

//Removing a name from the linked list
void DeleteEntry (struct Node* del) {
    if ((del->next != sentinel) && (del->prev != sentinel)) {
      del->next->prev = del->prev;
      del->prev->next = del->next;
    }
    else if (del->next == sentinel) {
      del->next->next = del->prev;
      del->prev->next = del->next;
    }
    else if(del->prev == sentinel) {
      del->next->prev = del->prev;
      del->prev->prev = del->next;
    }
    //    printf("Sentinel prev is now%s\n", sentinel->prev->data);

    return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "mydllist.c"
typedef enum {true, false} bool;
int range;
srand(time(NULL));
range = (13 - 1) + 1 //n.b. (13 - 1) + 1 = 13 is there some reason you wrote it like this?
int range = NUMBER_OF_CARDS;
srand(time(NULL));
int nextCard = 0;
int currentCard = 0;
int score = 0;
char oppositeGuess[20] = "";
int userChoice = 0 ;
bool playGame = true;
struct Node* CurrentPlayer = head;
struct Node* PreviousPlayer = head->prev;
int num_entered_players = 1;
char name[20];

while ((entered_players <= PLAYERLIMIT)) {
    printf("Player: ");
    scanf ("%s", name);
    InsertAtHead(name);
    num_entered_players++;
}
if (currentCard < 11) {
    printf("\nThe current card is a %d.\n", currentCard);
}
else if (currentCard == 11) {
    printf("\nThe current card is a jack.\n");
}
else if (currentCard == 12) {
    printf("\nThe current card is a queen.\n");
}
else if (currentCard == 13) {
    printf("\nThe current card is a king.\n");
}
else if (currentCard == 14) {
    printf("\nThe current card is an ace.\n");
}
void printCurrentCard(int currentCard){

  if (currentCard < 11) {
        printf("\nThe current card is a %d.\n", currentCard);
    }
    else if (currentCard == 11) {
        printf("\nThe current card is a jack.\n");
    }
    else if (currentCard == 12) {
        printf("\nThe current card is a queen.\n");
    }
    else if (currentCard == 13) {
        printf("\nThe current card is a king.\n");
    }
    else if (currentCard == 14) {
        printf("\nThe current card is an ace.\n");
    }   
}
while (playGame == true) {

    //Change current card to past card before creating a new current card
    currentCard = nextCard;

    //generate a random int for card
    nextCard = rand() % range + 2;

    printCurrentCard(currentCard);

....
}