这是在双向链表中的给定位置插入数据的相当好的方法吗?
Is this considerably good way to insert data at given position in doubly Linked List?
这个 InsertAt() 函数可以用更好的方式完成吗?
我需要很好的计算机编程指南,可以帮助我提高我的编码技能
1对1方式。我写这行是因为我的问题因为太少而没有提交
细节,因为我无话可说。 这在 Whosebug 中是个小麻烦
代码
#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,e,d,c,b,a,
k,f,e,d,k,c,b,a,
Data OverIndexed at 12 .
k,f,e,d,k,c,b,a,
k,k,f,e,d,k,c,b,a,
Data OverIndexed at 20 .
Can this InsertAt() function thing be done in more better way?
是的,可以。您的代码中有几个错误。
试试这个:
int main()
{
Head = NULL;
InsertAt(1);
Print();
}
它崩溃了吗?
您在此处取消引用 Head
而不检查 NULL:
if (pos == 1)
{
Head->prev = container; <--- what if Head is NULL ?
另一个错误:
如果函数 InsertAt
在 pos
为 <= 0
时被调用,则函数静默 returns,即当 pos
时没有像 done 那样打印错误太高了。
还有一个错误:
在无法插入新元素的情况下,即 pos <= 0
或 pos > length-of-list
,您会泄漏内存。您已经使用 malloc
来获取一个新节点,但是当插入没有发生时,内存就会“丢失”。不要malloc
在知道你是否需要新节点之前。
除此之外:
使用全局变量真是个坏主意Head
可能会失败(插入新节点)的函数应该有一个 return 值来指示 succes/failure
很奇怪 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, 2, 'c')) puts("InsertAt failed");
if (InsertAt(&Head, 1, 'a')) puts("InsertAt failed");
这个 InsertAt() 函数可以用更好的方式完成吗?
我需要很好的计算机编程指南,可以帮助我提高我的编码技能 1对1方式。我写这行是因为我的问题因为太少而没有提交 细节,因为我无话可说。 这在 Whosebug 中是个小麻烦
代码
#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,e,d,c,b,a,
k,f,e,d,k,c,b,a,
Data OverIndexed at 12 .
k,f,e,d,k,c,b,a,
k,k,f,e,d,k,c,b,a,
Data OverIndexed at 20 .
Can this InsertAt() function thing be done in more better way?
是的,可以。您的代码中有几个错误。
试试这个:
int main()
{
Head = NULL;
InsertAt(1);
Print();
}
它崩溃了吗?
您在此处取消引用 Head
而不检查 NULL:
if (pos == 1)
{
Head->prev = container; <--- what if Head is NULL ?
另一个错误:
如果函数 InsertAt
在 pos
为 <= 0
时被调用,则函数静默 returns,即当 pos
时没有像 done 那样打印错误太高了。
还有一个错误:
在无法插入新元素的情况下,即 pos <= 0
或 pos > length-of-list
,您会泄漏内存。您已经使用 malloc
来获取一个新节点,但是当插入没有发生时,内存就会“丢失”。不要malloc
在知道你是否需要新节点之前。
除此之外:
使用全局变量真是个坏主意
Head
可能会失败(插入新节点)的函数应该有一个 return 值来指示 succes/failure
很奇怪
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, 2, 'c')) puts("InsertAt failed");
if (InsertAt(&Head, 1, 'a')) puts("InsertAt failed");