데이터 구조 기 말 복습 총화: 스 택 과 대기 열, 순환 대기 열의 정상 적 인 조작 코드
                                            
 5352 단어  데이터 구조 연습
                    
창고:
대기 열:
일반 대기 열:
순환 대기 열:
링크 대기 열
창고:
스 택 건설, 스 택 진입, 스 택 데이터 획득, 스 택 에서 나 오 는 요소 갯 수 와 스 택 의 총 용량 기본 작업 을 포함 합 니 다.기 말 데이터 구조의 학생 들 은 자신 이 쓰 는 것 을 볼 수 있다.
#include 
#include 
#include 
#include 
#define stackSize 4
#define stackIncrement 10
using namespace std;
typedef struct {
	int *base;    //     
	int *top;     //     
	int size;     //     
	int len;      //       
}stackk;
int Init(stackk &L)         //   ,   
{
	L.base = (int *)malloc(stackSize * sizeof(int));
	if(!L.base) return 0;
	L.size = 0;
	L.size += stackSize;
	L.top = L.base;
	L.len = 0;
	return 1;
}
int push(stackk &L, int e)   //  
{
	if(L.top - L.base >= L.size) 
	{
		int *newbase = (int *)realloc(L.base, (L.size + stackIncrement)*sizeof(int));
		if(!newbase) return 0;
		L.base = newbase;
		L.size += stackIncrement;
	}
	*L.top++ = e;
	L.len++;
	return 1;
}
int pop(stackk &L)     //  
{
	if(L.top == L.base) return 0;
	L.top--;
	L.len--;
	return 1;
}
int get(stackk &L, int &e)  ////      
{
	if(L.top == L.base) return 0;
	else  e = *(L.top-1);
	return 1;
}
int getlen(stackk &L) {          //        
	return L.len;
}
int getsize(stackk &L){       ///      
	return L.size;
}
int main()
{
	printf("    6          
");
	int i = 6;
	stackk S; if(!Init(S)) printf("     !!
");
	while(i--)
	{
		int y; cin >> y;
		if(!push(S,y)) printf("    !!
");
	};
	cout << "         " << getlen(S) << " "<< endl;
	cout << "      " << getsize(S) << endl;
	i = 6;
	while(i--)
	{
		int h;
		get(S, h);cout << h << " ";
		if(!pop(S)) printf("    !!
");
	}
	return 0;
}    대기 열:
일반 대기 열:
#include 
#include 
#include 
#include 
#define stackSize 10
#define stackIncrement 10
using namespace std;
typedef struct {
	int *base;          //      
	int *top;           //      
	int *head;          //      
	int size;            //     
	int len;             ///        
}stackk;
int Init(stackk &L)                //    ,   
{
	L.base = (int *)malloc(stackSize * sizeof(int));
	if(!L.base) return 0;
	L.size = 0;
	L.size += stackSize;
	L.top = L.head = L.base;
	L.len = 0;
	return 1;
}
int push(stackk &L, int e)        //  
{
	if(L.top - L.base >= L.size) 
	{
		cout << "    !!" << endl;
		return 0;
	}
	*L.top++ = e;
	L.len++;
	return 1;
}
int pop(stackk &L)            //   
{
	if(L.top == L.head) return 0;
	L.head++;
	L.len--;
	return 1;
}
int get(stackk &L, int &e)       //      
{
	if(L.top == L.head) return 0;
	else  e = *(L.head);
	//else  e = *(L.top-1);            
	return 1;
} 
int getlen(stackk &L) {             //         
	return L.len;
}
int getsize(stackk &L){            //       
	return L.size;
}
int main()
{
	printf("    6           
");
	int i = 6;
	stackk S; if(!Init(S)) printf("      !!
");
	while(i--)
	{
		int y; cin >> y;
		if(!push(S,y)) printf("    !!
");
	};
	cout << "          " << getlen(S) << " "<< endl;
	cout << "       " << getsize(S) << endl;
	i = 6;
	while(i--)
	{
		int h;
		get(S, h);cout << h << " ";
		if(!pop(S)) printf("     !!
");
	}
	return 0;
}    순환 대기 열:
#include 
#include 
#include 
#include 
#define queSize 10
#define queIncrement 10
using namespace std;
typedef struct {
	int *base;          //      
	int top;           //      
	int head;          //      
	int size;            //     
	int len;             ///        
}que;
int Init(que &L)                //    ,   
{
	L.base = (int *)malloc(queSize * sizeof(int));
	if(!L.base) return 0;
	L.size = 0;
	L.size += queSize;
	L.top = L.head = 0;
	L.len = 0;
	return 1;
}
int push(que &L, int e)        //  
{
	if((L.top + 1) % queSize == L.head){
		cout << "    !!" << endl;
		return 0;
	}
	else {
	L.base[L.top] = e;
	L.top = (L.top + 1) %queSize;
	L.len++;
	}
	return 1;
}
int pop(que &L)            //   
{
	if(L.top == L.head) return 0;
	L.head = (L.head + 1) % queSize;
	L.len--;
	return 1;
}
int get(que &L, int &e)       //      
{
	if(L.top == L.head) return 0;
	else  e = L.base[L.head];
	//else  e = L.base[L.top - 1];            
	return 1;
} 
int getlen(que &L) {             //         
	return L.len;
}
int getsize(que &L){            //       
	return L.size;
}
int main()
{
	printf("    6           
");
	int i = 6;
	que S; if(!Init(S)) printf("      !!
");
	while(i--)
	{
		int y; cin >> y;
		if(!push(S,y)) printf("    !!
");
	};
	cout << "          " << getlen(S) << " "<< endl;
	cout << "       " << getsize(S) << endl;
	i = 6;
	while(i--)
	{
		int h;
		get(S, h);cout << h << " ";
		if(!pop(S)) printf("     !!
");
	}
	return 0;
}
    링크 대기 열
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
mooc 절 빅 데이터 구조 PTA 연습 문제 의 최대 하위 열 과 문제 (온라인 처리)K 개의 정수 로 구 성 된 시퀀스 { N 1 , N 2 , ..., N K },"연속 하위 열" 은 {로 정의 되 었 습 니 다. "최대 하위 열 과" 는 모든 연속 하위 열 요소 의 합 중 최대 자로 정 의 됩...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.