我正在尝试设置 head -> data =10,然后在链表的开头添加 10 个值(101-110)。
目前我得到输出:10
有人能帮我吗?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *head, emp;
int num = 10, i;
void add_beg(struct node *q, int n);
void traverse(struct node *q);
clrscr();
head = (node*)malloc(sizeof(node));
head->data = num;
head->next = NULL;
for (i= 101; i<=110; i++)
{
add_beg(head, i);
}
traverse(head);
getch();
}
void add_beg(struct node *q, int num)
{
int n = num;
struct node *temp;
temp = (node *)malloc(sizeof(node));
temp->data = n;
temp->next = q;
q = temp;
}
void traverse(struct node *q)
{
while(q!=NULL)
{
printf("%d\n",q->data);
q = q->next;
}
}