c描述双向链表删除结
运行不出来。我的删除部分有2个函数,一个是把要删除的数据取出,一个是删除它。请教各位!能判断是代码末尾的那个函数有问题#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node * prior;
struct node * next;
};
struct node * in_link(void); //创建一个双向链表
void print_link(struct node * head);
struct node * delete_link(struct node * head, int i); //删除链表结点
struct node * get(struct node * head, int i);
int main(void)
{
int i, y;
struct node * head;
head = in_link();
print_link(head);
printf("你想删除数据在链表的第几个数据: ");
scanf("%d", &i);
head = delete_link(head, i);
print_link(head);
return 0;
}
struct node * in_link(void)
{
struct node *head, *temp, *add_temp;
int n, i;
printf("您想插入几个数据: ");
scanf("%d", &n);
head = (struct node *) malloc(sizeof(struct node));
if (head == NULL) {
printf("分配内存失败\n");
exit (EXIT_FAILURE);
}
printf("请输入链表的第1个数据: ");
scanf("%d", &head->value);
head->next = NULL;
temp = head;
for (i = 2;i <= n;i++) {
add_temp = (struct node *) malloc(sizeof(struct node)); //动态分配内存空间
if (add_temp == NULL) {
printf("内存分配失败\n");
exit (EXIT_FAILURE);
}
printf("请输入链表的第%d个数据: ", i);
scanf("%d", &add_temp->value);
add_temp->next = NULL;
temp->next = add_temp;
temp = temp->next;
}
return head;
}
void print_link(struct node * head)
{
while (head != NULL) {
printf("%d ", head->value);
head = head->next;
}
printf("\n");
}
struct node * get(struct node * head, int i) //将你想删除的数据取出
{
struct node * temp;
int j = 1;
temp = head;
while (temp != NULL && j < i) {
temp = temp->next;
j++;
}
printf("%d\n", temp->value);
return temp;
}
struct node * delete_link(struct node * head, int i) //删除取出的数据,调用了get函数
{
struct node * temp;
temp = get(head, i);
temp->prior->next = temp->next;
temp->next->prior = temp->prior;
free(temp);
return head;
}