2014-08-29 80 views
-2

它是一個鏈接列表基本插入和顯示操作的代碼,但輸入參數鍵和信息插入功能程序不繼續我的意思是讓我們說我輸入4作爲關鍵和5作爲信息,應該有一個由head指向的節點,當show被調用時,我的鏈表應該顯示一個元素,但不會發生。插入和顯示功能有問題,或者我應該怎麼做?插入操作在鏈接列表代碼不起作用

#include <stdio.h> 
#include <stdlib.h> 

static struct node{ 
    int key, info; 
    struct node *next; 
}; 

static struct node *head, *z; 

initialize() 
{ 
    head = (struct node*)malloc(sizeof *head); 
    z = (struct node*)malloc(sizeof *z); 
    head->next = z; 
    z->next = z; 
    z->key = -1; 
} 

insert(int n, int info) 
{ 
    struct node *t, *x; 

    t = head; 

    while (t->next != z) { 
     t = t->next; 
    } 

    x = (struct node *)malloc (sizeof *x); 
    x->key = n; 
    x->next = t->next; 
    t->next = x; 
    x->info = info; 
} 

show() 
{ 
    struct node *t = head; 

    while (t->next != z) { 
     t = t->next; 
     printf("%d\t%d\n", t->key, t->info); 
    } 
} 

main() 
{ 
    initialize(); 
    int i, j; 

    printf("enter the number and info\n"); 
    scanf("%d%d", &i, &j); // i is key and j is info 
    insert(i, j); // passing arguments to insert function 
    show(); 
} 
+1

_progamme do not proceed_這是什麼意思?你意識到你永遠不會調用show()? – 2014-08-29 13:28:51

+1

在代碼中包含'stdlib.h'。 – user1336087 2014-08-29 13:31:27

+1

使用'-Wall'選項編譯您的代碼以查看可能的警告。 並調用'insert()'調用'show()'後查看輸出。 – user1336087 2014-08-29 14:03:11

回答

0

嘗試這種

static struct node{變化(以下稱 - >)struct node{

initialize() - >void initialize()

insert(int n, int info) - >void insert(int n, int info)

show() - >void show()

main() - >int main() //並返回0;

demo