所以我正在创建一个包含3个节点的链表,现在我正在尝试实现一个在C中显示列表的函数,这里是代码:
struct node *createList(struct node *start)
{
int i, n, data;
printf("Enter the number of nodes : ");
scanf("%d", &n);
if (n==0)
return start;
printf("Enter the first element to be inserted : ");
scanf("%d", &data);
printf("%d",data);
start=insertInBeginning(start, data);
for(i=2; i<=n; i++)
{
printf("Enter the next element to be inserted : ");
scanf("%d", &data);
insertAtEnd(start, data);
}
return start;
}
// end of createList
struct node *insertInBeginning(struct node *start, int data)
{
printf("we in begining \n");
struct node *new_node,*current;
printf("declared nodes");
new_node = &data;
printf("got new_node data \n");
if(start == NULL) {
printf("start is null! \n");
start = new_node;
current = new_node;
} else {
printf("start isn't null! \n");
new_node->link=start;
start=new_node;
}
}
//end of insertInBeginning
void insertAtEnd(struct node *start, int data)
{
struct node *new_node,*current,*temp;
new_node->info = data;
if(start == NULL)
{
start= new_node;
current= new_node;
} else
{
temp = start;
while(temp->link!=NULL) {
temp = temp->link;
}
temp -> link = new_node;
}
}
// end of insertAtEnd
void displayList(struct node *start)
{
printf("we in displaylist");
struct node *temp;
temp = start->link;
printf("temp has start values?");
if(temp = NULL)
{
printf("we null now \n");
} else {
printf("we NOT null \n" );
while(temp!=NULL)
{
printf("%d \n",temp->info);
temp = temp->link;
}
}
}
我遇到的问题是显示函数中的循环完成但是它没有打印任何值,但我知道temp不是NULL所以它显示的东西,只是不是我想要的,是无论如何要解决这个问题?
答案 0 :(得分:1)
这里有两个大问题:首先,(正如Martin James指出的那样),你没有从insertInBeginning()例程返回,所以start设置为某个未定义的值。您还遇到一个巨大的问题,即您只是将int data
投射到node *
。您必须为新节点安装malloc()内存并将数据写入其中。 (然后返回它)你在insertAtEnd()例程中遇到了同样的问题 - 你必须使用malloc()内存来保存新节点。
static struct node *
insertAtStart (struct node *start, int data)
{
struct node *new;
new = malloc(sizeof(*new));
if (new == NULL) {
perror("malloc");
exit(1);
}
new->value = data;
new->next = start;
return new;
}
static struct node *
insertAtEnd (struct node *start, int data)
{
struct node *cur, *new;
// create the node * we will be adding. (This really ought to have been done
// in the calling routine, so the same code would be used to create the node
// entry and to store the value. Note a big issue with this simple a struct,
// but more complex structs really ought to only be created in one place, so
// when they change, you won't have bugs where you forget to change one of
// the creation locations)
new = malloc(sizeof(*new));
if (new == NULL) {
perror("malloc");
exit(1);
}
new->value = data;
new->link = NULL;
// if this is going to be the only entry in the list, just return that it
// is the new start
if (start == NULL)
return new;
// find the last entry
for (cur = start ; cur->link != NULL ; cur = cur->link)
;
cur->link = new;
return start;
}
你真的想打开编译器警告 - 它可以节省大量时间让编译器告诉你这些错误。如果您正在使用gcc,请在编译命令中添加-Wall -Werror(如果您使用的是其他编译器,则需要阅读手册页。)
(还有其他gcc警告可以打开,但是-Wall会为你提供很多有用的警告。你可以阅读手册页找到你想要的所有内容,如果你想打开更多)
答案 1 :(得分:-1)
如上所述,插入功能有一些麻烦。
此代码声明结构node
一次:
#include<iostream>
#include<fstream>
using namespace std;
struct node{int info; node* next; node(int a=0, node* b=0){info=a; next=b;}}; // field1->value field2->next node
此“声明”保证创建的节点具有已定义的值。 创建具有暗淡值的简单列表的函数:
node* build_L(int dim)
{
if(dim)
{int x; cin>>x; return new node(x,build_L(dim-1));}
return 0;
}
打印:
void pr_L(node*L)
{
if(L)
{cout<<L->info<<' '; pr_L(L->next);}
else
cout<<endl;
}
在开头添加节点:
node*insB(node*L,int k)
{
node* n=new node(k,L);
return n;
}
最后:
node* insE(node*L, int k)
{
node*temp=L;
while(L->next)
{
L=L->next;
}
node*e=new node(k,0);
L->next=e;
L=temp;
return L;
}
在main
中,您可以调用函数:
main()
{
int dim_list;//set the number of nodes
cin>>dim_list;
//or scanf()
node*L=build_L(dim_list);
cout<<"List built"<<endl;
//or printf()
pr_list(L);
int k,f; cin>>k;
L=insB(L,k);
cout<<"node in first position: "<<endl;
pr_L(L);
cin>>f;
L=insE(L,f);
cout<<"node in queue: "<<endl;
pr_L(L);
}