C语言指针1002无标题,表、栈、队列的C语言实现(指针实现)

数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同高效的检索算法和索引技术有关。

希望大家喜欢,点赞哦

链表的实现:

链表list.h 接口头文件

/* list.h -- 简单列表类型的头文件 */

#ifndef LIST_H_

#define LIST_H_

#include /* C99 特性 */

//需要保存的数据类型

struct data_type

{

char c;

int i;

//写出想要表示的数据即可。。。

}

//一般类型定义

typedef struct data_type Item;

/*在链表的实现中,每一个链接被称为一个节点(node)。每个节点包含形成列表内容的信息和指向下一节点的指针*/

typedef struct node

{

Item item;

struct node * next;

} Node;

//为了管理列表,需要一个指向其开始处的指针

typedef Node * List;

/* 函数原型 */

/* 操作:初始化一个列表: */

void InitializeList(List * plist);

/* 操作:确定列表是否为空列表 */

bool ListIsEmpty(const List *plist);

/* 操作:确定列表是否已满 */

bool ListIsFull(const List *plist);

/* 操作:确定列表中项目的个数 */

unsigned int ListItemCount(const List *plist);

/* 操作:在列表尾部添加一个项目 */

bool AddItem(Item item, List * plist);

/* 操作:把一个函数作用于列表的每个项目 */

void Traverse (const List *plist, void (* pfun)(Item item) );

/* 操作:释放已分配的内存(如果有) */

void EmptyTheList(List * plist);

#endif

list.c 实现文件

/* list.c -- 支持列表操作的功能 */

#include

#include

#include "list.h"

/* 局部函数原型 */

static void CopyToNode(Item item, Node * pnode);

/* 接口函数 */

/* 初始化一个列表 */

void InitializeList(List * plist)

{

* plist = NULL;

}

/* 如果列表为空则返回真 */

bool ListIsEmpty(const List * plist)

{

if (*plist == NULL)

return true;

else

return false;

}

/* 如果列表已满则返回真 */

bool ListIsFull(const List * plist)

{

Node * pt;

bool full;

pt = (Node *) malloc(sizeof(Node));

if (pt == NULL)

full = true;

else

full = false;

free(pt);

return full;

}

/* 返回节点数 */

unsigned int ListItemCount(const List * plist)

{

unsigned int count = 0;

Node * pnode = *plist; /* set to start of list */

while (pnode != NULL)

{

++count;

pnode = pnode->next; /* set to next node */

}

return count;

}

/* 创建存放项目的节点,并把它添加到有plist指向的列表尾部 */

bool AddItem(Item item, List * plist)

{

Node * pnew;

Node * scan = *plist;

pnew = (Node *) malloc(sizeof(Node));

if (pnew == NULL)

return false; /* quit function on failure */

CopyToNode(item, pnew);

pnew->next = NULL;

if (scan == NULL) /* empty list, so place */

*plist = pnew; /* pnew at head of list */

else

{

while (scan->next != NULL)

scan = scan->next; /* find end of list */

scan->next = pnew; /* add pnew to end */

}

return true;

}

/* 访问每个节点并对他们分别执行由pfun指向的函数 */

void Traverse (const List * plist, void (* pfun)(Item item) )

{

Node * pnode = *plist; /* set to start of list */

while (pnode != NULL)

{

(*pfun)(pnode->item); /* apply function to item */

pnode = pnode->next; /* advance to next item */

}

}

/* 释放由malloc()分配的内存,把列表指针设置为NULL */

void EmptyTheList(List * plist)

{

Node * psave;

while (*plist != NULL)

{

psave = (*plist)->next; /* save address of next node */

free(*plist); /* free current node */

*plist = psave; /* advance to next node */

}

}

/* 局部函数定义 */

/* 把一个项目复制到节点中 */

static void CopyToNode(Item item, Node * pnode)

{

pnode->item = item; /* structure copy */

}

栈的实现:

栈 stack.h 接口头文件

#ifndef STACK_H_

#define STACK_H_

struct Node;

typedef struct Node *PtrToNode;

typedef PtrToNode Stack;

int IsEmpty( Stack S );

Stack CreateStack( void );

void DisposeStack( Stack S );

void MakeEmpty( Stack S );

void Push( ElementType X, Stack S );

ElementType Top( Stack S );

void Pop( Stack S );

#endif

stack.c 实现文件

#include "stack.h"

#include "fatal.h"

struct Node

{

ElementType Element;

PtrToNode Next;

}

int IsEmpty( Stack S )

