6-1 Deque PTA_Data Structures and Algorithms (English)

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 Rear both 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

代码如下:

 

Deque CreateDeque()
{
	Deque q = (Deque)malloc(sizeof(struct DequeRecord));
	PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
	p->Last = NULL;		//单独队头没有前节点
	p->Next = NULL;		//单独队头没有后节点
	q->Front = q->Rear = p;		//队头和队尾指向同一节点
	return q;
}
int Push(ElementType X, Deque D)
{
	PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
	if (p == NULL)
		return 0;
	p->Element = X;
	PtrToNode t = D->Front->Next;
	if (t == NULL)		//t为空,即插入第一个节点,队尾需要改变指向
	{
		D->Front->Next = p;
		p->Last = D->Front;
		D->Rear = p;
		return 1;
	}
	else		//t不为空,插入头节点不需要改变队尾指向
	{
		p->Last = D->Front;
		D->Front->Next = p;
		p->Next = t;
		t->Last = p;
		return 1;
	}
}
ElementType Pop(Deque D)
{
	if (D->Front == D->Rear) return ERROR;
	PtrToNode t = D->Front->Next;
	ElementType e = t->Element;
	if (t->Next == NULL)		//t的下一节点为空,即删除队列中的最后一个元素,需要改变队尾指向
	{
		D->Front->Next = NULL;
		D->Front->Last = NULL;
		D->Rear = D->Front;
	}
	else		//t不为空,不需要改变队尾指向
	{
		D->Front->Next = t->Next;
		t->Next->Last = D->Front;
		free(t);
		return e;
	}
}
//尾插和尾出较为简单,不需要判断是否改变队尾和队头指向
int Inject(ElementType X, Deque D)
{
	PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
	if (p == NULL) return 0;
	p->Element = X;
	D->Rear->Next = p;
	p->Last = D->Rear;
	p->Next = NULL;
	D->Rear = p;
	return 1;
}
ElementType Eject(Deque D)
{
	if (D->Front == D->Rear) return ERROR;
	PtrToNode p = D->Rear;
	D->Rear = p->Last;
	p->Last->Next = NULL;
	ElementType e = p->Element;
	free(p);
	return e;
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Data Structures and Algorithms by Benjamin Baka English | 30 May 2017 | ASIN: B01IF7NLM8 | 310 Pages | AZW3 | 6.63 MB Key Features A step by step guide, which will provide you with a thorough discussion on the analysis and design of fundamental Python data structures. Get a better understanding of advanced Python concepts such as big-o notation, dynamic programming, and functional data structures. Explore illustrations to present data structures and algorithms, as well as their analysis, in a clear, visual manner. Book Description Data structures allow you to organize data in a particular way efficiently. They are critical to any problem, provide a complete solution, and act like reusable code. In this book, you will learn the essential Python data structures and the most common algorithms. With this easy-to-read book, you will be able to understand the power of linked lists, double linked lists, and circular linked lists. You will be able to create complex data structures such as graphs, stacks and queues. We will explore the application of binary searches and binary search trees. You will learn the common techniques and structures used in tasks such as preprocessing, modeling, and transforming data. We will also discuss how to organize your code in a manageable, consistent, and extendable way. The book will explore in detail sorting algorithms such as bubble sort, selection sort, insertion sort, and merge sort. By the end of the book, you will learn how to build components that are easy to understand, debug, and use in different applications. What you will learn Gain a solid understanding of Python data structures. Build sophisticated data applications. Understand the common programming patterns and algorithms used in Python data science. Write efficient robust code. About the Author Benjamin Baka works as a software developer and has over 10 years, experience in programming. He is a graduate of Kwame Nkrumah University of Science and Technology and a member of the Linux Accra User Group. Notable in his language toolset are C, C++, Java, Python, and Ruby. He has a huge interest in algorithms and finds them a good intellectual exercise. He is a technology strategist and software engineer at mPedigree Network, weaving together a dizzying array of technologies in combating counterfeiting activities, empowering consumers in Ghana, Nigeria, and Kenya to name a few. In his spare time, he enjoys playing the bass guitar and listening to silence. You can find him on his blog. Table of Contents Python objects, types and expressions Python data types and structures Principles of data structure design Lists and pointer structures Stacks and Queues Trees Hashing and symbol tables Graphs and other algorithms Searching Sorting Selction Algorithms Design Ttechniques and Sstrategies Implementations, applications and tools

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值