双链表
初学数据结构,求大佬解答。不知道为啥VS2019执行程序的时候没有说出错,但程序只运行一半就结束了#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int DataType;
typedef struct DulNode
{
DataType data;
struct DulNode* prior, * next;
}DulNode;
DulNode* Creatlist(DataType a[], int n)
{
DulNode* s = NULL;
DulNode* first = (DulNode*)malloc(sizeof(DulNode));
first->prior = NULL;
for (int i = 0; i < n; i++)
{
s = (DulNode*)malloc(sizeof(DulNode));
s->data = a;
s->next=first->next;
first->next = s;
s->prior = first;
}
return first;
}
void PrintList(DulNode* first)
{
DulNode* p = first->next;
while (p != NULL)
{
printf("%d", p->data);
p = p->next;
}
}
int Length(DulNode* first)
{
DulNode* p = first->next;
int count = 0;
while (p != NULL)
{
p = p->next;
count++;
}
return count;
}
int Locate(DulNode* first, DataType x)
{
DulNode* p = first->next;
int count = 1;
while (p != NULL)
{
if (p->data == x) return count;
p = p->next;
count++;
}
return 0;
}
int Insert(DulNode* first, int i, DataType x)
{
DulNode* s = NULL, * p = first;
int count = 0;
while (p != NULL && count < i - 1)
{
p = p->next;
count++;
}
if (p == NULL) {
printf("位置错误,插入失败\n"); return 0;
}
else {
s = (DulNode*)malloc(sizeof(DulNode));
s->data = x;
s->prior = p;
s->next = p->next;
p->next->prior = s;
p->next = s;
return 1;
}
}
int Delete(DulNode* first, int i, DataType* ptr)
{
DulNode* p = first, * q = NULL;
int count = 0;
DataType x;
while (p != NULL && count < i - 1)
{
p = p->next;
count++;
}
if (p == NULL || p->next == NULL)
{
printf("位置错误,删除失败\n"); return 0;
}
else {
q = p->next; *ptr = q->data;
p->prior->next = p->next;
p->next->prior = p->prior;
free(q); return 0;
}
}
void DestroyList(DulNode* first)
{
DulNode* p = first;
while (first != NULL)
{
first = first->next;
free(p); p = first;
}
}
int main()
{
int r = { 1,2,3,4,5 }, i, x;
DulNode* first = NULL;
first = Creatlist(r, 5);
printf("当前线性表的数据为:");
PrintList(first);
Insert(first, 2, 8);
printf("执行插入操作后数据为:");
PrintList(first);
printf("当前双链表的长度为:%d", Length(first));
printf("请输入查找的元素值:");
scanf_s("%d", &x);
i = Locate(first, x);
if (1 == i)printf("元素中%的元素位置为:%d\n", x, i);
else printf("双链表中没有元素%d\n", x);
printf("请输入要删除第几个元素:");
scanf_s("%d", &i);
if (Delete(first, i, &x) == 1) {
printf("删除的元素值是%d,执行删除操作后数据为:", x);
PrintList(first);
}
else printf("删除操作失败\n");
DestroyList(first);
return 0;
} 本帖最后由 xieglt 于 2020-9-21 11:12 编辑
首先,建立链表的函数有问题
DulNode* Creatlist(DataType a[], int n)
{
DulNode* s = NULL;
DulNode* first = (DulNode*)malloc(sizeof(DulNode));
first->prior = NULL;
//first->next 没有初始化为NULL;
first->next = NULL;
for (int i = 0; i < n; i++)
{
s = (DulNode*)malloc(sizeof(DulNode));
s->data = a;
s->next=first->next;
first->next = s;
s->prior = first;
}
return first;
}
另外,你的新节点不是建立在链表末尾,而是在第2的位置,也就是first之后的位置
建议这么建立链表,保证新节点在链表末尾
DulNode* Creatlist(DataType a[], int n)
{
DulNode* s = NULL;
DulNode* first = (DulNode*)malloc(sizeof(DulNode));
first->prior = NULL;
first->next = NULL;
DulNode * pNode = first;
for (int i = 0; i < n; i++)
{
s = (DulNode*)malloc(sizeof(DulNode));
s->data = a;
s->next= NULL;
s->prior = pNode;
pNode->next = s;
pNode = s;
}
return first;
}
删除节点也有点问题
int Delete(DulNode* first, int i, DataType* ptr)
{
DulNode* p = first, * q = NULL;
int count = 0;
DataType x;
while (p != NULL && count < i - 1)
{
p = p->next;
count++;
}
//这个逻辑有问题,p != NULL 且 p->next != NULL 时说明链表里至少还有2个节点
//也就是说,至少还有一个元素,为什么不能删除?
if (p == NULL || p->next == NULL)
{
printf("位置错误,删除失败\n"); return 0;
}
else
{
//这个逻辑错了
q = p->next;
*ptr = q->data;
//应该是 *ptr = p->data;
p->prior->next = p->next;
p->next->prior = p->prior;
//free错了,删除的节点应该是p,也就是 free(p);
free(q);
//返回值错了,按逻辑应该是1
return 1;
}
}
主函数中的问题
//这个判断有问题,应该是i > 0
if (1 == i)
//这里%后面少了个d
printf("元素中%的元素位置为:%d\n", x, i);
else
printf("双链表中没有元素%d\n", x);
页:
[1]