C で簡単なテキスト エディターをプログラムしようとしており、LinkedList を使用しています。deleteEnd 関数に問題があります。どこで私は間違えましたか?
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
キャラクターとキャラクターの座標を次のような構造に保存します。
struct node {
struct node *previous;
char c;
int x;
int y;
struct node *next;
}*head;
これは、文字が入力されるたびに呼び出されます。
void characters(char 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()
{
struct node *temp,*last;
temp=head;
while(temp!=NULL)
{
last=temp;
temp=temp->next;
}
if(last->previous== NULL)
{
free(temp);
head=NULL;
}
last=NULL;
temp->previous=last;
free(temp);
}
ここからすべてが始まります。
main()
{
char c; //for storing the character
int x,y; //for the position of the character
clrscr();
for(;;)
{
c=getch();
x=wherex();
y=wherey();
if(c==0x1b) //escape for exit
{
exit(0);
}
else if (c==8) //for backspace
{
deletesEnd();
// clrscr();
printer();
}
else //normal characters
{
characters(c,x,y);
printer();
}
}
}