【发布时间】:2014-02-28 15:23:04
【问题描述】:
我是链表的初学者。我有一种情况,在终端获取链接的大小,然后读取要保存在频率中的所有数据(在我的代码中,它是“频率”,但通常称为数据/信息),并使用它们创建一个链表.
到目前为止我所做的在下面的代码中显示,它只是读取要创建的 LL 的大小并为每个输入的数据创建节点。现在我必须如何链接这些节点,以使元素首先指向其他元素,最后一个元素将具有 NULL。现在我在每个创建的节点的下一个都有 NULL。
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include <string.h>
struct node
{
int freq;
struct node *next;
};
typedef struct node node;
node *tree=NULL;
main()
{
int size,data;
printf("enter the size of node\n");
scanf("%d", &size);
printf("start entering the number of elements until your size\n");
node *prev;
node *temp;
prev = NULL;
do
{
scanf("%d\n", &data);
temp = (node*)malloc(sizeof(node));
temp->freq=data;
temp->next=NULL;
if (prev)
prev->next = temp;
else
tree = temp;
prev = temp;
size--;
}
while(size>0);
node *temp1;
temp1=temp;
while(temp1->next!=NULL)
{
printf("%d-> ",temp->freq);
temp1=temp1->next;
}
}
Que(1):我尝试链接这些在终端获取的节点,但它仍然不打印遍历的链表。问题出在哪里?
The output is:
hp@ubuntu:~/Desktop/Internship_Xav/Huf_pointer$ ./ll
enter the size of node
4
start entering the number of elements until your size
22
11
4
5
6//It don't print the linked list here
hp@ubuntu:~/Desktop/Internship_Xav/Huf_pointer$
【问题讨论】:
标签: c algorithm data-structures linked-list nodes