本文介绍了插入链表的Turbo C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图程序C.文本编辑器我有一个链表中插入一个元素的麻烦。该计划根本不会插入链表的中间任何东西。

I am trying to program a text editor in C. I am having trouble with inserting an element in a linked list. The program simply won't insert anything in the middle of the linked list.

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

}

这工作就好了太多:

this works just fine too:

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->Ÿ== ypos))的条件语句。因此,该方案应该插入元素,但它没有。 (

I tried to debug it, it goes inside the loop with the conditional statement of ((temp->x==xpos && temp->y==ypos)) Thus, the program is supposed to insert an element but it doesn't. :(

推荐答案

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

Try concept of insertion from this one - It can insert a node from front end

只是做一个新的指针到节点并将其命名为创建

Just make a new pointer to node and name it 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;

这篇关于插入链表的Turbo C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 04:04