出栈序列指定元素先输出,但是数组未能成功赋值

有5个元素,其入栈顺序为:A、B、C、D、E,在整个可能的出栈序列中,以元素C为第1个、D为第2个出栈的序列的函数中,无法正确输出。请大家帮忙看看指点一下~感谢Thanks♪(・ω・)ノ
这是我用的第一个办法,也是不行

img


这是我用的第一个办法,也是不行
入栈调试是正常的,入栈所有函数都能正常输出原序列,但是该排序函数中,数组的值一直是0;不知道哪一步出问题了

img

img

太乱了,重新写一个

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void swap(char *a, char *b) {
    char temp = *a;
    *a = *b;
    *b = temp;
}

void reverse(char *arr, int start, int end) {
    while (start < end) {
        swap(&arr[start], &arr[end]);
        start++;
        end--;
    }
}

void enumerate(char *arr, int n) {
    int count = 0;
    do {
        if (arr[2] == 'C' && arr[3] == 'D') {
            count++;
            printf("%d: ", count);
            for (int i = 0; i < n; i++) {
                printf("%c ", arr[i]);
            }
            printf("\n");
        }
    } while (next_permutation(arr, arr + n));
}

int main() {
    char arr[] = {'A', 'B', 'C', 'D', 'E'};
    int n = sizeof(arr) / sizeof(char);

    enumerate(arr, n);

    return 0;
}


  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/732377
  • 你也可以参考下这篇文章:【C语言】给一个任意一数字,1.求出它是几位数 2.按逆序数输出 3.分别输出每一位数字;
  • 除此之外, 这篇博客: 假设以两个元素依值递增有序排列的顺序表A和B 分别表示两个集合(同一表中的元素值各不相同),现要求另辟空间构成一个顺序表C,其元素为A和B元素的交集,且表C中的元素也是依值递增有序排列。中的 假设以两个元素依值递增有序排列的顺序表A和B 分别表示两个集合(同一表中的元素值各不相同),现要求另辟空间构成一个顺序表C,其元素为A和B元素的交集,且表C中的元素也是依值递增有序排列。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • # include <stdio.h>
    # include <stdlib.h>
    # define initsize 20//初始分配量
    # define LISTINCREMENT 5//分配增量
    typedef int ElemType;
    typedef struct
    {
    	ElemType * elem;
    	int length;
    	int listsize;
    }SqList;
    void IntiList(SqList &L,int n)//建立一个表长为n的顺序表
    {
    	ElemType *p;
    	L.elem = (ElemType *)malloc(initsize*sizeof(ElemType));
    	if(!L.elem)
    	{
    		printf("分配失败");
    	}
    	printf("请依次输入顺序表的元素(数据元素依值递增):\n");
    	for(p = L.elem;p < L.elem+n;p++)
    	{
    		scanf("%d",p);
    	}
    	L.length = n;
    	L.listsize = initsize;
    }
    void PrintList(SqList L)//输出顺序表
    {
    	ElemType *p;
    	printf("输出顺序表中的元素\n");
    	for(p=L.elem;p<L.elem+L.length;p++)
    	{
    		printf("%d\t",*p);
    	}
    	printf("\n");
    }
    void IntiList_NULL(SqList &L)//建立一个空的顺序表
    {
    	L.elem = (ElemType *)malloc(initsize*sizeof(ElemType));
    	if(!L.elem)
    	{
    		printf("分配失败");
    	}
    	L.length = 0;
    	L.listsize = initsize;
    }
    void ListInsert(SqList &L,int i,ElemType e)//在顺序表中第i个位置插入元素e
    {
    	ElemType *p,*q,*newbase;
    	q=L.elem+i-1;//q为插入位置
    	if(i<1||i>L.length+1)//i值不合法
    	{
    		printf("插入元素位置不合法\n");
    	}
    	if(L.length>=L.listsize)//当前储存空间已满,增加分配
    	{
    		newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
    		if(!newbase)
    		{
    			printf("分配失败");
    		}
    		L.elem=newbase;
    		L.listsize+=LISTINCREMENT;
    	}
    	for(p=L.elem+L.length-1;p>=q;p--)//插入位置及之后元素右移
    	{
    		*(p+1)=*p;
    	}
    	*q=e;
    	++L.length;
    }
    void find_intersection(SqList La,SqList Lb,SqList &Lc)//找出顺序表La和Lb中元素的交集,并按依值递增的顺序放入顺序表Lc中
    {
    	int i=0;
    	ElemType *pa,*pb;
    	for(pa=La.elem;pa<La.elem+La.length;pa++)
    	{
    		for(pb=Lb.elem;pb<Lb.elem+Lb.length;pb++)
    		{
    			if(*pb==*pa)
    			{
    				i++;
    				ListInsert(Lc,i,*pa);
    			}
    		}
    	}
    }
    int main()
    {
    	int n1,n2;
    	SqList La,Lb,Lc;
    	printf("请输入要建立的顺序表La的长度n1:\n");
    	scanf("%d",&n1);
    	printf("顺序表La\n");
    	IntiList(La,n1);
    	printf("请输入要建立的顺序表Lb的长度n2:\n");
    	scanf("%d",&n2);
    	printf("顺序表Lb\n");
    	IntiList(Lb,n2);
    	IntiList_NULL(Lc);
    	find_intersection(La,Lb,Lc);
    	PrintList(Lc);
    	return 0;
    }