王道の大学院受験のデータの構造とアルゴリズムのプログラミングの問題の解答
89718 ワード
関数名はこの問題の大まかな意味に注意してください.関数名の後の数字はこの章のプログラミング問題のシーケンス番号です.シーケンス番号に準じてください.ページ長は左側のディレクトリを使用してください.
第2章
2.2
} 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(jlen&&klen) { 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(klen) c->data[i++]=b->data[k++]; else while(jlen) c->data[i++]=a->data[j++]; return 0; } }
} 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+1];a->data[mid]=a->data[mid]-a->data[mid+1];}if(low>high){for(int i=a->len-1;i–)a->data[i+1]=a->data[i]=a->data[i]=a->data[high+ 1]=x;a->len+;}return 0;}void reverse 10(sqList*a,*a,*a,*a,{a,{a,{a,{a]]intm,int n)/配列中の要素を下から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) {
点midpos 11(sqList*a,sqList*b){if(a->len!=b->len|!isposorder(a)|!isposorder(b))return-1;////2つの昇順配列を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-dat> a[i]]]if(a-dat> a[i]])if(a-data[i]]a-data[i]]a[i<=b->data[j]){if(i++a->len||ma->len)returna->data[–i];}else{if(j++b->len||ma->len)returna->data[–j];}}int mainele 12(sqList*a){////元の配列の長さに一致する空間が必要であり、そのi号は元の配列の中でiの値の要素個数type t[a->len];for(int i=0;ilen;i++)t[i]=0;for(int i=0;ilen;i++){if(a->data[i]=a->len|n|n|n>=a->len|n|n|n|n|returna->returna[->data[–a|a->data[i]<0)return-2;else t[a->data[i]+;} for(int i=0;ilen;i++) if(t[i]>a->len/2) return i; return -1; }
2.2試験用例
2.3
2.3試験用例
第3章
3.3
第4章
4.3 ``` #include “stdafx.h” #include
第2章
2.2
#include
#include
#include
#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;ilen;i++)
{
if((a->data)[i]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;ilen;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;ilen;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;ilen;i++)
if(a->data[i]>min&&a->data[i]
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;ilen;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;ilen;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;ilen;i++)
if(a->data[i]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(jlen&&klen) { 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(klen) c->data[i++]=b->data[k++]; else while(jlen) c->data[i++]=a->data[j++]; return 0; } }
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;
} 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+1];a->data[mid]=a->data[mid]-a->data[mid+1];}if(low>high){for(int i=a->len-1;i–)a->data[i+1]=a->data[i]=a->data[i]=a->data[high+ 1]=x;a->len+;}return 0;}void reverse 10(sqList*a,*a,*a,*a,{a,{a,{a,{a]]intm,int n)/配列中の要素を下から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) {
// 8 , O(n) , O(1)
reverse10(a,0,a->len-1);
reverse10(a,0,a->len-p);
reverse10(a,a->len-p+1,a->len-1);
return 0;
点midpos 11(sqList*a,sqList*b){if(a->len!=b->len|!isposorder(a)|!isposorder(b))return-1;////2つの昇順配列を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-dat> a[i]]]if(a-dat> a[i]])if(a-data[i]]a-data[i]]a[i<=b->data[j]){if(i++a->len||ma->len)returna->data[–i];}else{if(j++b->len||ma->len)returna->data[–j];}}int mainele 12(sqList*a){////元の配列の長さに一致する空間が必要であり、そのi号は元の配列の中でiの値の要素個数type t[a->len];for(int i=0;ilen;i++)t[i]=0;for(int i=0;ilen;i++){if(a->data[i]=a->len|n|n|n>=a->len|n|n|n|n|returna->returna[->data[–a|a->data[i]<0)return-2;else t[a->data[i]+;} for(int i=0;ilen;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;ilen;i++)
// scanf("%d",&sql1->data[i]);//
for(int i=0;ilen;i++)
scanf("%d",&sql->data[i]);// scanf() ,
// mindel(sql,&value);
// printf(" %d
",value);
// for(int i=0;i<10;i++)
// printf(" %d ",sql->data[i]);
// nizhi(sql);
// printf("":%d
");
// delsameele(sql,5);
// delsame6(sql);
// combine7(sql,sql1,sql2) ;
// change_8(sql,4);
// insert9(sql,1);
// leftmove10(sql,3);
// for(int i=0;ilen;i++)
// printf("%d ",sql->data[i]);
// printf("%d",midpos11(sql,sql1));
printf("%d",mainele12(sql));
return 0;
}
2.3
// .cpp : 。
//
#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("
");
getchar();
}
void difference(node *a, node *b)// , a-b
{
node *q = b;
node *p;
node *pre, *r;
pre = a;
p = a->next;
if (!p)
return;
while (p)
{
//printf(" 3");
q = b->next;
while (q != NULL&&p->data != q->data)
{
// printf(" 1");
q = q->next;
}
if (q != NULL)
{
// printf(" 2");
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:
");
print(a);
// printf("%d 1 p data
",p->data);
while (p->next != NULL&&p->next->data <= q->data)
{
p = p->next;
//printf("%d 2 p data
",p->data);
}
// printf("chuchuchu");
tem = q;
//printf("%d 2 q ",tem->data);
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);
// printf(" 3");
}
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;
// print(h);
}
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;
// print(h);
}
p = h->next;
}
//
p = h;
q = h->next;
while (q)
{
printf("%d ", q->data);
p = q; q = q->next;
free(p);
}
free(h);
// , o(n^2) o(1);
// : ,
// node *p,*q,*tem;
// while(h->next){
// p=h;q=p->next;
// while(q->next)
// {
// if(q->next->datanext->data)
// p=q;
// else
// q=q->next;
// }
// printf("%d ",p->next->data);
// tem=p->next;
// p->next=p->next->next;
// free(tem)
//}
// 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)// h1,h2 h1
{
// 。 ,
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;
//printf("%d
", tem->data);
p = p->next;
tem->next = t;
h->next = tem;
t = tem;
}
if ((!p&&q)||(q&&p->data>q->data))// &&q, if p p,q ,while ,
{
node *tem = q;
//printf("%d
", tem->data);
q = q->next;
tem->next = t;
h->next = tem;
t = tem;
}
}
return h;
}
else
return NULL;
}
int comele15(node *h1, node *h2)
{
// h1 , h2 , h1 ,
// , h1,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)// p p->nex->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)// ,
{ // , , rnext ,
// , , rnext
// h lnext rnext rnext rnext; 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;// head
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)
{// h2 h2 h1
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
", 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;
/*q->rnext->lnext = m;
p->rnext = m->rnext;
m->lnext = q;
if (m->rnext)
m->rnext->lnext = p;
m->rnext = q->rnext;
q->rnext = m;*/
}
void printDnode20(Dnode20 *h);//
Dnode20 *createDnode20link()// 20
{
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:
");
// print(h1);
// printf("h2:
");
// print(h2);
// printf("3 h1 %d ,3 h2 %d
", select(h1, 3), select(h2, 3));
// h1 = del(h1, 3);
// h2 = del(h2, 3);
// printf(" 3 h1:
");
// print(h1);
// printf(" 3 h2:
");
// print(h2);
// printf(" h1 4 10, h2 4 10:
");
// h1 = insert(h1, 4, 10, 0);
// printf("h1:
");
// print(h1);
// h2 = insert(h2, 4, 10, 1);
// printf("h2:
");
// print(h2);
// printf(" h1 :
");
// reverse(h1);
// print(h1);
// printf(" h2 :
");
// pur_linklist(h2);
// print(h2);
// printf(" h1-h2 h1:
");
// difference(h1, h2);
// print(h1);
// printf("h1 :
");
// bubblesort1(h1);
// print(h1);
// printf("h1 :
");
// bubblesort2(h1);
// print(h1);
// printf("h2 :
");
// bubblesort2(h2);
// print(h2);
// printf("h1 h2 :
");
// linkcombine(h1,h2);
//recursiondel1(h2->next,3);
// r_print(h2->next);
// delminest4(h2);
// delvalue7(h2,2,5);
// posprint9(h2);
// resolve10(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);
Dnode20 *h=createDnode20link();
Locate20(h, 1);
printDnode20(h);
Locate20(h, 2);
printDnode20(h);
Locate20(h, 3);
printDnode20(h);
Locate20(h, 5);
printDnode20(h);
Locate20(h, 5);
printDnode20(h);
Locate20(h, 5);
printDnode20(h);
Locate20(h, 5);
printDnode20(h);
Locate20(h, 5);
printDnode20(h);
Locate20(h, 7);
printDnode20(h);
getchar();
getchar();
return 0;
}
第3章
3.3
// 3.3.cpp : 。
//
#include "stdafx.h"
#include
#include
#include
#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;
}
//3.36
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()
{// : P0,P1 P2 , P1,p2 P3 ,
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);// i=2 , P0,P1 , P2 , P1,P2 P3
printf("value:%d ", value); // , ;
push(s1, tem1);
push(s1, value);
}
}
}
return 2 * x*pop(s1) - 2 * (n - 1)*pop(s1);// Pn-1 Pn-2
}
第4章
4.3 ``` #include “stdafx.h” #include