0

我正在尝试用 C 编写文本编辑器。在链接列表中插入元素时遇到问题。程序根本不会在链表中间插入任何东西。

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

我使用了单链表。

struct node {
struct node *previous;
int c;
int x;
int y;
struct node *next;
}*head;

这工作正常:

void characters(int typed, int xpos, int ypos)      //assign values of a node
{
struct node *temp,*var,*temp2;
temp=(struct node *)malloc(sizeof(struct node));
temp->c=typed;
temp->x=xpos;
temp->y=ypos;

if(head==NULL)
{
    head=temp;
    head->next=NULL;
}

else
{
    temp2=head;
    while(temp2!=NULL)
    {
        var=temp2;
        temp2=temp2->next;
    }
    temp2=temp;
    var->next=temp2;
    temp2->next=NULL;
}

}

这也很好用。

void printer()                          //to print everything
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
    gotoxy(temp->x,temp->y);
    printf("%c",temp->c);
    temp=temp->next;
}

}

这也很好用:

void deletesEnd                  //delete at the end
{
struct node *temp,*last;
temp=head;
last=temp;
while(temp!=NULL && temp->next!=NULL)
{
    last=temp;
    temp=temp->next;
}
if(last==temp)
    {
        free(temp);
        head=NULL;
    }
else{
free(last->next);
last->next=NULL;
}
} 

这就是问题:

 void checker(int ch, int xpos, int ypos)
{
int flag=0;
struct node *temp,*temp1,*insert_node;
temp=head;
while(temp!=NULL)
{
    if(temp->x==xpos && temp->y==ypos)
    {
        temp1=temp;
        temp=insert_node;
        insert_node->c=ch;
        insert_node->x=xpos;
        insert_node->y=ypos;
        insert_node->next=temp1;
        flag=1;
        break;
    }

    else
        temp= temp->next;
}
free(temp);
free(temp1);
if(flag==0)
    characters(ch,xpos,ypos);
}

main()
{
int c;                          //for storing the character
int x,y;                    //for the position of the character
clrscr();
for(;;)
{
    c=getch();
    x=wherex();
    y=wherey();
    if(c==27)
        exit(0);

    else if(c==0|| c==224)
    {
        switch(getch())
        {
            case 72:                    //for up
                    gotoxy(x,y-1);
                    break;
            case 80:                    //for down
                    gotoxy(x,y+1);
                    break;
            case 75:                    //for left
                    gotoxy(x-1,y);
                    break;
            case 77:                    //for right
                    gotoxy(x+1,y);
                    break;
         }
    }

    else if(c==13)
    {
        printf("\n");
    }

    else if(c==8)                       //for backspace
    {
            deletesEnd();
            clrscr();
            printer();

    }

    else                        //for normal characters
    {
        checker(c,x,y);
           //   characters(c,x,y);
        printer();
    }
}
}

我试图调试它,它使用 ((temp->x==xpos && temp->y==ypos)) 的条件语句进入循环因此,程序应该插入一个元素,但它没有. :(

4

2 回答 2

0

也许您必须为要首先插入的元素malloca struct node,而不仅仅是声明 a struct node *

尝试添加struct node *insert_node = (struct node *)malloc(struct node)您的检查方法。

于 2013-08-04T12:41:48.327 回答
0

尝试从这个插入的概念 - 它可以从前端插入一个节点

只需创建一个指向节点的新指针并将其命名为 create

struct node *create;

int item;
printf("Enter a number you want to insert\n\t");
scanf("%d",&item);
create = (struct node*)malloc(sizeof(struct node*));
create->info = item;
create->ptr = first;
HEAD = create;
于 2013-08-04T12:51:47.203 回答