[英]LinkedList Delete End in C
我正在尝试使用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();
}
}
}
为了找到列表中的最后一个节点,最简单的方法是循环直到next
指针为NULL
为止。
对于最后的删除,您还需要跟踪倒数第二个节点,它看起来可能像这样:
struct node *prev, *curr;
for (prev = NULL, curr = head; curr->next != NULL; prev = curr, curr = curr->next)
;
/* `curr` is now the last node in the list, `prev` is the next-to-last node */
if (prev != NULL)
prev->next = NULL; /* Unlink the last node */
else
head = NULL; /* List was only one node long */
/* Free the memory for the last node */
free(curr);
如果您正确地使列表保持双向链接,则循环将更加容易,因为您不必再跟踪倒数第二个节点了:
struct node *node;
for (node = head; node->next != NULL; node = node->next)
;
/* `node` is now the last node in the list */
if (node->previous != NULL)
node->previous->next = NULL; /* Unlink last node */
else
head = NULL; /* List was only one node long */
/* Free the memory for the last node */
free(curr);
从一开始就跟踪尾巴将使事情变得简单得多:
if (tail != NULL)
{
struct node *node = tail;
if (node->previous != NULL)
{
node->previous->next = NULL; /* Unlink last node */
tail = node->previous;
}
else
head = tail = NULL; /* Removing last node in list */
free(node);
}
尝试这个 :
void deletesEnd()
{
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;
}
在算法中,您尝试使用NULL指针,因此无法到达我认为的上一个节点。
尝试这个
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->previous->next = NULL;
last=NULL;
temp->previous=last;
free(temp);
}
您会看到,您已将last元素放在list中的last
,然后执行last=NULL;
只是将null放在last
变量中。 temp
已经是NULL
所以temp->previous=last;
没有任何意义。 您需要最后一项的上一项指向NULL
作为行
last->previous->next = NULL;
不
这行:
temp->previous=last;
因为temp在该点已经为空,这将给您带来麻烦(因为这是while循环中的终止条件)。
将您的方法更改为:
void deletesEnd()
{
struct node *temp,*last;
temp=head;
while(temp!=NULL)
{
last=temp;
temp=temp->next;
}
if(last != NULL) {
if (last->previous != null) { // Last element gonig to be deleted so the previous element if exists should point his next to null (end of list)
last->previous->next = null;
}
free(last); // Free the last element in the list
}
}
链表仅使用两个简单的通用规则...
只是检查条件...
尝试这些规则。 希望它对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.