《数据结构与算法分析》学习笔记一:表、栈和队列

        学习编程快一年了,之前一直想有所学的东西写一些实用的小程序,也花了不少时间看python这样的语言,但是几星期前看到的一篇博文《我的算法学习之路》,很有感触,于是下定决心趁早开始算法的学习,按照他文章中的推荐,我买了《数据结构与算法分析》c语言描述一书 (豆瓣链接) 。按照看书的进度把所得所想与大家分享。

表、栈和队列

        抽象数据类型(abstract data type, ADT)是一些操作的集合,是数学的抽象。像C语言中整数、浮点数和布尔值是基本数据类型有与他们相关的操作一样,抽象数据类型也有与他们相关的操作,比如查找,测定大小等等。这些操作的实现只在程序中编写一次,任何部分需要该ADT执行的某一个操作时可以通过调用相应的函数来进行。

表ADT

        表处理的是形如a1, a2, a3, ... an 的数据,对表的操作有PrintList 和MakeEmpty; Find 返回关键字首次出现的位置;Insert和Delete从表中插入和删除某个关键字;FindKth返回某个位置上的元素。

表的简单数组实现

       所有对表的操作都可以通过动态数组实现。但是数组的大小需要已知,否则会浪费大量的空间,而且插入和删除操作的最坏情况都是O(N),平均来看,这两种操作也都需要线性时间,效率较低。所以一般不用数组实现。

链表

       链表有一系列不必再内存中连续的结构组成,每一个结构含有一个表元素和指向下一个结构的指针,称之为Next指针。


执行PrintList 或Find(L, Key) 时,我们只要传入该表第一个元素,然后用Next指针遍历。删除命令可以通过修改指针实现。插入命令需要使用一次malloc 分配新单元,然后再调整指针。

实现细节:

#ifndef _List_H
#define _List_H

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int ElementType;

List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position P,List L);
Position Find(ElementType X,List L);
void Delete(ElementType X,List L);
void Insert(ElementType X,List L, Position P);
void DeleteList(List L);

#endif

/* Place in the implementation file*/
struct Node
{
    ElementType Element;
    PtrToNode Next;
};


int IsEmpty(List L)
{
    return L->Next==NULL;
}

int IsLast(Position P,List L)
{
    return P->Next==NULL;
}

Position Find(ElementType X,List L)
{
    PtrToNode p;
    p=L->Next;
    while(p!=NULL&&p->Element!=X)
        p=p->Next;
    return p;
}

void Delete(ElementType X,List L)
{
    Position p;
    p=Find(X,L);
    if(!(IsLast(p,L)))    {
        PtrToNode temp=p->Next;
        p->Next=temp->Next;
        free(temp);
    }
}

void Insert(ElementType X,List L, Position P)
{
    Position temp;
    temp=malloc(sizeof(struct Node));
    if(temp!=NULL)
    {
        temp->Element=X;
        temp->Next=P->Next;
        P->Next=temp;
    }
}

void DeleteList(List L)
{
    Position p,temp;
    p=L->Next;
    L->Next=NULL;
    while(p!=NULL)
    {
        temp=p;
        p=p->Next;
        free(temp);
    }
}

栈ADT

        栈(stack) 是限制插入和删除智能在一个位置上进行的表,该位置是表的末端,叫做栈的顶(top),栈又被叫做LIFO(先进后出)表。对栈的基本操作有Push 和 Pop,前者相当于插入,后者相当于删除最后插入的元素。最后插入的元素可以用Top在Pop之前查看。

栈的链表实现

    通过在表顶部插入来实现Push,通过删除表顶元素来实现Pop,Top操作只是返回表顶端元素的值。

    实现代码:

#ifndef _Stack_H
#define _Stack_H

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef int ElementType;

int IsEmpty(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X,Stack S);
ElementType Top(Stack S);
void Pop(Stack S);

#endif  /* _Stack_H */

/* Place in the implementation file*/
struct Node
{
	ElementType Element;
	PtrToNode Next;
};


int IsEmpty(Stack S)
{
	return S->Next==NULL;
}

void MakeEmpty(Stack S)
{
	if(S!=NULL)
		while(!IsEmpty(S))
			Pop(S);
}

void Push(ElementType X,Stack S)
{
	PtrToNode temp;
	temp=(PtrToNode)malloc( sizeof(struct Node) );
	if(temp!=NULL) 
	{
		temp->Element=X;
		temp->Next=S->Next;
		S->Next=temp;
	}
}
ElementType Top(Stack S)
{
	if(!IsEmpty(S))
		return S->Next->Element;
}
void Pop(Stack S)
{
	PtrToNode temp;
	if(!IsEmpty(S))
	{
		temp=S->Next;
		S->Next=temp->Next;
		free(temp);
	}
}

栈的数组实现

        用数组实现栈避免了指针,这种方法的唯一潜在危险是我们需要提前声明一个数组的大小。一般来说,声明一个数组足够大并不至于浪费太多空间,如果栈的元素个数太大可以考虑用数组实现。用数组实现栈是简单的,每个占有一个TopOfStack记录元素的大小,没压入一个元素加1,弹出一个元素减1。这些操作都以常数时间运行,而影响栈的执行效率的问题是错误检测,像对空栈的Pop或者对满栈的Push都将应为数组越界都可能引起曾许崩溃。实现代码:

