请注意函数名为此题的大致意思,函数名后的数字为该章节编程题的序号,请注意以序号为准 ——页面长请使用左侧目录
第2章
2.2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
typedef int
type;
typedef struct
{ int len;
type data[MAX];
}sqList;
bool mindel1(sqList *a,
type *value)
{
if(a->len<=
0)
{
printf(
"线性表为空!");
return -
1;
}
int j=
0;
for(int i=
1;i<a->len;i++)
{
if((a->
data)[i]<a->data[j])
j=i;
}
*value=a->
data[j];
a->
data[j]=(a->data)[(a->len)-1];
return
0;
}
int nizhi2(sqList *a)
{
if(a->len<
0)
return -
1;
for(int i=
0;i<=a->len/
2;i++)
{
a->
data[i]+=a->data[a->len-1-i] ;
a->
data[a->len-1-i]=a->data[i]-a->data[a->len-1-i];
a->
data[i]-=a->data[a->len-1-i];
}
return
0;
}
int delsameele3(sqList *a,
type x)
{ //每遍历一个元素时都考虑其向前移动的位数
int k=
0;
for(int i=
0;i<a->len;i++)
if(a->
data[i]!=x)
a->
data[i-k]=a->data[i];
else k++;
a->len-=k;//否则最后的k个元素将重复出现,也符合定义
return
0;
// 方法二:
// for(int i=
0;i<a->len;i++)
//
if(a->
data[i]!=x)
// a->
data[k++]->data[i];//删除后的顺序表k上的元素总等于按顺序不等于x的i位置的元素
// a->len=k;
}
int delsure4(sqList *a,
type min,type max)
{
int k;
if(min>=max||a->len<=
0)
return -
1;
for(int i=
0;i<a->len;i++)
if(a->
data[i]>min&&a->data[i]<max)
a->
data[k++]=a->data[i];//删除后的顺序表k上的元素总等于按顺序不等于x的i位置的元素
a->len=k;
return
0;
}
int delsure5(sqList *a,
type min,type max)
{
int k;
if(min>=max||a->len<=
0)
return -
1;
for(int i=
0;i<a->len;i++)
if(a->
data[i]>=min&&a->data[i]<=max)
a->
data[k++]=a->data[i];//删除后的顺序表k上的元素总等于按顺序不等于x的i位置的元素
a->len=k;
return
0;
}
int delsame6(sqList *a)
{
int k=
0;
for(int i=
1;i<a->len;i++)
if(a->
data[i]>a->data[i-1])//若要实现逆序的顺序表去重则换位小于号
a->
data[i-k]=a->data[i];
else if(a->
data[i-1]=a->data[i]) k++;
else return -
1;//如果顺序表不是顺序则输出错误
a->len-=k;
return
0;
}
bool isposorder(sqList *a)//非题
{
for(int i=
1;i<a->len;i++)
if(a->
data[i]<a->data[i-1])
return false;
return true;
} int combine7(sqList *a,sqList *b,sqList *c) { int i=0;int j=0;int k=0; c->len=a->len+b->len; if(!isposorder(a)||!isposorder(b)) return -1; else { while(j<a->len&&k<b->len) { if(a->data[j]<=b->data[k]) c->data[i++]=a->data[j++]; else c->data[i++]=b->data[k++]; } if(j==a->len) while(k<b->len) c->data[i++]=b->data[k++]; else while(j<a->len) c->data[i++]=a->data[j++]; return 0; } }
<span class="hljs-keyword">if</span>(a-><span class="hljs-typedef"><span class="hljs-keyword">data</span>[i]>=a->len||a-><span class="hljs-keyword">data</span>[i]<0)</span>
return -<span class="hljs-number">2</span>;
<span class="hljs-keyword">else</span>
t[a-><span class="hljs-typedef"><span class="hljs-keyword">data</span>[i]]++;</span>
}
for(int i=<span class="hljs-number">0</span>;i<a->len;i++)
<span class="hljs-keyword">if</span>(t[i]>a->len/<span class="hljs-number">2</span>)
return i;
return -<span class="hljs-number">1</span>;
} int change_8(sqList *a,int n) { reverse(a,0,a->len-1); reverse(a,0,a->len-1-n); reverse(a,a->len-n,a->len-1); return 0; } int insert9(sqList *a,int x) { int low=0;int high=a->len-1; int mid; if(!isposorder(a)) return -1; while(low<=high) { mid=(low+high)/2; if(x>=a->data[mid]) low=mid; else high=mid-1; } if(a->data[mid]==x&&high!=a->len-1) { a->data[mid]+=a->data[mid+1]; a->data[mid+1]=a->data[mid]-a->data[mid+1]; a->data[mid]=a->data[mid]-a->data[mid+1]; } if(low>high) { for(int i=a->len-1;i<high;i a->data[i+1]=a->data[i]; a->data[high+1]=x; a->len++; } return 0; } void reverse10(sqList *a,int m,int n)//把数组中的元素从下标为n到 下标为n的元素逆置 { for(int i=0;i<=(n-m)/2;i++) { int t; t=a->data[m+i]; a->data[m+i]=a->data[n-i]; a->data[n-i]=t;
}
} int leftmove10(sqList *a,int p) {
//在第<span class="hljs-number">8</span>题的基础上实际为数组交换位置,只不过此时不能用二分法时间复杂度为<span class="hljs-type">O</span>(n) ,空间复杂度为<span class="hljs-type">O</span>(<span class="hljs-number">1</span>)
reverse10(a,<span class="hljs-number">0</span>,a->len-<span class="hljs-number">1</span>);
reverse10(a,<span class="hljs-number">0</span>,a->len-p);
reverse10(a,a->len-p+<span class="hljs-number">1</span>,a->len-<span class="hljs-number">1</span>);
return <span class="hljs-number">0</span>;
}int midpos11(sqList *a,sqList *b) { if(a->len!=b->len||!isposorder(a)||!isposorder(b)) return -1; //将两个升序数组合并成一个新的数组只用找到新数组第(n+n)/2上位置上元素时间复杂度为O(n) ,空间复杂度为O(1) int i,j,k;i=j=k=0; for(int m=1;m<=a->len;m++) if(a->data[i]<=b->data[j]) { if(i++a->len||ma->len) return a->data[ } else { if(j++b->len||ma->len) return a->data[ } } int mainele12(sqList *a) { //需要一个与原数组长度一致的空间,其i号记录原数组中值为i的元素个数 type t[a->len]; for(int i=0;i<a->len;i++) t[i]=0; for(int i=0;i<a->len;i++) { if(a->data[i]>=a->len||a->data[i]<0) return -2; else t[a->data[i]]++; } for(int i=0;i<a->len;i++) if(t[i]>a->len/2) return i; return -1; }
2.2测试用例
int main(
void)
{
type value;
int a[
10];
sqList *sql=(sqList *)
malloc(
sizeof(sqList));
sqList *sql1=(sqList *)
malloc(
sizeof(sqList));
sqList *sql2=(sqList *)
malloc(
sizeof(sqList));
sql1->len=sql->len=
10;
sql2->len=sql->len+sql1->len;
for(
int i=
0;i<sql->len;i++)
scanf(
"%d",&sql->data[i]);
printf(
"%d",mainele12(sql));
return 0;
}
2.3
#include "stdafx.h"
#include<stdio
.h
>
#include<malloc
.h
>
#include<stdlib
.h
>
typedef int
type;
typedef struct lnode
{
int
data;
struct lnode
*next;
}Lnode;
typedef Lnode node;
typedef struct dnode
{
int
data;
struct dnode
*lnext;
struct dnode
*rnext;
}Dnode;
node
*headinsert()
{
int x;
node
*h
= (node
*)malloc(sizeof(node));
h
->next
= NULL;
scanf_s(
"%d",
&x);
while (x
!= -1)
{
node
*s
= (node
*)malloc(sizeof(node));
s
->data = x;
s
->next
= h
->next;
h
->next
= s;
scanf_s(
"%d",
&x);
}
return h;
}
node
*tailinsert()
{
int x;
node
*h,
*t;
t
= h
= (node
*)malloc(sizeof(node));
h
->next
= NULL;
scanf_s(
"%d",
&x);
while (x
!= -1)
{
node
*s
= (node
*)malloc(sizeof(node));
s
->data = x;
s
->next
= NULL;
t
->next
= s;
t
= s;
scanf_s(
"%d",
&x);
}
return h;
}
node
*insert(node
*h, int n, int m, int b)
{
node
*p;
if (h
== NULL)
return h;
p
= h
->next;
if (b
== 0)
{
if (p
->data == n)
{
node
*tem
= (node
*)malloc(sizeof(node));
tem
->data = m;
tem
->next
= p;
h
->next
= tem;
}
while (p
->next
!= NULL)
{
if ((p
->next
->data)
== n)
{
node
*tem
= (node
*)malloc(sizeof(node));
tem
->data = m;
tem
->next
= p
->next;
p
->next
= tem;
p
= p
->next;
}
p
= p
->next;
}
}
else
{
if ((h
->next
->data == n))
{
node
*tem
= (node
*)malloc(sizeof(node));
tem
->data = m;
tem
->next
= h
->next
->next;
h
->next
->next
= tem;
p
= p
->next;
}
while (p
!= NULL)
{
if (p
->data == n)
{
node
*tem
= (node
*)malloc(sizeof(node));
tem
->data = m;
tem
->next
= p
->next;
p
->next
= tem;
}
p
= p
->next;
}
}
return h;
}
int
select(node
*h, int n)
{
int i
= 1;
while (h
->data != n)
{
if (h
->next
== NULL)
return 0;
h
= h
->next;
i
++;
}
return i
- 1;
}
int getlength(node
*h)
{
node
*p
= h;
int i
= 0;
while (p
->next
!= NULL)
{
p
= p
->next;
i
++;
}
return i;
}
node
*get(node
*h, int n)
{
node
*p
= h;
int i;
if (n
>getlength(h)
|| n
<= 0)
return h;
for (i
= 1; i
<= n; i
++)
p
= p
->next;
return p;
}
void print(node
*h)
{
node
*p
= h
->next;
if (h
== NULL)
return;
while (p
!= NULL)
{
printf(
"%d ", p
->data);
p
= p
->next;
}
printf(
"\n");
getchar();
}
void difference(node
*a, node
*b)
{
node
*q
= b;
node
*p;
node
*pre,
*r;
pre
= a;
p
= a
->next;
if (
!p)
return;
while (p)
{
q
= b
->next;
while (q
!= NULL&&p
->data != q
->data)
{
q
= q
->next;
}
if (q
!= NULL)
{
r
= p;
pre
->next
= p
->next;
p
= p
->next;
free(r);
}
else
{
pre
= p;
p
= p
->next;
}
}
}
void bubblesort1(node
*h)
{
int w, i, change, j;
node
*p1,
*p2,
*p3;
for (i
= getlength(h), change
= 1; i
>1 && change; i
--)
{
change
= 0;
for (j
= 1; j
<i;
++j)
if (get(h, j)
->data>get(h, j
+ 1)
->data)
{
p1
= get(h, j
- 1);
p2
= get(h, j);
p3
= get(h, j
+ 1);
p2
->next
= p3
->next;
p3
->next
= p2;
p1
->next
= p3;
change
= 1;
}
}
}
node
*linkcombine(node
*a, node
*b)
{
node
*p
= a; node
*q
= b
->next; node
*prep
= a;
node
*preq
= b; node
*tem;
if (p
== NULL)
return b;
if (q
== NULL)
return b;
while (q
!= NULL)
{
printf(
"当前a:\n");
print(a);
while (p
->next
!= NULL&&p
->next
->data <= q
->data)
{
p
= p
->next;
}
tem
= q;
q
= q
->next;
tem
->next
= p
->next;
p
->next
= tem;
p
= p
->next;
}
}
void recursiondel1(node
*h,
type x)
{
node
*p;
if (h
== NULL)
return;
if (h
->data == x)
{
p
= h;
h
= h
->next;
free(p);
recursiondel1(h, x);
}
else
recursiondel1(h
->next, x);
}
node
*del2(node
*h, int n)
{
node
*p
= h;
node
*q;
while (p
->next
!= NULL)
if ((p
->next)
->data == n)
{
q
= p
->next;
p
->next
= q
->next;
free(q);
}
else
p
= p
->next;
return h;
}
void r_print3(node
*h)
{
if (h
== NULL)
return;
r_print3(h
->next);
printf(
"%d ", h
->data);
}
void delminest4(node
*h)
{
node
*p,
*q,
*min;
if (h
->next
== NULL)
return;
p
= h;
min = q
= h
->next;
while (q
->next
!= NULL)
{
if (q
->next
->data<min->data)
{
p
= q;
min = q
->next;
}
q
= q
->next;
}
p
->next
= min->next;
free(
min);
}
void reverse5(node
*h)
{
node
*p
= h
->next;
node
*q;
h
->next
= NULL;
while (p
!= NULL)
{
q
= p;
p
= p
->next;
q
->next
= h
->next;
h
->next
= q;
}
}
void bubblesort6(node
*h)
{
int w, i, change, j;
node
*tem;
for (i
= getlength(h), change
= 1; i
>1 && change; i
--)
{
change
= 0;
for (j
= 1; j
<i;
++j)
if (get(h, j)
->data>get(h, j
+ 1)
->data)
{
w
= get(h, j)
->data;
get(h, j)
->data = get(h, j
+ 1)
->data;
get(h, j
+ 1)
->data = w;
change
= 1;
}
}
}
int delvalue7(node
*h,
type a,
type b)
{
node
*h1
= h;
while (h1
->next
!= NULL)
if (h1
->next
->data>a
&&h1
->next
->data<b)
{
node
*p
= h1
->next;
h1
->next
= h1
->next
->next;
free(p);
}
else h1
= h1
->next;
return 0;
}
node
*searchsamenode8(node
*h1, node
*h2)
{
int m, n; m
= n
= 0;
node
*p1,
*p2; p1
= h1
->next; p2
= h2
->next;
while (p1
->next
!= NULL)
m
++;
while (p2
->next
!= NULL)
n
++;
if (m
== 0 || n
== 0)
return NULL;
if (m
>= n)
{
m
-= n;
p1
= h1
->next;
while (m
-->0)
p1
= p1
->next;
}
else
{
n
-= m;
p2
= h2
->next;
while (n
-->0)
p1
= p1
->next;
}
if (p1
== p2)
return p1;
else if (p1
== p2
== NULL)
return NULL;
else
{
p1
= p1
->next;
p2
= p2
->next;
}
}
int posprint9(node
*h)
{
node
*p
= h
->next; node
*q
= p;
while (q
->next)
if (q
->next
->data >= q
->data)
q
= q
->next;
else
{
if (q
->next
->data <= p
->data)
{
h
->next
= q
->next;
q
->next
= q
->next
->next;
h
->next
->next
= p;
}
else
{
while (
!(p
->data <= q
->next
->data&&p
->next
->data >= q
->next
->data))
p
= p
->next;
node
*tem
= p
->next;
p
->next
= q
->next;
q
->next
= q
->next
->next;
p
->next
->next
= tem;
}
p
= h
->next;
}
p
= h;
q
= h
->next;
while (q)
{
printf(
"%d ", q
->data);
p
= q; q
= q
->next;
free(p);
}
free(h);
return 0;
}
void resolve10(node
*h, node
*bh)
{
node
*p
= h
->next;
node
*hh
=NULL;
hh
->next
= h;
bh
->next
= p;
while (p
&&p
->next)
{
h
->next
= p
->next;
p
->next
= h
->next
->next;
p
= p
->next;
h
= h
->next;
}
if (h
->next
!= NULL)
h
->next
= NULL;
h
= hh;
}
void pur_linklist12(node
*h)
{
node
*p
= h
->next;
if (
!p
|| !(p
->next))
return;
while (p
!= NULL)
{
node
*q
= p;
node
*tem;
while (q
->next
!= NULL)
{
if (q
->next
->data == p
->data)
{
tem
= q
->next;
q
->next
= q
->next
->next;
free(tem);
}
else q
= q
->next;
}
p
= p
->next;
}
}
bool isposorder(node
*h)
{
node
*p
= h
->next;
while (p
->next)
if (p
->next
->data<p
->data)
return false;
else
p
= p
->next;
return true;
}
node
*combineneg13(node
*h1, node
*h2)
{
if (isposorder(h1)
&& isposorder(h2))
{
node
*h,
*p,
*q,
*t;
h
=t
= NULL;
p
= h1
->next; q
= h2
->next;
while (p
||q)
{
if (
!q
||(p
&&p
->data <= q
->data ))
{
node
*tem
= p;
p
= p
->next;
tem
->next
= t;
h
->next
= tem;
t
= tem;
}
if ((
!p
&&q)
||(q
&&p
->data>q
->data))
{
node
*tem
= q;
q
= q
->next;
tem
->next
= t;
h
->next
= tem;
t
= tem;
}
}
return h;
}
else
return NULL;
}
int comele15(node
*h1, node
*h2)
{
if (
!(isposorder(h1)
&& isposorder(h2)))
return -1;
node
*p
= h1;
node
*q
= h2
->next;
while (q
&&p
->next)
{
node
*tem;
if (p
->next
->data < q
->data)
{
tem
= p;
while (p
->next
&&p
&&p
->next
->data < q
->data)
p
= p
->next;
if (
!p
->next)
{
tem
->next
= p
->next;
break;
}
if (p
->next
->data == q
->data)
tem
->next
= p
->next;
else
{
tem
->next
= p
->next
->next;
p
= p
->next;
}
}
else if (p
->next
->data == q
->data)
{
p
= p
->next; q
= q
->next; tem
= p;
}
else
while (p
->next
&&q
&&p
->next
->data > q
->data)
q
= q
->next;
}
p
->next
= NULL;
return 0;
}
bool issonsequen16(node
*h1, node
*h2)
{
node
*p
= h1
->next;
node
*q
= h2
->next;
if (getlength(h1)
< getlength(h2))
return false;
while (p
&&q)
{
if (p
->data != q
->data)
p
= p
->next;
node
*m
= p;
while (q
&&m)
if (m
->data != q
->data || !m)
{
p
= p
->next; break;
}
else
{
q
= q
->next;
m
= m
->next;
}
if (
!q)
return true;
}
return false;
}
Dnode
*createcirDlink()
{
int x;
Dnode
*h,
*t;
t
= h
= (Dnode
*)malloc(sizeof(Dnode));
h
->rnext
= NULL;
scanf_s(
"%d",
&x);
while (x
!= -1)
{
Dnode
*s
= (Dnode
*)malloc(sizeof(Dnode));
s
->data = x;
s
->rnext
= h
->rnext;
t
->rnext
= s;
s
->lnext
= t;
t
= s;
scanf_s(
"%d",
&x);
}
t
->rnext
= h;
h
->lnext
= t;
return h;
}
bool issymmetry17(Dnode
*h)
{
Dnode
*lnode
= h
->lnext;
Dnode
*rnode
= h
->rnext;
while (
!(lnode
== rnode
|| lnode
->lnext
== rnode))
{
if (lnode
->data != rnode
->data)
return false;
lnode
= lnode
->lnext;
rnode
= rnode
->rnext;
}
return true;
}
node
*createcirlink()
{
int x;
node
*h,
*t;
t
= h
= (node
*)malloc(sizeof(node));
h
->next
= NULL;
scanf_s(
"%d",
&x);
while (x
!= -1)
{
node
*s
= (node
*)malloc(sizeof(node));
s
->data = x;
s
->next
= h
->next;
t
->next
= s;
t
= s;
scanf_s(
"%d",
&x);
}
t
->next
= h;
return h;
}
node
*getcirlinktail(node
*h)
{
node
*p
= h
->next;
while (p
->next
!= h)
p
= p
->next;
return p;
}
void printcirlink(node
*h)
{
node
*p
= h
->next;
while (p
!= h)
{
printf(
"%d ", p
->data);
p
= p
->next;
}
}
void combinecirlink18(node
*h1, node
*h2)
{
node
*h2t
= getcirlinktail(h2);
node
*p
= h1
->next;
h2t
->next
= p;
h1
->next
= h2
->next;
free(h2);
return ;
}
void delcirminest19(node
*h)
{
int
min;
node
*q
=h
->next;
while (q
!=h)
{
min = q
->data;
node
*p
= q
->next;
while (p
!= h){
if (p
->data < min)
min = p
->data;
p
= p
->next;
}
while (p
->next
!= h)
{
if (p
->next
->data == min)
{
printf(
"删除:%d\n", p
->next
->data);
p
->next
= p
->next
->next;
}
p
= p
->next;
}
q
= h
->next;
}
}typedef struct dnode20
{
int
data, fre;
dnode20
*lnext,
*rnext;
}Dnode20;
Dnode20
*Locate20(Dnode20
*h,
type x)
{
Dnode20
*p
=h;
Dnode20
*q,
*m;
while (p
->rnext
&&p
->rnext
->data != x)
p
= p
->rnext;
if (
!p
->rnext)
return NULL;
++(p
->rnext
->fre);
q
= p;
m
= p
->rnext;
p
->rnext
= m
->rnext;
if (m
->rnext
!=NULL)
m
->rnext
->lnext
= p;
while (q
!= h
&&q
->fre
<= m
->fre)
q
= q
->lnext;
q
->rnext
->lnext
= m;
m
->rnext
= q
->rnext;
m
->lnext
= q;
q
->rnext
= m;
}
void printDnode20(Dnode20
*h);
Dnode20
*createDnode20link()
{
type x;
Dnode20
*h,
*t;
t
= h
= (Dnode20
*)malloc(sizeof(Dnode20));
h
->rnext
=h
->lnext
= NULL;
scanf_s(
"%d",
&x);
while (x
!= -1)
{
Dnode20
*s
= (Dnode20
*)malloc(sizeof(Dnode20));
s
->data = x;
s
->fre
= 0;
s
->rnext
= NULL;
t
->rnext
= s;
s
->lnext
= t;
t
= s;
scanf_s(
"%d",
&x);
}
t
->rnext
= NULL;
printDnode20(h);
return h;
}
void printDnode20(Dnode20
*h)
{
Dnode20
*p
= h
->rnext;
while (p)
{
printf(
"%d fre:%d ", p
->data, p
->fre);
p
= p
->rnext;
}
}
2.3测试用例
int _tmain(
int argc, char* argv[])
{
node
*h1,
*h2;
// printf(
"h1为头插法");
//h1 = headinsert();
//printf(
"%d %d %d",h1->
next->data,h1->
next->
next->data,h1->
next->
next->
next->data);
// printf(
"h2为尾插法");
// printf(
"%d",h1->
next->
next->data);
//h1 = tailinsert();
// printf(
"h1:\n");
// print(h1);
// printf(
"h2:\n");
// print(h2);
// printf(
"3在h1的%d号位置,3在h2的%d号位置\n",
select(h1,
3),
select(h2,
3));
// h1 = del(h1,
3);
// h2 = del(h2,
3);
// printf(
"删除3以后h1:\n");
// print(h1);
// printf(
"删除3以后h2:\n");
// print(h2);
// printf(
"在h1的4前添加10,在h2的4后添加10:\n");
// h1 = insert(h1,
4,
10,
0);
// printf(
"h1:\n");
// print(h1);
// h2 = insert(h2,
4,
10,
1);
// printf(
"h2:\n");
// print(h2);
// printf(
"将h1就地倒置:\n");
// reverse(h1);
// print(h1);
// printf(
"删除h2中的所有重复元素后:\n");
// pur_linklist(h2);
// print(h2);
// printf(
"求集合运算h1-h2后的h1:\n");
// difference(h1, h2);
// print(h1);
// printf(
"h1交换结点冒泡排序后:\n");
// bubblesort1(h1);
// print(h1);
// printf(
"h1交换结点值冒泡排序后:\n");
// bubblesort2(h1);
// print(h1);
// printf(
"h2交换结点值冒泡排序后:\n");
// bubblesort2(h2);
// print(h2);
// printf(
"h1与h2合并后:\n");
// linkcombine(h1,h2);
//recursiondel1(h2->
next,
3);
// r_print(h2->
next);
// delminest4(h2);
// delvalue7(h2,
2,
5);
// posprint9(h2);
// resolve1
0(h2->
next,h1) ;
// print(h2);
// print(h1);
//h1 = tailinsert();
//print(combineneg13(h1, h2));
//h2 = tailinsert();
//comele(h1,h2);
//print(h1);
// printf(
"%d",issonsequen16(h1,h2));
//node *cirh1 = createcirlink();
/*node *cirh2= createcirlink();
combinecirlink18(cirh1, cirh2);
printcirlink(cirh1);*/
//
printf(
"%d", issymmetry17(h));
//delcirminest19(cirh1);
Dnode2
0 *h=createDnode20link();
Locate2
0(h,
1);
printDnode2
0(h);
Locate2
0(h,
2);
printDnode2
0(h);
Locate2
0(h,
3);
printDnode2
0(h);
Locate2
0(h,
5);
printDnode2
0(h);
Locate2
0(h,
5);
printDnode2
0(h);
Locate2
0(h,
5);
printDnode2
0(h);
Locate2
0(h,
5);
printDnode2
0(h);
Locate2
0(h,
5);
printDnode2
0(h);
Locate2
0(h,
7);
printDnode2
0(h);
getchar();
getchar();
return 0;
}
第3章
3.3
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#define max 50;
typedef char type;
typedef struct
{
type data[
50];
int top;
}
stack;
void initialstack(
stack *s)
{
s->top =
0;
}
int push(
stack *s, type x)
{
if (s->top ==
50)
return -
1;
s->data[s->top++] = x;
return 0;
}
type pop(
stack *s)
{
if (s->top ==
0)
return -
2;
type x = s->data[--s->top];
return x;
}
void kuohaomatch1()
{
char s[
50];
int i =
0;
char d =
'a';
scanf_s(
"%c", &d);
while (d !=
' '&&i<
50)
{
s[i++] = d;
scanf_s(
"%c", &d);
}
s[i] =
'\0';
stack *s1 = (
stack *)
malloc(
sizeof(
stack));
stack *s2 = (
stack *)
malloc(
sizeof(
stack));
initialstack(s1);
initialstack(s2);
for (i =
0; s[i] !=
'\0'; i++)
switch (s[i])
{
case'{':push(s1,
'}');
break;
case'(':push(s1,
')');
break;
case'[':push(s1,
']');
break;
default:
if (pop(s1) != s[i])
printf(
"不匹配");
else
printf(
"匹配");
}
printf(
"匹配");
getchar();
getchar();
}
int count3()
{
int n, x;
stack *s1 = (
stack *)
malloc(
sizeof(
stack));
initialstack(s1);
scanf_s(
"%d %d",&n,&x);
if (n ==
0)
return 1;
else if (n ==
1)
return 2 * x;
else
{
for (
int i =
0; i <=n;i++)
{
if (i ==
0)
push(s1,
1);
else if (i ==
1)
push(s1,
2*x);
else
{
int tem1 = pop(s1);
int value =
2 * x*tem1 -
2 * (i -
1)*pop(s1);
printf(
"value:%d ", value);
push(s1, tem1);
push(s1, value);
}
}
}
return 2 * x*pop(s1) -
2 * (n -
1)*pop(s1);
}
第4章
4.3
``` #include “stdafx.h” #include