【Data Structures and Algorithms】6-1 Deque(25 分)

A "deque" is a data structure consisting of a list of items, on which the following operations are possible:

  • Push(X,D): Insert item X on the front end of deque D.
  • Pop(D): Remove the front item from deque D and return it.
  • Inject(X,D): Insert item X on the rear end of deque D.
  • Eject(D): Remove the rear item from deque D and return it. Write routines to support the deque that take O(1) time per operation.

Format of functions:

Deque CreateDeque();
int Push( ElementType X, Deque D );
ElementType Pop( Deque D );
int Inject( ElementType X, Deque D );
ElementType Eject( Deque D );

where Deque is defined as the following:

typedef struct Node *PtrToNode;
struct Node {
    ElementType Element;
    PtrToNode Next, Last;
};
typedef struct DequeRecord *Deque;
struct DequeRecord {
    PtrToNode Front, Rear;
};

Here the deque is implemented by a doubly linked list with a header. Front and Rear point to the two ends of the deque respectively. Front always points to the header. The deque is empty when Front and Rearboth point to the same dummy header. Note: Push and Inject are supposed to return 1 if the operations can be done successfully, or 0 if fail. If the deque is empty, Pop and Eject must return ERROR which is defined by the judge program.

Sample program of judge:

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

#define ElementType int
#define ERROR 1e5
typedef enum { push, pop, inject, eject, end } Operation;

typedef struct Node *PtrToNode;
struct Node {
    ElementType Element;
    PtrToNode Next, Last;
};
typedef struct DequeRecord *Deque;
struct DequeRecord {
    PtrToNode Front, Rear;
};
Deque CreateDeque();
int Push( ElementType X, Deque D );
ElementType Pop( Deque D );
int Inject( ElementType X, Deque D );
ElementType Eject( Deque D );

Operation GetOp();          /* details omitted */
void PrintDeque( Deque D ); /* details omitted */

int main()
{
    ElementType X;
    Deque D;
    int done = 0;

    D = CreateDeque();
    while (!done) {
        switch(GetOp()) {
        case push: 
            scanf("%d", &X);
            if (!Push(X, D)) printf("Memory is Full!\n");
            break;
        case pop:
            X = Pop(D);
            if ( X==ERROR ) printf("Deque is Empty!\n");
            break;
        case inject: 
            scanf("%d", &X);
            if (!Inject(X, D)) printf("Memory is Full!\n");
            break;
        case eject:
            X = Eject(D);
            if ( X==ERROR ) printf("Deque is Empty!\n");
            break;
        case end:
            PrintDeque(D);
            done = 1;
            break;
        }
    }
    return 0;
}

/* Your function will be put here */

Sample Input:

Pop
Inject 1
Pop
Eject
Push 1
Push 2
Eject
Inject 3
End

Sample Output:

Deque is Empty!
Deque is Empty!
Inside Deque: 2 3

思路:

这题重点是要弄清楚这个双向链表的结构,就是它是如何存储的

     

当链表为空时是这样的

链表中有一个数据

链表中有两个(及以上)的数据

每次插入和删除的时候都要考虑是哪一种情况,以及D->Front->Next、D->Rear、D->Rear->Next、D->Rear->Last如何变化

CreateDeque函数分配空间时要注意,不能漏了 PtrToNode Node=(PtrToNode)malloc( sizeof(struct Node) );

Deque CreateDeque() {
	PtrToNode Node=(PtrToNode)malloc( sizeof(struct Node) );   // 1
	Deque D=(Deque)malloc( sizeof(struct DequeRecord) );       // 2
	Node->Next=Node->Last=NULL;
	D->Rear=D->Front=Node;
	
	return D;
}
int Push( ElementType X, Deque D ) {
	PtrToNode a=(PtrToNode)malloc( sizeof(struct Node) );
	if(a) {
		a->Element=X;
		a->Last=D->Front;
		a->Next=D->Front->Next;
		if(D->Front->Next)
			D->Front->Next->Last=a;
		else
			D->Rear=a;
		D->Front->Next=a;
		return 1;
	}
	else return 0;
}
ElementType Pop( Deque D ) {
	ElementType val;
	PtrToNode a=D->Front->Next;
	if(a) {
		val=a->Element;
		if(a==D->Rear) {
			D->Front->Next=NULL;
			D->Rear=D->Front;
		}
		else {
			D->Front->Next->Next->Last=D->Front;
			D->Front->Next=D->Front->Next->Next;
		}
		free(a);	
		return val;
	}
	else 
		return ERROR;
	
}
int Inject( ElementType X, Deque D ) {
	PtrToNode a=(PtrToNode)malloc( sizeof(struct Node) );
	if(a) {
		a->Element=X;
		a->Next=NULL;
		if(D->Front==D->Rear) {
			D->Front->Next=a;
			a->Last=D->Front;
		}
		else {
			D->Rear->Next=a;
			a->Last=D->Rear;	
		}
		D->Rear=a;
		return 1;
	}
	else 
		return 0;
}
ElementType Eject( Deque D ) {
	ElementType val;
	if(D->Front==D->Rear)
		return ERROR;
	else {
		val=D->Rear->Element;
		if(D->Front->Next==D->Rear) {
			free(D->Rear);
			D->Front->Next=NULL;
			D->Rear=D->Front;
		}
		else {
			D->Rear=D->Rear->Last;
			free(D->Rear->Next);
			D->Rear->Next=NULL;
		}
		return val;
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值