|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
初学数据结构,求大佬解答。不知道为啥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[i];
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[5] = { 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;
} |
-
不知为啥只执行了前面的PrintList就不继续了,也没有说出错
|