数据结构学习连接

来源:互联网 发布:精通nginx第二版 编辑:程序博客网 时间:2024/05/20 20:47

栈和队列

http://www.cnblogs.com/sharpCode/archive/2011/04/07/2008841.html

 

 

背包问题

View Code

表达式求值

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef char ElemType;
4 typedef struct LNode
5 {
6 ElemType ch;
7 struct LNode *next;
8 }LNode ,*LinkList;
9 typedef struct DbLNode
10 {
11 double ch;
12 struct DbLNode *next;
13 }DbLNode ,*DbLinkList ;
14 typedef DbLinkList DbLinkStack;
15 typedef LinkList LinkStack;
16
17  void initDbStack(DbLinkStack *S)
18 {
19 *S=NULL;
20 }
21  void initStack(LinkStack *S)
22 {
23 *S=NULL;
24 }
25  void Push(LinkStack *S,ElemType e)
26 {
27 LNode *p;
28 p=(LNode *)malloc(sizeof(LNode));
29 p->ch=e;
30 p->next=*S;
31 *S=p;
32 }
33  void PushDb(DbLinkStack *S,double e)
34 {
35 DbLNode *p;
36 p=(DbLNode *)malloc(sizeof(DbLNode));
37 p->ch=e;
38 p->next=*S;
39 *S=p;
40 }
41  bool Pop(LinkStack *S,ElemType *e)
42 {
43 if(*S)
44 {
45 LNode *p;
46 p=*S;
47 *S=p->next;
48 *e=p->ch;
49 free(p);
50 return true;
51 }
52 else
53 {
54 return false;
55 }
56 }
57
58  bool PopDb(DbLinkStack *S,double *e)
59 {
60 if(*S)
61 {
62 DbLNode *p;
63 p=*S;
64 *S=p->next;
65 *e=p->ch;
66 free(p);
67 return true;
68 }
69 else
70 {
71 return false;
72 }
73 }
74  bool getTop(LinkStack S ,ElemType *e)
75 {
76 if(S)
77 {
78 *e=S->ch;
79 return true;
80 }
81 else
82 {
83 return false;
84 }
85 }
86  bool stackEmpty (LinkStack S)
87 {
88 if(S) return false;
89 else return true;
90 }
91
92  bool OpMember(ElemType ch)
93 {
94 if(ch=='(' || ch==')' || ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='#')
95 {
96 return true;
97 }
98 else
99 {
100 return false;
101 }
102 }
103  int order(ElemType op)
104 {
105 switch (op)
106 {
107 case '#':
108 {
109 return -1;
110 }break;
111 case '(':
112 {
113 return 0;
114 }break;
115 case '+':
116 case '-':
117 {
118 return 1;
119 }break;
120 case '*':
121 case '/':
122 case ')':
123 {
124 return 2;
125 }break;
126 default :
127 {
128 return -100;
129 }
130 }
131 }
132  int precede(ElemType a,ElemType b)
133 {
134 if(order(a) >= order(b) ) return 1;
135 else return 0;
136 }
137  void transform(char * suffix,char *exp )
138 {
139 LinkStack S;
140 initStack(&S);
141 Push(&S,'#');
142 ElemType *p;
143 ElemType ch;
144 ElemType e;
145 int k=0;
146 p=exp;
147 ch=*p;
148 while(!stackEmpty(S))
149 {
150 if(!OpMember(ch)) suffix[k++]=ch;
151 else
152 {
153 switch (ch)
154 {
155 case '(': {Push(&S,ch);}break;
156 case ')':
157 {
158 Pop(&S,&e);
159 while(e!='(')
160 {
161 suffix[k++]=e;
162 Pop(&S,&e);
163 }
164 }break;
165 default :
166 {
167 while(getTop(S,&e) && precede(e,ch))
168 {
169 suffix[k++]=e;
170 Pop(&S,&e);
171 }
172 if(ch!='#') Push(&S,ch);
173 }break;
174 }
175 }
176 if(ch!='#') ch=*++p;
177 }
178 suffix[k]='/0';
179
180 }
181  void printSuffix(char *suffix)
182 {
183 printf("输出后缀式:");
184 char *p;
185 p=suffix;
186 while(*p!='/0')
187 {
188 printf("%c",*p);
189 p++;
190 }
191 printf("/n");
192 }
193  double Operate(double a,char ch,double b)
194 {
195 switch (ch)
196 {
197 case '+': return a+b;break;
198 case '-': return a-b;break;
199 case '*': return a*b;break;
200 case '/': return a/b;break;
201 default:
202 return 0;break;
203 }
204 }
205  double evaluation(char *suffix)
206 {
207 DbLinkStack S;
208 initDbStack(&S);
209 char ch;
210 double a,b;
211 double result;
212 ch=*suffix++;
213 while(ch!='#')
214 {
215 if(!OpMember(ch)) PushDb(&S,ch-48);
216 else
217 {
218 PopDb(&S,&b); PopDb(&S,&a);
219 PushDb(&S,Operate(a,ch,b));
220 }
221 ch=*suffix++;
222 }
223 PopDb(&S,&result);
224 return result;
225 }
226  void main()
227 {
228 char suffix[100];
229 char exp[100]="2+(1+(4/2-1))*3#";
230 transform(suffix,exp);
231 printSuffix(suffix);
232 printf("%f",evaluation(suffix));
233
234 }

