链式堆栈_C语言实现

链式栈具有存储灵活,没存利用率高的特点,今天采用c语言实现链式堆栈。栈的主体采用单链表的方式实现,链表不带头结点。为了方便知道栈的栈底和栈顶,需要定义两个分别指向链表的头和尾的指针top,base。链表从头部生长,出栈也从头部出,这样实现起来比较简单。
进栈出栈的图示如下:
链式栈
链表的数据结构:


#define OK 0
#define ERROR 1
typedef int SElemType;
/*声明栈的数据结构*/
typedef struct node
{
   SElemType data;
   struct node *next;
}LsNode;
typedef struct
{
   LsNode *base;//栈底指针
   LsNode *top;//栈顶指针
   int stackSize;//栈的当前容量
}LStack;

功能函数:

#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
#include<malloc.h>
/*功能:创建一个栈
  返回:返回指向栈的指针
  */
LStack *Create_LsStack()
{
    LStack *S;
	S=(LStack *)malloc(sizeof(LStack));
	S->top=S->base=(LsNode *)malloc(sizeof(LsNode));
	if(!S->base)
	{
	   printf("no memory aviable\n");
	   exit(0);
	}
	S->base->next=NULL;
	S->stackSize=0;
	return S;
}
/*功能:元素进栈
  返回:OK:进栈成功
        ERROR:进栈失败
		OVERFLOW:溢出
  */
int Push_SqStack(LStack *S,SElemType e)
{
	
    LsNode *NewNode;
	NewNode=(LsNode *)malloc(sizeof(LsNode));
	if(NewNode==NULL)
	{
		printf("there is not enough memory aviable\n");
		return ERROR;
	}
	NewNode->data=e;
	if(S->stackSize==0)//第一个进栈的节点需要特殊处理
	{
	  S->top->data=e;
	  S->base->next=NULL;
	  S->stackSize++;
	  free(NewNode);
	}
	else 
	{
		NewNode->next=S->top;
		S->top=NewNode;
		S->stackSize++;
	}
	return OK;
}
/*功能:元素出栈
  返回:栈顶的值
  */
SElemType Pop_LsStack(LStack *S)
{
	LsNode *p;
	SElemType e;
	if(S->stackSize==0)
	{
	    printf("stack is empty\n");
		return ERROR;
	}
	else
	{
		p=S->top;
		e=p->data;
		S->top=S->top->next;
	//	p->next=NULL;
		free(p);
		return e;
	}
}
/*功能:读取栈顶的元素
  返回:栈顶元素的值
  */
SElemType Read_Top_LsStack(LStack *S)
{
	SElemType top;
   if(S->stackSize==0)
	{
	    printf("stack is empty\n");
		return ERROR;
	}
   top=S->top->data;
   return top;
}

实现功能,将9个数一次压入堆栈,从其中读出5个,返回剩下堆栈的栈顶指针。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include"stack.h"
int main(void)
{
 LStack *S;
	int i;
	SElemType x,result;
	printf("创建一个链式的栈\n");
    S=Create_LsStack();
	if(S!=NULL)printf("创建成功\n");
	printf("进栈的元素为:\n");
	for(i=1;i<10;i++)
	{
	  result=Push_SqStack(S, i);
	  if(result==OK)printf("%d\t",i);
	}
	printf("\n");
	printf("出栈的元素为\n");
	for(i=0;i<5;i++)
	{
	   x=Pop_LsStack(S);
	   printf("%d\t",x);
	}
	printf("\n");
	printf("栈顶的元素为\n");
	x=Read_Top_LsStack(S);
	printf("%d\n",x);
     system("pause");
	return 0;
}

运行结果:
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,实现线性表的链存储结构可以使用指针来实现。链存储结构是通过节点之间的指针连接来表示线性表中的元素关系。 首先,我们需要定义一个节点结构体,该结构体包含两个成员:数据域和指针域。数据域用于存储节点中的数据,指针域用于指向下一个节点。 ```c // 定义节点结构体 typedef struct Node { int data; // 数据域 struct Node* next; // 指针域,指向下一个节点 } Node; ``` 接下来,我们可以定义一个链表结构体,该结构体包含一个指向头节点的指针。 ```c // 定义链表结构体 typedef struct LinkedList { Node* head; // 头节点指针 } LinkedList; ``` 然后,我们可以实现一些基本的操作函数来对链表进行操作,例如插入、删除、查找等。 1. 插入操作:在链表中插入一个新节点,可以在头部插入或者在指定位置插入。 ```c // 在头部插入新节点 void insertAtHead(LinkedList* list, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = list->head; list->head = newNode; } // 在指定位置插入新节点 void insertAtPosition(LinkedList* list, int data, int position) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; if (position == 0) { newNode->next = list->head; list->head = newNode; } else { Node* current = list->head; for (int i = 0; i < position - 1 && current != NULL; i++) { current = current->next; } if (current != NULL) { newNode->next = current->next; current->next = newNode; } } } ``` 2. 删除操作:从链表中删除指定位置的节点。 ```c // 删除指定位置的节点 void deleteAtPosition(LinkedList* list, int position) { if (list->head == NULL) { return; } Node* temp = list->head; if (position == 0) { list->head = temp->next; free(temp); return; } for (int i = 0; temp != NULL && i < position - 1; i++) { temp = temp->next; } if (temp == NULL || temp->next == NULL) { return; } Node* nextNode = temp->next->next; free(temp->next); temp->next = nextNode; } ``` 3. 查找操作:在链表中查找指定值的节点。 ```c // 查找指定值的节点 Node* search(LinkedList* list, int value) { Node* current = list->head; while (current != NULL) { if (current->data == value) { return current; } current = current->next; } return NULL; } ``` 这样,我们就可以使用上述定义的结构体和函数来实现线性表的链存储结构了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值