我正在尝试学习如何在数组位置使用链接列表,我觉得我理解这个概念,但我不能运行我的程序......任何帮助都将不胜感激!
#include <stdio.h>
#include <stdlib.h>
struct linkList{
float val;
struct linkList *next;
};
int main()
{
struct linkList n1,n2,n3,*start;
n1.val=5.5;
n2.val=6.6;
n3.val=7.7;
start=&n1;
n1.next=&n2;
n2.next=&n3;
n3.next=0;
while(start.next!=0){
printf("%f",start.val);
start=start.next;
}
return 0;
}
答案 0 :(得分:0)
您的问题已解决。请参阅以下C代码段:
#include <stdio.h>
#include <stdlib.h>
struct linkList
{
float val;
struct linkList *next;
};
int main(void)
{
struct linkList n1, n2, n3, *start;
n1.val = 5.5;
n2.val = 6.6;
n3.val = 7.7;
start = &n1;
n1.next = &n2;
n2.next = &n3;
n3.next = 0;
while(start->next != NULL)
{
printf("%f \n",start->val);
start=start->next;
}
return 0;
}
答案 1 :(得分:0)
试试这个:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList() {
struct node *ptr = head;
printf("\n[ ");
//start from the beginning
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
return 0;
}