车厢调度

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 typedef char ElemType;
5 typedef struct LNode
6 {
7 ElemType elem;
8 struct LNode *next;
9 }LNode ,*LinkList;
10 typedef LinkList LinkStack;
11
12  void initLinkStack(LinkStack *LS)
13 {
14 *LS=NULL;
15 }
16  void push(LinkStack *LS,ElemType e)
17 {
18 LNode *s;
19 s=(LNode *)malloc(sizeof(LNode));
20 s->elem=e;
21 s->next=*LS;
22 *LS=s;
23 }
24  void pop(LinkStack *LS,ElemType *s)
25 {
26 LNode *p;
27 p=*LS;
28 *s=p->elem;
29 *LS=p->next;
30 free(p);
31 }
32  void getTop(LinkStack *LS,ElemType *top)
33 {
34 LNode *p;
35 p=*LS;
36 *top=p->elem;
37 }
38  void diaodu(ElemType *array,int n,ElemType *out)
39 {
40 int i;
41 int k=0;
42 ElemType top;
43 ElemType s;
44 LinkStack LS;
45 initLinkStack(&LS);
46 for(i=0;i<n;i++)
47 {
48 push(&LS,array[i]);
49 getTop(&LS,&top);
50 if(top=='S')
51 {
52 pop(&LS,&s);
53 out[k++]=s;
54 }
55 }
56 while(LS!=NULL)
57 {
58 pop(&LS,&s);
59 out[k++]=s;
60 }
61 }
62  void main()
63 {
64 int i;
65 ElemType *array="HSSHHSSHSHHHHSSHHSHSS";
66 ElemType *out=(ElemType *)malloc(sizeof(ElemType)*strlen(array));
67 diaodu(array,strlen(array),out);
68
69 for(i=0;i<strlen(array);i++)
70 {
71 printf("%c ",out[i]);
72 }
73 }

