首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插入链表Turbo C

插入链表Turbo C
EN

Stack Overflow用户
提问于 2013-08-04 12:33:25
回答 2查看 1.2K关注 0票数 0

我试图在C中编写一个文本编辑器,我在链接列表中插入一个元素时遇到了困难。程序根本不会在链接列表的中间插入任何内容。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>

我用了单链列表。

代码语言:javascript
复制
struct node {
struct node *previous;
int c;
int x;
int y;
struct node *next;
}*head;

这样做很好:

代码语言:javascript
复制
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;
}

}

这也挺好的。

代码语言:javascript
复制
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;
}

}

这也很好:

代码语言:javascript
复制
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;
}
} 

这就是问题所在:

代码语言:javascript
复制
 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)),因此,程序应该插入一个元素,但它没有。

EN

回答 2

Stack Overflow用户

发布于 2013-08-04 12:41:48

也许您必须为要首先插入的元素创建一个malloc struct node,而不仅仅是声明一个struct node *

尝试在检查器方法中添加struct node *insert_node = (struct node *)malloc(struct node)

票数 0
EN

Stack Overflow用户

发布于 2013-08-04 12:51:47

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

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

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18042984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档