2016-10-29 2 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", 시작 -> 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; 
    } 
관련 문제