递归函数的非递归形式

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3  #define TRUE 1
4  #define FALSE 0
5 typedef struct
6 {
7 int nval;
8 int xval;
9 int yval;
10 }ElemType;
11 typedef struct LNode
12 {
13 ElemType elem;
14 struct LNode * next;
15 }LNode,*LinkList;
16 typedef LinkList LinkStack;
17  void initLinkStack(LinkStack *LS)
18 {
19 *LS=NULL;
20 }
21  void push(LinkStack *LS,ElemType e)
22 {
23 LNode *s;
24 s=(LNode *)malloc(sizeof(LNode));
25 s->elem=e;
26 s->next=*LS;
27 *LS=s;
28 }
29 void pop(LinkStack *LS,ElemType *s)
30 {
31 LNode *p;
32 p=*LS;
33 *s=p->elem;
34 *LS=p->next;
35 free(p);
36 }
37 void getTop(LinkStack *LS,ElemType *top)
38 {
39 LNode *p;
40 p=*LS;
41 *top=p->elem;
42 }
43 int emptyStack(LinkStack *LS)
44 {
45 LNode *p;
46 p=*LS;
47 if(p==NULL) return TRUE;
48 else return FALSE;
49 }
50 int value(ElemType top)
51 {
52 if(top.nval==0) return top.xval+1;
53 else
54 {
55 switch (top.nval)
56 {
57 case 1:return top.xval;
58 case 2:return 0;
59 case 3:return 1;
60 default:return 2;
61 }
62 }
63 }
64 int g(int n,int x,int y)
65 {
66 // ElemType e;
67 int result;
68 ElemType top;
69 LinkStack LS;
70 initLinkStack(&LS);
71 top.nval=n;
72 top.xval=x;
73 top.yval=y;
74 push(&LS,top);
75 do
76 {
77 while(top.nval!=0 && top.yval!=0)
78 {
79 top.yval--;
80 push(&LS,top);
81 }
82 pop(&LS,&top);
83 result=value(top);
84 if(!emptyStack(&LS))
85 {
86 pop(&LS,&top);
87 top.nval--;top.yval=top.xval;top.xval=result;
88 push(&LS,top);
89 }
90 }while(!emptyStack(&LS));
91 return result;
92 }
93 void main()
94 {
95 printf("%d/n",g(3,1,1));
96 }

检查是否符合该模式

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define TRUE 1
4 #define FALSE 0
5 typedef char ElemType;
6 typedef struct LNode
7 {
8 ElemType elem;
9 struct LNode * next;
10 }LNode ,*LinkList;
11 typedef LinkList LinkStack;
12 void initLinkStack(LinkStack *LS)
13 {
14 *LS=NULL;
15 }
16
17 void push(LinkStack *LS,ElemType e)
18 {
19 LNode *s;
20 s=(LNode *)malloc(sizeof(LNode));
21 s->elem=e;
22 s->next=*LS;
23 *LS=s;
24 }
25 void pop(LinkStack *LS,ElemType *s)
26 {
27 LNode *p;
28 p=*LS;
29 *LS=p->next;
30 *s=p->elem;
31 free(p);
32 }
33 void getTop(LinkStack *LS,ElemType *top)
34 {
35 LNode *p;
36 p=*LS;
37 *top=p->elem;
38 }
39 int emptyStack(LinkStack *LS)
40 {
41 LNode *p;
42 p=*LS;
43 if(p==NULL) return TRUE;
44 else return FALSE;
45 }
46 void checkMode(ElemType *check,int *ischeck)
47 {
48
49 LinkStack LS;
50 ElemType *p;
51 ElemType s;
52 p=check;
53 initLinkStack(&LS);
54 *ischeck=TRUE;
55 while(*p!='&')
56 {
57 push(&LS,*p++);
58 }
59 p++;
60 while(!emptyStack(&LS)&& *p!='@')
61 {
62 pop(&LS,&s);
63 if(s!=*p++)
64 {
65 *ischeck=FALSE;
66 return;
67 }
68 }
69 if(!emptyStack(&LS) || *p!='@') *ischeck=FALSE;
70 }
71 void main()
72 {
73 int ischeck;
74 ElemType *check="a+1&1+a@";
75 checkMode(check,&ischeck);
76 if(ischeck) printf("符合该模式/n");
77 else printf("不符合该模式/n");
78 }

