String链接列表返回空符号

时间:2013-10-25 04:41:19

标签: c

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

e.g。 命令?嘿嘿 命令?洛尔 命令? PRL 嘿 大声笑 命令?德嘿 命令? PRL 洛尔

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

#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;
    }
  }
}

1 个答案:

答案 0 :(得分: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;
        }
    }
}