{

return S->Next ==NULL;

}

Stack CreateStack( void )

{

Stack S;

S=malloc (sizeof(struct Node));

if(S==NULL)

FatalError( "Out of space!!!" );

S->Next = NULL;

MakeEmpty(S);

return S;

}

void MakeEmpty( Stack S )

{

if (S==NULL)

Error( "Must use CreateStack first" );

else

while(!IsEmpty(S))

Pop(S);

}

void Stack (Stack S)

{

MakeEmpty(S);

free(S);

}

void Push( ElementType X, Stack S )

{

PtrToNode TmpCell;

TmpCell = malloc( sizeof( struct Node ) );

if( TmpCell == NULL )

FatalError( "Out of space!!!" );

else

{

TmpCell->Element = X;

TmpCell->Next = S->Next;

S->Next = TmpCell;

}

}

ElementType Top( Stack S )

{

if( !IsEmpty( S ) )

return S->Next->Element;

Error( "Empty stack" );

return 0; /* Return value used to avoid warning */

}

void Pop( Stack S )

{

PtrToNode FirstCell;

if( IsEmpty( S ) )

Error( "Empty stack" );

else

{

FirstCell = S->Next;

S->Next = S->Next->Next;

free( FirstCell );

}

}

队列的实现:

队列queue.h 接口头文件

typedef int ElementType;

/* START: fig3_57.txt */

#ifndef _Queue_h

#define _Queue_h

struct QueueRecord;

typedef struct QueueRecord *Queue;

int IsEmpty( Queue Q );

int IsFull( Queue Q );

Queue CreateQueue( int MaxElements );

void DisposeQueue( Queue Q );

void MakeEmpty( Queue Q );

void Enqueue( ElementType X, Queue Q );

ElementType Front( Queue Q );

void Dequeue( Queue Q );

ElementType FrontAndDequeue( Queue Q );

#endif /* _Queue_h */

/* END */

queue.c 实现文件

#include "queue.h"

#include "fatal.h"

#include

#define MinQueueSize ( 5 )

struct QueueRecord

{

int Capacity;

int Front;

int Rear;

int Size;

ElementType *Array;

};

/* START: fig3_58.txt */

int

IsEmpty( Queue Q )

{

return Q->Size == 0;

}

/* END */

int

IsFull( Queue Q )

{

return Q->Size == Q->Capacity;

}

Queue

CreateQueue( int MaxElements )

{

Queue Q;

/* 1*/ if( MaxElements < MinQueueSize )

/* 2*/ Error( "Queue size is too small" );

/* 3*/ Q = malloc( sizeof( struct QueueRecord ) );

/* 4*/ if( Q == NULL )

/* 5*/ FatalError( "Out of space!!!" );

/* 6*/ Q->Array = malloc( sizeof( ElementType ) * MaxElements );

/* 7*/ if( Q->Array == NULL )

/* 8*/ FatalError( "Out of space!!!" );

/* 9*/ Q->Capacity = MaxElements;

/*10*/ MakeEmpty( Q );

/*11*/ return Q;

}

/* START: fig3_59.txt */

void

MakeEmpty( Queue Q )

{

Q->Size = 0;

Q->Front = 1;

Q->Rear = 0;

}

/* END */

void

DisposeQueue( Queue Q )

{

if( Q != NULL )

{

free( Q->Array );

free( Q );

}

}

/* START: fig3_60.txt */

static int

Succ( int Value, Queue Q )

{

if( ++Value == Q->Capacity )

Value = 0;

return Value;

}

void

Enqueue( ElementType X, Queue Q )

{

if( IsFull( Q ) )

Error( "Full queue" );

else

{

Q->Size++;

Q->Rear = Succ( Q->Rear, Q );

Q->Array[ Q->Rear ] = X;

}

}

/* END */

ElementType

Front( Queue Q )

{

if( !IsEmpty( Q ) )

return Q->Array[ Q->Front ];

Error( "Empty queue" );

return 0; /* Return value used to avoid warning */

}

void

Dequeue( Queue Q )

{

if( IsEmpty( Q ) )

Error( "Empty queue" );

else

{

Q->Size--;

Q->Front = Succ( Q->Front, Q );

}

}

ElementType

FrontAndDequeue( Queue Q )

{

ElementType X = 0;

if( IsEmpty( Q ) )

Error( "Empty queue" );

else

{

Q->Size--;

X = Q->Array[ Q->Front ];

Q->Front = Succ( Q->Front, Q );

}

return X;

}

希望大家喜欢,点赞哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值