括号匹配

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef char ElemType;
4 typedef struct LNode
5 {
6 ElemType bracket;
7 struct LNode *next;
8 }LNode ,*LinkList;
9 typedef LinkList LinkStack;
10 void initStack(LinkStack *S)
11 {
12 *S=NULL;
13 }
14 void Push(LinkStack *S,ElemType e)
15 {
16 LNode *p;
17 p=(LNode *)malloc(sizeof(LNode));
18 p->bracket=e;
19 p->next=*S;
20 *S=p;
21 }
22 bool Pop(LinkStack *S,ElemType *e)
23 {
24 if(*S)
25 {
26 LNode *p;
27 p=*S;
28 *e=p->bracket;
29 *S=p->next;
30 free(p);
31 return true;
32 }
33 else return false;
34 }
35 bool stackEmpty(LinkStack S)
36 {
37 if(S) {return false;}
38 else return true;
39 }
40 ElemType getTop(LinkStack S)
41 {
42 return S->bracket;
43 }
44 bool matching (char *exp)
45 {
46 LinkStack S;
47 initStack(&S);
48 char ch=*exp++;
49 int state=1;
50 char e;
51 while(ch!='#' && state)
52 {
53 switch (ch)
54 {
55 case '(':
56 case '[':
57 {
58 Push(&S,ch);
59 }break;
60 case ')':
61 {
62 if(!stackEmpty(S) &&getTop(S)=='(') {Pop(&S,&e);}
63 else state=0;
64 }break;
65 case ']':
66 {
67 if(!stackEmpty(S) && getTop(S)=='[') {Pop(&S,&e);}
68 else state=0;
69 }break;
70 }
71 ch=*exp++;
72 }
73 if(state && stackEmpty(S)) return true;
74 else return false;
75 }
76 void main()
77 {
78
79 char exp[100];
80
81 printf("输入括号表达式:");
82 gets(exp);
83 if(matching(exp))
84 {
85 printf("括号匹配");
86 }
87 else
88 {
89 printf("括号不匹配");
90 }
91
92
93 }

数制转换

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef bool status;
4 typedef struct LinkStack
5 {
6 int remainder; //余数
7 struct LinkStack *next;
8 }LinkStack,*PLStack;
9 void initStack(PLStack &S)
10 {
11 S=NULL;
12 }
13 void Push(PLStack &S,int e)
14 {
15 PLStack p=(PLStack)malloc(sizeof(LinkStack));
16 p->remainder=e;
17 p->next=S;
18 S=p;
19 }
20 status stackIsEmpty(PLStack &S)
21 {
22 if(S==NULL)
23 {
24 return true;
25 }
26 else
27 {
28 return false;
29 }
30 }
31 void Pop(PLStack &S ,int &e)
32 {
33 if(S)
34 {
35 PLStack p=(PLStack)malloc(sizeof(LinkStack));
36 e=S->remainder;
37 p=S;
38 S=S->next;
39 free(p);
40 }
41
42 }
43 void main()
44 {
45 PLStack S;
46 initStack(S);
47 int N,e;
48 printf("输入任意非负十进制整数,打印输出与其等值的八进制数:");
49 scanf("%d",&N);
50 while(N)
51 {
52 Push(S,N%8);
53 N=N/8;
54 }
55 while(!stackIsEmpty(S))
56 {
57 Pop(S,e);
58 printf("%d",e);
59 }
60 }

循环链表表示队列

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef char ElemType;
4 typedef struct LNode
5 {
6 ElemType data;
7 struct LNode *next;
8 }LNode ,*LinkList;
9 typedef LinkList QueuePtr;
10 typedef struct
11 {
12 QueuePtr rear;
13 }LinkQueue;
14 void initQueue(LinkQueue *Q)
15 {
16 LNode *s;
17 s=(LNode *)malloc(sizeof(LNode));
18 Q->rear=s;
19 s->next=Q->rear;
20 }
21 void enQueue(LinkQueue *Q,ElemType e)
22 {
23 LNode *s;
24 s=(LNode *)malloc(sizeof(LNode));
25 s->data=e;
26 s->next=Q->rear->next;
27 Q->rear->next=s;
28 Q->rear=s;
29 }
30 void delQueue(LinkQueue *Q,ElemType *e)
31 {
32 if(Q->rear->next!=Q->rear) //队列不空
33 {
34 LNode *p;
35 LNode *q;
36 p=Q->rear->next; //p指向头结点
37 q=p->next;
38 *e=q->data;
39 p->next=q->next;
40 if(q->next==p) //剩下最后一个结点 要将Q->rear 指向 头结点 ,不然会有内存问题
41 {
42 Q->rear=p;
43 }
44 free(q);
45 }
46 else //队列空
47 {
48 *e='#';
49 }
50 }
51 void main()
52 {
53 LinkQueue Q;
54 ElemType e;
55 initQueue(&Q);
56 enQueue(&Q,'a'); //测试用过的数据
57 enQueue(&Q,'b'); //测试用过的数据
58 enQueue(&Q,'c'); //测试用过的数据
59 delQueue(&Q,&e); //测试用过的数据
60 delQueue(&Q,&e); //测试用过的数据
61 delQueue(&Q,&e); //测试用过的数据
62 enQueue(&Q,'d'); //测试用过的数据
63 delQueue(&Q,&e); //测试用过的数据
64 printf("%c",e);
65 }

