我写了以下代码,但它在执行 create(( 函数后停止工作。我想删除从头节点开始的备用元素。我的 delete_Alt(( 函数正确吗?请告诉我我错在哪里。
#include <stdio.h>
#include <stdlib.h>
// using a structure
typedef struct mynode {
int data;
struct mynode *prev; // to point to previous node
struct mynode *link; // to point to next node
} node;
node *head = NULL;
// creating the list
void create() {
node *p, *q;
int ch;
do {
p = (node *)malloc(sizeof(node));
printf("enter datan");
scanf("%d", &p->data);
if (head == NULL)
{
p->prev = head;
q = p;
}
else
{
p->prev = q;
p->link = NULL;
q->link = p;
q = p;
}
printf("create another node?, press 1 ");
scanf ("%d",&ch);
} while(ch==1);
}
//to delete alternate elements
void delete_Alt() {
if (head == NULL)
printf("Empty list...ERROR");
node *previous, *current, *next;
previous = head;
current = head->link;
while (previous !=NULL && current != NULL) {
previous->prev = current->prev;
previous->link = current->link;
next = current->link;
previous->link = next;
next->prev = previous;
free(current);
}
}
// print the list
void display() {
node *temp;
temp = head;
while (temp != NULL) {
printf("%d ",temp->data);
temp = temp->link;
}
printf("n");
}
int main() {
node *head = NULL;
create();
printf("List before deleting is: ");
display();
delete_Alt();
printf("List after deleting is: ");
display();
return 0;
}
您在创建和删除功能中犯了一些小错误......
这是更新的代码尝试一下...
#include <stdio.h>
#include <stdlib.h>
// using a structure
typedef struct mynode {
int data;
struct mynode *prev; // to point to previous node
struct mynode *link; // to point to next node
} node;
node *head = NULL;
// creating the list
void create() {
node *p, *q;
int ch;
do {
p = (node *)malloc(sizeof(node));
printf("enter datan");
scanf("%d", &p->data);
p->link = NULL;
if (head == NULL)
{
p->prev = NULL;
head = p;
}
else
{
q = head;
while (q->link != NULL)
q = q->link;
p->prev = q;
q->link = p;
}
printf("create another node?, press 1 ");
scanf ("%d",&ch);
} while(ch==1);
}
//to delete alternate elements
void delete_Alt() {
if (head == NULL)
printf("Empty list...ERROR");
node *previous, *current, *next;
previous = head;
current = head->link;
while (previous !=NULL && current != NULL)
{
previous->link = current->link;
next = current->link;
free(current);
if(next)
{
next->prev = previous;
current = next->link;
}
else
current = NULL;
previous = next;
}
}
// print the list
void display() {
node *temp;
temp = head;
while (temp != NULL) {
printf("%d ",temp->data);
temp = temp->link;
}
printf("n");
}
int main() {
node *head = NULL;
create();
printf("List before deleting is: ");
display();
delete_Alt();
printf("List after deleting is: ");
display();
return 0;
}
在您的程序中,您只为 head 分配了一次值:
node *head = NULL;
然后它的值不会改变。
您从未将"head"分配给列表中第一个创建的元素。因此,它始终为空。试试这个:
if (head == NULL)
{
p->prev = head;
head = p;
q = p;
}
在您的delete_alt中,您需要执行以下操作:
while (previous !=NULL && current != NULL) {
previous->link = current->link;
next = current->link;
free(current);
if(next) {
next->prev = previous;
current = next->link;
}
else current = NULL;
previous = next;
}
在这里尝试一下:https://repl.it/HK2P/0