2016-10-29 56 views
-1

我想學習如何在數組中使用鏈接列表,我覺得我理解這個概念,但我不能讓我的程序運行... 任何幫助,將不勝感激!我想學習如何使用鏈接列表

#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; 
} 
+1

您需要使用箭頭運算符' - >使用像'start'指針時,'。您還應該在某處使用換行符結束printf等打印操作,否則會擔心分隔數字。 –

+0

'while(start!= 0){printf(「%f」,start-> val);開始=開始 - >下; }' – BLUEPIXY

回答

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; 
} 
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; 
    }