#ifndef _Stack_H
#define _Stack_H

struct StackRecord;
typedef struct StackRecord* Stack;
typedef int ElementType;

Stack CreateStack(int MaxElements);
int IsEmpty(Stack S);
int IsFull(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X,Stack S);
ElementType Top(Stack S);
void Pop(Stack S);

#endif   /* _Stack_H */

/* Place in implementation file */
#define EmptyTOS (-1)

struct StackRecord
{
	int Capacity;
	int TopOfStack;
	ElementType *Array;
};

Stack CreateStack(int MaxElements)
{
	Stack S;
	S=(StackRecord*)malloc(sizeof(struct StackRecord));
	S->Array=(ElementType*)malloc(sizeof(ElementType) * MaxElements);
	if(S->Array!=NULL) {
		S->Capacity=MaxElements;
		S->TopOfStack=EmptyTOS;
	}
}

int IsEmpty(Stack S)
{
	return S->TopOfStack==EmptyTOS;
}

void MakeEmpty(Stack S)
{
	S->TopOfStack=EmptyTOS;
}

void Push(ElementType X,Stack S)
{
	if(++S->TopOfStack<S->Capacity)
		S->Array[S->TopOfStack]=X;
}

ElementType Top(Stack S)
{
	if(!IsEmpty(S))
		return S->Array[S->TopOfStack];
	return 0;
}

void Pop(Stack S)
{
	if(!IsEmpty(S))
		--S->TopOfStack;
}

队列ADT

      和栈一样,队列(queue)也是表,使用队列时插入在一端进行而删除在另一端进行。队列的基本操作有Enqueue(入队),他是在表的末端插入一个元素,有Dequeue(出队),它是删除表开头 (front) 的元素。

队列的链表实现

       如同栈的情形一样,对于队列,任何表的实现都容易的(但是Enqueue不是O(1)时间内完成的)。实现代码:

#ifndef _Queue_H
#define _Queue_H

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Queue;
typedef int ElementType;

Queue CreateQueue();
int IsEmpty(Queue S);
void MakeEmpty(Queue S);
void Enqueue(ElementType X,Queue S);
ElementType Front(Queue S);
void Dequeue(Queue S);

#endif  /* _Queue_H */

/* Place in the implementation file*/
struct Node
{
	ElementType Element;
	PtrToNode Next;
};

Queue CreateQueue()
{
	Queue Q=(Queue)malloc(sizeof(Node));
	Q->Next=NULL;
}

int IsEmpty(Queue Q)
{
	return Q->Next==NULL;
}

void MakeEmpty(Queue Q)
{
	if(Q!=NULL)
		while(!IsEmpty(Q))
			Dequeue(Q);
}

void Enqueue(ElementType X,Queue Q)
{
	PtrToNode temp,ptr;
	temp=(PtrToNode)malloc( sizeof(struct Node) );
	if(temp!=NULL) 
	{
		temp->Element=X;
		temp->Next=NULL;
		ptr=Q->Next;
		while(ptr!=NULL)
			ptr=ptr->Next;
		ptr=temp;
	}
}
ElementType front(Queue S)
{
	if(!IsEmpty(S))
		return S->Next->Element;
}
void Dequeue(Queue Q)
{
	PtrToNode temp;
	if(!IsEmpty(Q))
	{
		temp=Q->Next;
		Q->Next=temp->Next;
		free(temp);
	}
}

队列的数组实现

       对于每一个队列数据结构,我们保留一个数组Queue[ ] 以及位置Font 和Rear ,他们记录队列的两端,我们还记录实际存在队列中的元素个数Size。当Front 或Rear到达数组的尾端时,又让他有绕回开头。实现代码:

#ifndef _Queue_H
#define _Queue_H

struct Node;
struct QueueRecord;
typedef struct Node *PtrToNode;
typedef QueueRecord* Queue;
typedef int ElementType;

Queue CreateQueue(int MaxElements);
int IsEmpty(Queue S);
int IsFull(Queue S);
void MakeEmpty(Queue S);
void Enqueue(ElementType X,Queue S);
ElementType Front(Queue S);
void Dequeue(Queue S);

#endif  /* _Queue_H */

/* Place in the implementation file*/

struct QueueRecord
{
	int Capacity;
	int Front;
	int Rear;
	int Size;
	ElementType *Array;	
};

Queue CreateQueue(int MaxElements)
{
	Queue Q;
	Q->Size=0;
	Q->Rear=-1;
	Q->Front=0;
	Q->Capacity=MaxElements;
	return Q;
}

int IsEmpty(Queue Q)
{
	return Q->Size==0;
}

void MakeEmpty(Queue Q)
{
	Q->Size==0;
	Q->Front=0;
	Q->Rear=-1;
}

void Enqueue(ElementType X,Queue Q)
{
	if(!IsFull(Q)) {
		Q->Size++;
		if(++Q->Rear ==Q->Capacity)
			Q->Rear=0;
		Q->Array[Q->Rear]=X;
	}
}

ElementType front(Queue S)
{
	if(!IsEmpty(S))
		return S->Array[S->Front];
}
void Dequeue(Queue Q)
{
	PtrToNode temp;
	if(!IsEmpty(Q))
	{
		if(++Q->Front ==Q->Capacity)
			Q->Front=0;
	}
}

        表、栈和队列这三种最基本的数据结构到此就整理完了。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值