Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我的链接列表程序的改进_C - Fatal编程技术网

C 我的链接列表程序的改进

C 我的链接列表程序的改进,c,C,这是一个正在运行的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next, *prev; }; struct node *root = NULL; void push(int); void pop(void); struct node *create_node(int); void travel(void); int main() { i

这是一个正在运行的程序

#include <stdio.h>
#include <stdlib.h>
struct node {
    int data;
    struct node *next, *prev;
};
struct node *root = NULL;
void push(int);
void pop(void);
struct node *create_node(int);
void travel(void);
int main()
{
    int i, j, choice, count;
    printf("enter choice\n");
    scanf("%d", &choice);
    count = 0;
    while (choice == 1) {
        printf("enter a data element");
        scanf("%d", &j);
        if (count == 0) {
            root = (struct node *)malloc(sizeof(struct node));
            root->next = NULL;
            root->data = j;
        } else
            push(j);
        count++;
        printf("enter choice\n");
        scanf("%d", &choice);
    }
    printf("the link list is \n");
//travel function to be created
    travel();
}

void push(int data)
{
    struct node *t1;
    t1 = root;
    while (t1->next != NULL) {
        t1 = t1->next;
    }
    t1->next = create_node(data);
}

void pop()
{
}

void travel(void)
{
    struct node *t1;
    t1 = root;
    while (t1->next != NULL) {
        printf("%d ", t1->data);
        t1 = t1->next;
    }
    printf("%d ", t1->data);
}

struct node *create_node(int data)
{
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->data = data;
    p->next = NULL;
    p->prev = NULL;
    return p;
}
#包括
#包括
结构节点{
int数据;
结构节点*next,*prev;
};
结构节点*root=NULL;
无效推力(int);
void-pop(void);
结构节点*创建节点(int);
无效旅行(无效);
int main()
{
int i,j,选择,计数;
printf(“输入选项”);
scanf(“%d”,选择(&C);
计数=0;
while(选项==1){
printf(“输入数据元素”);
scanf(“%d”和“&j”);
如果(计数=0){
root=(结构节点*)malloc(sizeof(结构节点));
root->next=NULL;
根->数据=j;
}否则
推(j);
计数++;
printf(“输入选项”);
scanf(“%d”,选择(&C);
}
printf(“链接列表是\n”);
//要创建的旅行功能
旅行();
}
无效推送(整型数据)
{
结构节点*t1;
t1=根;
while(t1->next!=NULL){
t1=t1->next;
}
t1->next=创建_节点(数据);
}
void pop()
{
}
无效旅行(无效)
{
结构节点*t1;
t1=根;
while(t1->next!=NULL){
printf(“%d”,t1->data);
t1=t1->next;
}
printf(“%d”,t1->data);
}
结构节点*创建节点(int数据)
{
结构节点*p=(结构节点*)malloc(sizeof(结构节点));
p->data=数据;
p->next=NULL;
p->prev=NULL;
返回p;
}
上面的程序是完全工作的,我使用了全局指针根。 我的问题是,如果我不想在这里使用全局指针根,那么如何维护 因为每次我都必须在push-pop函数中返回列表的根
有没有其他方法可以达到同样的效果?

最简单的方法是将指向根节点的指针传递给每个函数:

void push(struct node **root, int data) { ... }
void pop(struct node **root) { ... }
void travel(struct node *root) { ... }
因此,在main函数中,可以声明一个局部变量来保存根指针:

struct node *root = NULL;
然后,当您调用
push
时,例如,您传递根poiner的地址:

push(&root, data);
我强烈建议您修复
push
travel
函数,以便它们对根指针为
NULL
的情况具有鲁棒性。这是在你之前的问题中讨论过的,你应该听从建议

如果您这样做了,那么您就可以摆脱
count
为零的测试以及相关的特殊情况代码。然后,您将替换此:

if (count == 0) {
    root = (struct node *)malloc(sizeof(struct node));
    root->next = NULL;
    root->data = j;
} else
    push(&root, j);
为此:

push(&root, j);
要将消息带回家,新的
推送将如下所示:

void push(struct node **root, int data)
{
    if (*root == NULL)
        *root = create_node(data);
    else
    {
        struct node *last = *root;
        while (last->next != NULL) {
            last = last->next;
        }
        last->next = create_node(data);
    }
}
您还需要修改
travel
,以检查
根节点是否为
NULL
。我将把它作为练习留给你

维护头指针和尾指针可能是一种更好的方法,因为这样可以避免太多的列表遍历