如何解决这是在双向链表中的给定位置插入数据的好方法吗?
这个 InsertAt() 函数可以用更好的方式完成吗?
我需要良好的计算机编程指南,他们可以帮助我提高我的编程技能 1对1方式。我写这行是因为我的问题因为太少而没有被提交 细节,因为我无话可说。 这是 StackOverflow 中的小烦恼
代码
#include <stdio.h>
#include <stdlib.h>
struct Node *Head;
struct Node
{
char data;
struct Node *prev;
struct Node *next;
};
//this function inserts data at head.
void Insert(char data)
{
struct Node *tempHead = (struct Node *)malloc(sizeof(struct Node));
tempHead->data = data;
tempHead->next = Head;
tempHead->prev = NULL;
if (Head != NULL)
Head->prev = tempHead;
Head = tempHead;
}
void Print()
{
struct Node *tempHead = Head;
while (tempHead != NULL)
{
printf("%c,",tempHead->data);
tempHead = tempHead->next;
}
printf("\n");
}
// this function inserts at given pos .
void InsertAt(int pos)
{
struct Node *tempHead = Head;
struct Node *container = (struct Node *)malloc(sizeof(struct Node));
container->data = 'k';
if (pos == 1)
{
Head->prev = container;
container->prev = NULL;
container->next = Head;
Head = container;
return;
}
else
{
for (int i = 1; i < pos; i++)
{
if (tempHead->next == NULL && i != (pos - 1))
{
printf("Data OverIndexed at %d .\n",pos);
break;
}
else if (tempHead->next == NULL && i == pos - 1)
{
tempHead->next = container;
container->prev = tempHead;
container->next = NULL;
break;
}
else if (i == pos - 1)
{
struct Node *temp1 = tempHead->next;
tempHead->next = container;
container->prev = tempHead;
container->next = temp1;
temp1->prev = container;
break;
}
tempHead = tempHead->next;
}
}
}
int main()
{
Head = NULL;
Insert('a');
Insert('b');
Insert('c');
Insert('d');
Insert('e');
Insert('f');
Print();
InsertAt(1);
Print();
InsertAt(5);
Print();
InsertAt(12);
Print();
InsertAt(2);
Print();
InsertAt(20);
}
输出
f,e,d,c,b,a,k,f,Data OverIndexed at 12 .
k,Data OverIndexed at 20 .
解决方法
这个 InsertAt() 函数可以用更好的方式完成吗?
是的,可以。您的代码中有几个错误。
试试这个:
int main()
{
Head = NULL;
InsertAt(1);
Print();
}
它崩溃了吗?
您在此处取消引用 Head
而不检查 NULL:
if (pos == 1)
{
Head->prev = container; <--- what if Head is NULL ?
另一个错误:
如果函数 InsertAt
在 pos
为 <= 0
的情况下被调用,该函数会静默返回,即当 pos
太高时不会像 done 那样打印错误。
又一个错误:
在无法插入新元素的情况下,即 pos <= 0
或 pos > length-of-list
,您会泄漏内存。您已经使用 malloc
来获取一个新节点,但是当插入没有发生时,内存“丢失”了。在知道您是否需要新节点之前不要malloc
。
除此之外:
-
使用全局
真是个坏主意Head
-
一个可能失败(插入新节点)的函数应该有一个返回值来指示成功/失败
-
很奇怪
InsertAt
不接受data
值(如Insert
)。
我会这样做:
int InsertAt(struct Node **pHead,int pos,char data)
{
if (pos <= 0) return -1; // Illegal pos
if (pos == 1)
{
// pos == 1 is always valid
struct Node *container = malloc(sizeof *container);
if (container == NULL) exit(1);
container->data = data;
container->prev = NULL;
container->next = *pHead;
if (container->next != NULL) container->next->prev = container;
*pHead = container; // Change Head
return 0;
}
if (*pHead == NULL) return -1; // Illegal pos
struct Node *tempHead = *pHead;
for (int i = 1; i < (pos-1); i++) // Notice pos-1
{
tempHead = tempHead->next;
if (tempHead == NULL) return -1; // Illegal pos
}
// Insert after tempHead
struct Node *container = malloc(sizeof *container);
if (container == NULL) exit(1);
container->data = data;
container->prev = tempHead;
container->next = tempHead->next;
tempHead->next = container;
if (container->next != NULL) container->next->prev = container;
return 0;
}
然后调用:
struct Node *Head = NULL;
if (InsertAt(&Head,1,'b')) puts("InsertAt failed");
if (InsertAt(&Head,2,'d')) puts("InsertAt failed");
if (InsertAt(&Head,'c')) puts("InsertAt failed");
if (InsertAt(&Head,'a')) puts("InsertAt failed");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。