双向循环链表,顺序栈

今日课程:双向链表

                  双向循环链表

                  顺序栈

顺序栈的排序:主函数代码如下:

#include <stdio.h>
#include "SequenceStack.h"

int main()
{
	int i, num, tmp;
	Stack *s1, *s2;

	if (StackInit(&s1) != SUCCESS || StackInit(&s2) != SUCCESS)
	{
		printf("Init Failure!\n");
	}

	for (i = 0; i < SIZE; i++)
	{
		scanf("%d", &num);
		if (i == 0)
		{
			push(s1, num);
		}
		else
		{
			if (num >= GetTop(s1))
			{
				push(s1, num);
			}
			else
			{
				while (StackEmpty(s1) != TRUE && num < GetTop(s1))
				{
					tmp = GetTop(s1);	
					pop(s1);
					push(s2, tmp);
				}
				push(s1, num);
				while (StackEmpty(s2) != TRUE)
				{
					tmp = GetTop(s2);
					pop(s2);
					push(s1, tmp);
				}
			}
		}
	}

	for (i = 0; i < SIZE; i++)
	{
		printf("%d ", GetTop(s1));
		pop(s1);
	}
	return 0;
}

函数库如下:

#include"sestack.h"
#include<stdlib.h>

int stackinit(STACK **sa)
{
    if(*sa==NULL)
    {
        return FAILURE;
    }


    *sa = (STACK *)malloc(sizeof(STACK)*1);
    (*sa)->top = -1;
    (*sa)->date = (elemtype *)malloc(sizeof(elemtype)*size);
    return SUCESS;
}

int empty(STACK *sa)
{
    if(sa == NULL)
        return FAILURE;
    return(sa->top=-1)? TRUE:FALSE;
}

int push(STACK *sa, elemtype e)
{
    if(sa == NULL||sa->top>=9)
        return FAILURE;

    sa->date[sa->top+1]=e;
    sa->top++;

    return SUCESS;
}

int gettop(STACK *sa)
{
    if(sa == NULL)
        return FAILURE;
    
   

    return sa->date[sa->top];
}

int out(STACK *sa)
{
    if(sa->top==-1)        
        return FAILURE;

    
    sa->top--;

}

int clear(STACK *sa)
{
    if(sa==NULL)
        return FAILURE;

    sa->top = -1;
    return SUCESS;
}

int destory(STACK **sa)

{
    free((*sa)->date);
    free(*sa);
    *sa = NULL;
        return SUCESS;
}

猜你喜欢

转载自blog.csdn.net/qq_42719683/article/details/81461075
今日推荐