大话数据结构——线性表-链式存储之头插法创建链表

 1 #include<iostream>

 2 

 3 #include<time.h>

 4 #include <stdlib.h>

 5 

 6 using namespace std;

 7 

 8 #define OK 1

 9 #define TRUE 1

10 #define FALSE 0

11 #define ERROR 0

12 

13 typedef int status;//返回的状态值

14 typedef int elemtype;//节点里数据的类型

15 

16 //数据结构

17 typedef struct Node

18 {

19     elemtype data;//数据

20     Node *next;//指向下一节点的指针

21 }Node;

22 typedef struct Node *sqlist;

23 

24 //单链表的整表创建,头插法

25 status GreatListHead(sqlist *L,int n)

26 {

27     srand((unsigned)time(NULL));

28     sqlist p;

29     (*L)=(sqlist)malloc(sizeof(Node));

30     (*L)->next=NULL;

31     for(int i=0;i<n;++i)

32     {

33         p=(sqlist)malloc(sizeof(Node));

34         p->data=rand()%10+1;

35         p->next=(*L)->next;

36         (*L)->next=p;

37     }

38     return 1;

39 }

40 

41 

42 int main()

43 {

44     sqlist L,P;

45     GreatListHead( &L,10);

46     P=L;

47     for(int i=0;i<10;i++)

48     {

49         cout<<P->next->data<<' ';

50         P=P->next;

51     }

52     cout<<endl;

53 

54     system("pause");

55     return 1;

56 

57 }

 

你可能感兴趣的:(数据结构)