基本上插入中间工作,但是当我想插入一个值作为第一个(在开始时)它插入但我还会显示一个地址(列表头的数据部分)。
为了使listhead指向新元素,我需要修改什么?
#include <stdio.h>
#include <conio.h>
struct entry {
int value;
struct entry *next;
};
//X position struct u want to target
struct entry *insertentry(struct entry *new_entry, struct entry *x){
int data;
printf("Enter a value for your new entry\n");
scanf("%i",&data);
new_entry->value=data;
new_entry->next=x->next;
x->next=&(*new_entry);
return new_entry;
};
int main (void)
{
struct entry n1, n2, n3,*n4,n5;
struct entry list_head, *list_pointer=&list_head;
list_head.next=&n1;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n4=insertentry(&n5,&list_head);
n3.next = (struct entry *) 0; // Mark list end with null pointer
while ( list_pointer != (struct entry *) 0 ) {
printf ("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
printf("%i\n",n5.value);
getch();
return 0;
}
答案 0 :(得分:0)
出于某种原因,我没有conio.h所以我在这里删除它。您错过了将变量正确设置为col-sm-6
。
此代码应该:
list_pointer=&n1;
答案 1 :(得分:0)
它不显示地址,而是显示list_head.next的未初始化值
int main (void)
{
struct entry n1, n2, n3,*n4,n5;
struct entry list_head, *list_pointer=list_head.next;
list_pointer.value = 0; // another way without discarding list_pointer content
list_head.next=&n1;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
...
}
答案 2 :(得分:0)
现在它运行正常,但我已经手动完成了问题,&#34;如何在它之前插入节点的功能&#34;看起来像
#include <stdio.h>
#include <conio.h>
struct entry {
int value;
struct entry *next;
};
//X struct u want to target
struct entry insertentry(struct entry *new_entry, struct entry *x){
int data;
printf("Enter a value for your new entry");
scanf("%i",&data);
new_entry->value=data;
new_entry->next=x->next;
x->next=&(*new_entry);
return *new_entry;};
int main (void)
{struct entry n1, n2, n3,n4,n5,list_head;
struct entry *list_pointer;
n1.value = 100;
list_head.next=&n1;
n4=insertentry(&n5,&list_head);
list_pointer=&n4;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = (struct entry *) 0; // Mark list end with null pointer
while ( list_pointer != (struct entry *) 0 ) {
printf ("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
printf("%i\n",n5.value);
getch();
return 0;
}