杨辉三角

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define TRUE 1
4 #define FALSE 0
5
6 typedef int ElemType;
7
8 typedef struct
9 {
10 ElemType *elem;
11 int front;
12 int rear;
13 int queuesize;
14 }Queue;
15
16 void initQueue(Queue *Q,int maxsize)
17 {
18 Q->elem=(ElemType *)malloc(sizeof(ElemType)*(maxsize+1));
19 Q->front=Q->rear=0;
20 Q->queuesize=maxsize;
21
22 }
23 void enQueue(Queue *Q,ElemType e)
24 {
25 //if((Q->rear+1)%Q->queuesize==Q->front) return FALSE;
26 Q->elem[Q->rear]=e;
27 Q->rear=(Q->rear+1)%Q->queuesize;
28 // return TRUE;
29
30 }
31 void delQueue(Queue *Q,ElemType *h)
32 {
33 //if(Q->front==Q->rear) return FALSE;
34 *h=Q->elem[Q->front];
35 Q->front=(Q->front+1)%Q->queuesize;
36 // return TRUE;
37 }
38 void getHead(Queue *Q ,ElemType *s)
39 {
40 // if(Q->front==Q->rear) return FALSE;
41 *s=Q->elem[Q->front];
42 // return TRUE;
43 }
44 int emptyQueue(Queue *Q)
45 {
46 Queue *S;
47 S=Q;
48 if(S->front==S->rear) return TRUE;
49 else return FALSE;
50
51 }
52 void yanghui()
53 {
54 Queue Q;
55 int n;
56 int k=1;
57 // int e; //要插入到队尾的结点的值
58 int h; //getHead 得到的队头结点的值
59 int s; // delQueue 删除得到的队头结点的值
60 printf("输入杨辉三角的行数:");
61 scanf("%d",&n);
62 initQueue(&Q,n+2);
63 enQueue(&Q,0);
64 enQueue(&Q,1);
65 enQueue(&Q,1);
66 printf("%4d/n",1);
67 while(k<n)
68 {
69 enQueue(&Q,0);
70 do
71 {
72 delQueue(&Q,&s);
73 getHead(&Q,&h);
74 if(h!=0) printf("%4d",h);
75 else printf("/n");
76 enQueue(&Q,s+h);
77 }while(h!=0);
78 k++;
79 }
80 while(!emptyQueue(&Q))
81 {
82 delQueue(&Q,&s);
83 printf("%d",s);
84 }
85
86 /* while ( k <=n ) { // 通过循环队列输出n 行的值
87
88 enQueue ( &Q, 0 ); // 行界值"0"入队列
89 do { // 输出第 k 行,计算第 k+1 行
90 delQueue ( &Q, &s );
91 getHead ( &Q, &h );
92 if (h) { printf("%4d",h); }
93 // 若e为非行界值0,则打印输出 e 的值并加一空格
94 else printf("/n"); // 否则回车换行,为下一行输出做准备
95 enQueue(&Q, s+h);
96 } while (h!=0);
97 k++;
98 }
99 */
100 }
101 void main()
102 {
103 yanghui();
104 printf("/n");
105 }

运动会安排日程

