微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这是在双向链表中的给定位置插入数据的好方法吗?

如何解决这是在双向链表中的给定位置插入数据的好方法吗?

这个 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 ?

另一个错误:

如果函数 InsertAtpos<= 0 的情况下被调用,该函数会静默返回,即当 pos 太高时不会像 done 那样打印错误。

又一个错误:

在无法插入新元素的情况下,即 pos <= 0pos > length-of-list,您会泄漏内存。您已经使用 malloc 来获取一个新节点,但是当插入没有发生时,内存“丢失”了。在知道您是否需要新节点之前不要malloc

除此之外:

  1. 使用全局 Head

    真是个坏主意
  2. 一个可能失败(插入新节点)的函数应该有一个返回值来指示成功/失败

  3. 很奇怪 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?