c语言--随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。

[复制链接]
查看11 | 回复0 | 2010-4-6 16:30:26 | 显示全部楼层 |阅读模式
#include #include #define N 8
//这个链表有8个元素typedef struct list{int data; struct list *next;} SLIST;
//定义链表的结构体SLIST *creatlist(int *a)
//建立链表{SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; idata=a; p->next=q; p=q; } p->next=0; return h;}void outlist(SLIST *h)
//输出链表{SLIST *p; p=h->next; if (p==NULL) printf("\nThe list is NULL!\n"); else {printf("\nHead");
do {printf("->%d",p->data); p=p->next;} while(p!=NULL);
printf("->End\n");}}void main(){SLIST *head; int a[N];
int i;
for(i=0;idata));}else{ q->data=rand();}q->next=L->next;L->next=q; }}void LinkListDump(LinkList L){ Node *p=L->next; while(p) {printf("%d\t",p->data);p=p->next; } printf("\n");}int main(){ int len; printf("Please enter the number of the elements:\n"); scanf("%d",&len); int flag=0;//if flag==1,you will input the elements,else by random; LinkList L; L=(Node*)malloc(sizeof(Node));//with the head node; L->next=NULL; CreateLinkList(L,flag,len); LinkListDump(L); return 0;}你看这样行不行?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行