View Code
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef int ElemType;
4 #define TRUE 1
5 #define FALSE 0
6 typedef struct
7 {
8 ElemType *elem;
9 int front;
10 int rear;
11 int queuesize;
12 int incrementsize;
13 }Queue;
14 void initQueue(Queue * Q,int size)
15 {
16 Q->elem=(ElemType *)malloc(sizeof(ElemType)*10); //size +1 判断队列是否满
17 Q->front=Q->rear=0;
18 Q->queuesize=size;
19 Q->incrementsize=10;
20 }
21 void incrementQueuesize(Queue *Q)
22 {
23 int k;
24 ElemType *a;
25 a=(ElemType *)malloc(Q->queuesize+Q->incrementsize);
26 for(k=0;k<Q->queuesize-1;k++)
27 a[k]=Q->elem[(Q->front+k)%Q->queuesize];
28 //腾挪原循环队列中的数据元素
29 free(Q->elem); //释放原占数组空间
30 Q->elem=a; //为Q.elem设置新的数组位置
31 Q->front=0;
32 Q->rear=Q->queuesize-1; //设置新的头尾指针
33 Q->queuesize+=Q->incrementsize;
34 }
35 void enQueue(Queue *Q ,ElemType e)
36 {
37 if((Q->rear+1)==Q->front) incrementQueuesize(Q);
38 Q->elem[Q->rear]=e;
39 Q->rear=(Q->rear+1)%Q->queuesize;
40 }
41 void delQueue(Queue * Q,ElemType * h)
42 {
43 *h=Q->elem[Q->front];
44 Q->front=(Q->front+1)%Q->queuesize;
45 }
46 int emptyQueue(Queue * Q)
47 {
48 Queue *S;
49 S=Q;
50 if(S->front==S->rear) return TRUE;
51 else return FALSE;
52 }
53 void division(int R[9][9],int n,int *result)
54 {
55 int pre=n;
56 int group=0;
57 int e;
58 int h;
59 int j;
60 int clash[9];
61 int flag=1;
62 Queue Q;
63 initQueue(&Q,n);
64 for(e=0;e<n-1;e++)
65 {
66 enQueue(&Q,e);
67 }
68 while(!emptyQueue(&Q))
69 {
70 if(flag)
71 {
72 enQueue(&Q,n-1);
73 flag=0;
74 }
75 delQueue(&Q,&h);
76 if(h<=pre)
77 {
78 group++;
79 for(j=0;j<n;j++) clash[j]=0;
80 }
81 if(clash[h]==0)
82 {
83 result[h]=group;
84 for(j=0;j<n;j++) clash[j]+=R[h][j];
85 }
86 else enQueue(&Q,h);
87 pre=h;
88 }
89 }
90 void main()
91 {
92 int n=9;
93 int i;
94 int k=1;
95 int max=0;
96 int result[9];
97 int r[9][9]=
98 {{0,1,0,0,0,1,0,0,0},
99 { 1,0,0,0,1,1,0,1,1},
100 { 0,0,0,0,0,1,1,0,0},
101 { 0,0,0,0,1,0,0,0,1},
102 { 0,1,0,1,0,0,1,0,1},
103 { 1,1,1,0,0,0,1,0,0},
104 { 0,0,1,0,1,1,0,0,0},
105 { 0,1,0,0,0,0,0,0,0},
106 { 0,1,0,1,1,0,0,0,0}};
107 division(r,n,result);
108 for(i=0;i<n;i++)
109 {
110 if(result[i]>max) max=result[i];
111 printf("%4d",result[i]);
112 }
113
114 while(k<=max)
115 {
116 printf("/n");
117 printf("%d :",k);
118 for(i=0;i<n;i++)
119 {
120 if(result[i]==k) printf("%d ",i);
121 }
122 k++;
123 }
124 printf("/n");
125 }

 

线性表

 

 http://www.cnblogs.com/sharpCode/archive/2011/04/07/2008825.html

 

 

二叉树和树

http://www.cnblogs.com/sharpCode/archive/2011/04/18/2020285.html

 

 

串和数组

http://www.cnblogs.com/sharpCode/archive/2011/04/08/2009472.html

原创粉丝点击