C:从双链接列表中仅删除一个节点
我正在编写一个程序,创建一个双链接列表并从中删除某个元素。除了列表中只有一个元素以及当我试图删除它时,程序崩溃的部分之外,所有的事情都很正常。有什么建议吗C:从双链接列表中仅删除一个节点,c,doubly-linked-list,C,Doubly Linked List,我正在编写一个程序,创建一个双链接列表并从中删除某个元素。除了列表中只有一个元素以及当我试图删除它时,程序崩溃的部分之外,所有的事情都很正常。有什么建议吗 #include <stdio.h> #include <stdlib.h> struct Node{ int data; struct Node* next; struct Node* prev; }; struct Node* head; struct Node* tail; struc
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
struct Node* prev;
};
struct Node* head;
struct Node* tail;
struct Node* CreateNewNode(int x){
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=x;
newNode->next=NULL;
newNode->prev=NULL;
return newNode;
}
void InsertAtBegin(int x){
struct Node* newNode=CreateNewNode(x);
if(head == NULL) {
head = newNode;
tail = newNode;
return;
}
head->prev = newNode;
newNode->next = head;
head = newNode;
}
void InsertAtEnd(int x){
struct Node* newNode=CreateNewNode(x);
if(tail==NULL){
head=newNode;
tail=newNode;
return;
}
tail->next=newNode;
newNode->prev=tail;
tail=newNode;
}
struct Node* PointTo(int k){
struct Node* curr=head;
int i=1;
while(curr!=NULL && i<k){
curr=curr->next;
i++;
}
return curr;
}
void DelFromList(int k){
struct Node* temp;
temp=PointTo(k);
if(k==1){
head=temp->next;
temp->next->prev=NULL;
free(temp);
return;
}
if(temp->next==NULL){
temp->prev->next=NULL;
tail=temp->prev;
free(temp);
return;
}
if(temp->next==NULL && temp->prev==NULL){
head=NULL;
tail=NULL;
printf("atpazista\n");
free(temp);
return;
}
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
free(temp);
}
void Print(){
struct Node* temp = head;
if(head==NULL){
printf("List empty\n");
return;
}
printf("List: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
void Read(int *x){
while(scanf("%d", x)==0){
printf("NUMBERS ONLY\n");
scanf("%s");
}
}
void Free(){
struct Node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
free(temp->prev);
}
}
int main()
{
int n=0, x;
printf("Number of elements?\n");
while(n<=0){
Read(&n);
}
int i;
for(i=0; i<n; i++){
printf("Type a number\n");
Read(&x);
InsertAtEnd(x);
}
Print();
printf("Head: %d\n", head->data);
printf("Tail: %d\n", tail->data);
printf("Number of element to be deleted?\n");
n=0;
while(n<=0){
Read(&n);
}
DelFromList(n);
Print();
printf("Head: %d\n", head->data);
printf("Tail: %d\n", tail->data);
Free();
return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
结构节点*prev;
};
结构节点*头部;
结构节点*尾部;
结构节点*CreateNewNode(int x){
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
newNode->data=x;
newNode->next=NULL;
newNode->prev=NULL;
返回newNode;
}
void insertbegin(int x){
结构节点*newNode=CreateNewNode(x);
if(head==NULL){
头=新节点;
tail=newNode;
返回;
}
head->prev=newNode;
新建节点->下一步=头部;
头=新节点;
}
void insertaden(int x){
结构节点*newNode=CreateNewNode(x);
if(tail==NULL){
头=新节点;
tail=newNode;
返回;
}
tail->next=newNode;
newNode->prev=尾部;
tail=newNode;
}
结构节点*指向(int k){
结构节点*curr=head;
int i=1;
while(curr!=NULL&&inxt;
i++;
}
返回货币;
}
void DelFromList(int k){
结构节点*temp;
温度=指向点(k);
如果(k==1){
头=温度->下一步;
临时->下一步->上一步=空;
免费(临时);
返回;
}
如果(临时->下一步==NULL){
temp->prev->next=NULL;
尾部=温度->上一个;
免费(临时);
返回;
}
如果(临时->下一个==NULL&&temp->上一个==NULL){
head=NULL;
tail=NULL;
printf(“atpazista\n”);
免费(临时);
返回;
}
临时->下一步->上一步=临时->上一步;
临时->上一个->下一个=临时->下一个;
免费(临时);
}
作废打印(){
结构节点*温度=头部;
if(head==NULL){
printf(“列表为空\n”);
返回;
}
printf(“列表:”);
while(temp!=NULL){
printf(“%d”,临时->数据);
温度=温度->下一步;
}
printf(“\n”);
}
无效读取(int*x){
而(scanf(“%d”,x)==0){
printf(“仅限数字”);
scanf(“%s”);
}
}
无空(){
结构节点*温度=头部;
while(临时->下一步!=NULL){
温度=温度->下一步;
自由(临时->上一个);
}
}
int main()
{
int n=0,x;
printf(“元素数?\n”);
而(水田),;
printf(“要删除的元素数?\n”);
n=0;
而(水田),;
printf(“尾部:%d\n”,尾部->数据);
自由();
返回0;
}
也许可以交换以下内容的顺序:
if(temp->next==NULL){
temp->prev->next=NULL;
tail=temp->prev;
free(temp);
return;
}
if(temp->next==NULL && temp->prev==NULL){
head=NULL;
tail=NULL;
printf("atpazista\n");
free(temp);
return;
}
后一个代码将永远不会执行,因为第一个案例已经执行并返回