【发布时间】:2015-08-20 14:07:51
【问题描述】:
我正在尝试实现一个链表。但不幸的是它不起作用。我尝试更改代码。它不起作用。插入函数不起作用,当我调用 displaylist() 函数时我什么也没看到。请帮帮我。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node {
int key;
struct node *next;
} node;
struct node *head, *z, *t;
listinit(void)
{
head = (struct node *) malloc(sizeof *head);
z = (struct node *) malloc(sizeof *z);
head->next = z;
z->next = z;
}
delnext(struct node *t)
{
t->next = t->next->next;
}
node *insertafter(int v, struct node *t)
{
struct node *x;
x = (struct node *)malloc(sizeof *x);
x->key = v;
x->next = t->next;
t->next = x;
return x;
};
void displaylist(void)
{
node *curr = head->next;
while(curr != z){
printf("%d -> ", curr->key);
curr = curr->next;
}
printf("\nHappy Coding! :D\n\n");
}
int main(void)
{
listinit();
int cmd = 0,val = 0;
printf("MENU: \n"
"1. INSERT\n"
"2. DELETE\n"
"3. DISPLAY\n");
printf("OPTION> ");
scanf("%d",&cmd);
switch(cmd){
case 1:
printf("Please Enter your Key Value >");
scanf("%d",&val);
insertafter(val, &head);
main();
case 2:
main();
case 3:
displaylist();
main();
}
}
【问题讨论】:
标签: c linked-list