0

我想创建一个程序来实现一个行编辑器,它将行作为输入并打印它们

我正在使用游标来操作单链表,但由于指针类型不兼容,在 displayFromCursor、displayAll、moveCursor 函数中出现错误,我哪里出错了?请帮忙,我现在被困在这里

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

typedef struct node
{
    char line[300];
    struct node *link;
}NODE;

typedef struct cursor
{
    NODE *prev;
    NODE *next;
}CURSOR;

typedef struct list
{
    NODE *link;
    int count;
}LIST;

void takeCommand(CURSOR **pCursor,LIST **pList);
void insertLine(CURSOR **pCursor,LIST**);
void deleteLine(CURSOR **pCursor,LIST **pList);
void moveCursor(CURSOR **pCursor,LIST **pList);
void displayFromCursor(CURSOR **pCursor);
void displayAll(LIST **pList);

void main(void)
{
    LIST *pList;
    pList->count = 0;
    pList->link = NULL;

    CURSOR *pCursor;
    pCursor->prev = NULL;
    pCursor->next = NULL;

    takeCommand(&pCursor,&pList);
    printf("BYE\n");
    return;
}

void takeCommand(CURSOR **pCursor,LIST **pList)
{
    char c;
    printf(">");
    c=getchar();
    switch(c)
    {
    case 'I': insertLine(pCursor,pList);
        case 'D': deleteLine(pCursor,pList);
            case 'M': moveCursor(pCursor,pList);
                case 'L': displayFromCursor(pCursor);
                    case 'A': displayAll(pList);
                        case 'X':return;
                        default : printf("Enter correct value again");
    }
    takeCommand(pCursor,pList);
}

void insertLine(CURSOR **pCursor,LIST **pList)
{
    char newLine[300];
    bool x=true;
    do
    {
        if(strcmp(newLine,"//"))
        {
            x=false;
        }
        else
        {
            scanf("%s",newLine);
            NODE *pNew;
            pNew = (NODE*)malloc(sizeof(NODE));
            strcpy(pNew->line,newLine);
            pNew->link = (*pCursor)->next;
            (*pCursor)->prev = pNew;
            ((*pList)->count)++;
        }
    }while(true);
    return;
}

void deleteLine(CURSOR **pCursor,LIST **pList)
{
    NODE *temp;
    temp->link = (*pCursor)->next;
    (*pCursor)->next = ((*pCursor)->next)->link;
    free(temp);
    ((*pList)->count)--;
    return;
}

void displayFromCursor(CURSOR **pCursor)
{
    if((*pCursor)->next !=NULL)
    {
        printf("%s\n",(*pCursor)->next->line);
        displayFromCursor( ((*pCursor)->next) );
    }
    return;
}

void displayAll(LIST **pList)
{
    if((*pList)->link !=NULL)
    {
        printf("%s\n",(*pList)->link->line);
        displayFromCursor( ((*pList)->link) );
    }
    return;
}

void moveCursor(CURSOR **pCursor,LIST **pList)
{
    int countNew=(*pList)->count;
    int temp,i;
    printf("Enter the number to move cursor\n");
    scanf("%d",&temp);
    countNew-=temp;
    while((*pCursor)->next !=NULL)
    {
        (*pCursor) = (*pCursor)->next;
        countNew--;
    }
    (*pCursor)->next = (*pList)->link;
    (*pCursor)->prev = NULL;
    for(i=0;i<countNew;i++)
    {
        (*pCursor)->prev = (*pCursor)->next;
        (*pCursor)->next = (*pCursor)->next->link;
    }
    return;
}
4

1 回答 1

0

问题在排队

displayFromCursor( ((*pCursor)->next) );

其中函数 displayFromCursor 期望struct CURSOR **作为参数,但实际上参数的类型是struct NODE *.Similarly in function displayAll。您只需要pCursor作为参数传递。

于 2013-06-25T13:35:51.800 回答