王道考研数据结构与算法编程题题目解答

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vivi_and_qiao/article/details/79868412

请注意函数名为此题的大致意思,函数名后的数字为该章节编程题的序号,请注意以序号为准
——页面长请使用左侧目录

第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-&gt;<span class="hljs-typedef"><span class="hljs-keyword">data</span>[i]&gt;=a-&gt;len||a-&gt;<span class="hljs-keyword">data</span>[i]&lt;0)</span>
    return -<span class="hljs-number">2</span>;
    <span class="hljs-keyword">else</span>
        t[a-&gt;<span class="hljs-typedef"><span class="hljs-keyword">data</span>[i]]++;</span>
}
for(int i=<span class="hljs-number">0</span>;i&lt;a-&gt;len;i++) 
  <span class="hljs-keyword">if</span>(t[i]&gt;a-&gt;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-&gt;len-<span class="hljs-number">1</span>);
 reverse10(a,<span class="hljs-number">0</span>,a-&gt;len-p);
 reverse10(a,a-&gt;len-p+<span class="hljs-number">1</span>,a-&gt;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[–i];
}
else
{
if(j++b->len||ma->len)
return a->data[–j];
}
}
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",&sql1->data[i]);//顺序表赋值 
    for(int i=0;i<sql->len;i++)
      scanf("%d",&sql->data[i]);//此处应该注意scanf()函数里若加入空格等空白符会使输入多一次,原因复杂 
//  mindel(sql,&value);
//  printf("最小值为 %d\n",value);
//  for(int i=0;i<10;i++)
//  printf(" %d ",sql->data[i]);
//  nizhi(sql); 
//      printf("":%d\n");
//      delsameele(sql,5);
//      delsame6(sql);
//   combine7(sql,sql1,sql2)  ;
// change_8(sql,4);
//      insert9(sql,1);
//     leftmove10(sql,3);

//  for(int i=0;i<sql->len;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("\n");
    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:\n");
        print(a);
        //  printf("%d  1当前p的data\n",p->data); 
        while (p->next != NULL&&p->next->data <= q->data)
        {
            p = p->next;
            //printf("%d  2当前p的data\n",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->data<p->next->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\n", 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\n", 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\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;
        /*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:\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);
    //       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<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;
}
//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

猜你喜欢

转载自blog.csdn.net/vivi_and_qiao/article/details/79868412