0

这是我一直在研究的一个程序,最近发布了很多对此感到抱歉的内容,而且我一直在进步并遇到错误。该程序允许您将字符串放入链接列表中,然后让您操作该列表。"ins" 允许您将字符串插入列表中,"del" 允许您删除这些字符串,而"prl" 允许您查看列表中的内容。我想我已经完成了相当多的工作,但是当您在“命令?”之后运行 prl 并且列表中还有项目时,它显示为空符号,当我希望它显示为我的字符串列表时.

例如命令?ins 嘿命令?ins 大声笑命令?prl 嘿 Lol 命令?del 嘿指挥部?哈哈

等等。我希望这是足够的描述性。如果您有任何意见,请告诉我,非常感谢!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MIN_LENGTH 4
#define MAX_LENGTH 11

struct node{
     char list;
      int count;
       struct node *next;
 };

typedef struct node Node;
typedef Node *ListNode;

void ins(ListNode *ptr, char value);
char del(ListNode *ptr, char value);
char prl(ListNode currPtr);


int main(void){

  ListNode startPtr = NULL;

  char com[MIN_LENGTH]; 
  char cho[MAX_LENGTH]; 

  while(strcmp(com, "end") != 0){
    printf("Command? ");
    scanf("%s", &com);

     if(strcmp(com, "ins") == 0){
       scanf("%s", &cho);
       ins(&startPtr, cho);
       printf("%s\n", cho);

        }

      else if(strcmp(com, "del") == 0){
    scanf("%s", &cho);
    if(del(&startPtr, cho)){
    }
    else{
      printf("%s not found.\n", cho);
    }
      }

      else if(strcmp(com, "prl") == 0){
    prl(startPtr);
       // printf("%s\n", cho);
    }

    else if(strlen(com) >= 4 || strlen(com) < 3){
    printf("You have entered an incorrect command.\n");
    }
  }
}


void ins(ListNode *ptr, char value){

  ListNode newPtr;
  ListNode prevPtr;
  ListNode currPtr;

  newPtr = (struct node*) malloc(sizeof(Node));

    if(newPtr != NULL){
    newPtr->list = value;
    newPtr->next = NULL;

    prevPtr = NULL;
    currPtr = *ptr;

    while(currPtr != NULL && value > currPtr-> list){
      prevPtr = currPtr;
      currPtr = currPtr->next;
    }
    if(prevPtr == NULL){
      newPtr->next = *ptr;
      *ptr = newPtr;
    }
    else{ 
      prevPtr->next = newPtr;
      newPtr->next = currPtr;
    }
   }
    else{
      printf("No memory available\n");
    }
}
char del(ListNode *ptr, char value){
  ListNode prevPtr;
  ListNode currPtr;
  ListNode tempPtr;

  if(value == (*ptr)->list){
    tempPtr = *ptr;
    *ptr = (*ptr)->next;
    free(tempPtr);
    return value;
  }
  else{
    prevPtr = *ptr;
    currPtr = (*ptr)->next;

    while(currPtr != NULL && currPtr->list != value){
      prevPtr = currPtr;
      currPtr = currPtr->next;
    }

    if(currPtr != NULL){
      tempPtr = currPtr;
      prevPtr->next = currPtr->next;
      free(tempPtr);
      return value;
    }
  }
  return '\0';
}

char prl(ListNode currPtr){
  if(currPtr == NULL){
    printf("The List is Empty.\n");
  }else{
    while(currPtr != NULL){
      printf("%c\n", currPtr->list);
      currPtr = currPtr->next;
    }
  }
}
4

1 回答 1

1

您的 del() 和 ins() 函数使用 char 而不是 char*,也是您的 Node 结构。

请注意我在代码中所做的更改:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MIN_LENGTH 4
#define MAX_LENGTH 11

struct node{
    char list[MAX_LENGTH];
    int count;
    struct node *next;
};

typedef struct node Node;
typedef Node *ListNode;

void ins(ListNode *ptr, char *value);
char* del(ListNode *ptr, char *value);
char prl(ListNode currPtr);


int main(void){

    ListNode startPtr = NULL;

    char com[MIN_LENGTH]; 
    char cho[MAX_LENGTH]; 

    while(strcmp(com, "end") != 0){
        printf("Command? ");
        scanf("%s", &com);

        if(strcmp(com, "ins") == 0){
            scanf("%s", &cho);
            ins(&startPtr, cho);
            printf("%s\n", cho);

        }

        else if(strcmp(com, "del") == 0){
            scanf("%s", &cho);
            if(del(&startPtr, cho)){
            }
            else{
                printf("%s not found.\n", cho);
            }
        }

        else if(strcmp(com, "prl") == 0){
            prl(startPtr);
            // printf("%s\n", cho);
        }

        else if(strlen(com) >= 4 || strlen(com) < 3){
            printf("You have entered an incorrect command.\n");
        }
    }
}


void ins(ListNode *ptr, char *value){

    ListNode newPtr;
    ListNode prevPtr;
    ListNode currPtr;

    newPtr = (ListNode) malloc(sizeof(Node));

    if(newPtr != NULL){
        memset(newPtr, 0, sizeof(Node));
        memcpy(newPtr->list, value, strlen(value));
        newPtr->next = NULL;

        prevPtr = NULL;
        currPtr = *ptr;

        while(currPtr != NULL && value > currPtr-> list){
            prevPtr = currPtr;
            currPtr = currPtr->next;
        }
        if(prevPtr == NULL){
            newPtr->next = *ptr;
            *ptr = newPtr;
        }
        else{ 
            prevPtr->next = newPtr;
            newPtr->next = currPtr;
        }
    }
    else{
        printf("No memory available\n");
    }
}
char* del(ListNode *ptr, char *value){
    ListNode prevPtr;
    ListNode currPtr;
    ListNode tempPtr;

    if(0 == strcmp(value, (*ptr)->list)){
        tempPtr = *ptr;
        *ptr = (*ptr)->next;
        free(tempPtr);
        return value;
    }
    else{
        prevPtr = *ptr;
        currPtr = (*ptr)->next;

        while(currPtr != NULL && 0 != strcmp(value, currPtr->list)){
            prevPtr = currPtr;
            currPtr = currPtr->next;
        }

        if(currPtr != NULL){
            tempPtr = currPtr;
            prevPtr->next = currPtr->next;
            free(tempPtr);
            return value;
        }
    }
    return '\0';
}

char prl(ListNode currPtr){
    if(currPtr == NULL){
        printf("The List is Empty.\n");
    }else{
        while(currPtr != NULL){
            printf("%s\n", currPtr->list);
            currPtr = currPtr->next;
        }
    }
}
于 2013-10-25T06:09:00.710 回答