1 #include"stdio.h"
2 #include"stdlib.h"
3 #include"malloc.h"
4
5
6 const int maxlen=1000;//线性表的最大长度
7
8 //------------线性表------------------
9 struct List
10 {
11 int Data[maxlen];//存放数据
12 int CurNum;//当前线性表
13 };
14
15 void Intialize( List &A)//线性表初始化
16 {
17 A.CurNum = 0;//线性表元素个数为0
18 }
19
20 int Length(List &A)//求表长度的实现
21 {
22 return A.CurNum;
23 }
24
25 int Insert( List &A,const int i,const int x)//插入元素运算对应的函数
26 {
27 if(A.CurNum==maxlen)printf("溢出!\n");//溢出,不能插入
28 if(i<1||i>Length(A)+1)printf("插入范围有错!\n");//插入范围有错
29 else {
30 for(int j=A.CurNum-1;j>=i-1;j--){
31 A.Data[j+1]=A.Data[j];
32 }
33 A.Data[i-1]=x;
34 A.CurNum++;
35 return 0;
36 }
37 }
38 int Get_int(List &A,const int i,int &x)//按序号取元素运算
39 {
40 if(i<=0||i>A.CurNum)printf("序号错误!\n");
41 else {
42 x=A.Data[i-1];
43 return 0;
44 }
45 }
46 //------------线性表------------------
47
48
49 //-------------链表------------------
50 typedef char type;
51 typedef struct Linklist
52 {
53 type data;
54 struct Linklist *next;
55 }link_list;
56
57 /*创建链表*/
58 link_list *Create_list(link_list *head)
59 {
60 head = (link_list *)malloc(sizeof link_list);
61 if(head==NULL)
62 {
63 printf("setup fail\n");
64
65 exit(0);
66 }
67 head->data=NULL;
68 head->next=NULL;
69 return head;
70 }
71
72 /*向链表中插入一个元素*/
73 void Insert_list(link_list *head,type data)
74 {
75 link_list *q=NULL;
76 link_list *p=NULL;
77 link_list *s=NULL;
78 q=head;
79 p=q->next;
80 while(p!=NULL)
81 {
82 q=p;
83 p=q->next;
84 }
85 s=(link_list *)malloc(sizeof link_list);
86 s->data = data;
87 q->next=s;
88 s->next=p;
89 }
90
91 void browse(link_list *head)
92 {
93 link_list *q=NULL;
94 q=head->next;
95 while(q!=NULL)
96 {
97 printf("num:%c\n",q->data);
98 q=q->next;
99 }
100 }
101
102
103 /*查找元素 并返回位置*/
104 link_list *find_list(link_list *head,int data)
105 {
106 link_list *q=NULL;
107 link_list *p=NULL;
108 q=head;
109 p=q->next;
110 while(p!=NULL&&p->data!=data)
111 {
112 q=p;
113 p=q->next;
114 }
115 printf("num :%c data : %c\n",p->data);
116 return p;
117 }
118 int main()
119 {
120 int middle=0;
121 int num=0;
122 int i=0;
123 link_list *head = NULL;
124 link_list *s = NULL;/*接收指针 case3的指针接收*/
125 head=Create_list(head);
126 printf("head->next %d\n",head->next);
127 printf("head->num %d\n",head->data);
128
129
130
131
132 Insert_list(head,'a');
133 Insert_list(head,'b');
134 Insert_list(head,'c');
135 //'A'++;
136
137 browse(head);
138
139
140 //List A,B,C;
141
142 return 0;
143 }