写了个单链表在尾部追加随机数的函数,编译结果全是一样的数
程序代码:
# include <stdio.h> # include <malloc.h> # include <time.h> typedef int DataType; typedef struct Node{ // 定义单链表结点类型 DataType data; struct Node *next; } SLNode; void ListInit(SLNode **head); int Listappend(SLNode *head, int n, DataType x); void ListPrint(SLNode *head); int main(void) { SLNode *head; int i, x, n; n=10; ListInit(&head); //计算插入时间 Listappend(head,n,rand()); ListPrint(head); } //初始化 void ListInit(SLNode **head){ *head=(SLNode *)malloc(sizeof(SLNode)); (*head)->next=NULL; } //追加 int Listappend(SLNode *head, int n, DataType x){ SLNode *p,*q; p=head; for(int i=1;i<=n;i++){ q=(SLNode *)malloc(sizeof(SLNode)); q->data=x; p->next=q; p=q; } p->next=NULL; return 1; } /* 3.打印链表,链表的遍历*/ void ListPrint(SLNode *head) { if(NULL == head) //链表为空 { printf("链表为空\n"); } else { while(head != NULL) { printf("%d ",head->data); head = head->next; } printf("\n"); } }
运行结果见图:6361024 41 41 41 41 41 41 41 41 41 41
Process returned 0 (0x0) execution time : 0.062 s
Press any key to continue.
求大神帮助
[ 本帖最后由 beadrop 于 2013-11-16 11:23 编辑 ]