目录
1.结构体创建
typedef int data_t;
typedef struct next_t{
data_t data;
struct next_t *next;
}linkstack;
2.创建链表
linkstack *create_linkstack(void)
{
linkstack *l;
l = (linkstack *)malloc(sizeof(linkstack));
if(l == NULL)
{
printf("l is error\n");
exit(0);
}
l->data = 0;
l->next = NULL;
return l;
}
3.入栈
void push_linkstack(linkstack *l,data_t data)
{
if(l == NULL)
{
printf("l_push is error\n");
exit(0);
}
linkstack *p;
p = (linkstack *)malloc(sizeof(linkstack));
p->next = NULL;
p->data = data;
if(l->next == NULL)
{
l->next = p;
}
else
{
p->next = l->next;
l->next = p;
}
return ;
}
4.出栈
data_t pop_linkstack(linkstack *l)
{
if(l == NULL)
{
printf("l_pop is error\n");
exit(0);
}
linkstack *q;
data_t t;
q = l->next;
l->next = q->next;
t = q->data;
free(q);
q = NULL;
return t;
}
5.遍历
void show_linkstack(linkstack *l)
{
if(l == NULL)
{
printf("l_show is error\n");
exit(0);
}
linkstack *q;
q = l->next;
while(q)
{
printf("%d ",q->data);
q = q->next;
}
printf("\n");
return ;
}
6.清除
void clear_linkstack(linkstack *l)
{
if(l == NULL)
{
printf("l_show is error\n");
exit(0);
}
linkstack *q;
q = l;
int value;
while(q->next)
{
value = pop_linkstack(q);
printf("free(%d)\n",value);
}
return ;
}
程序
LinkStack.h
#include <stdio.h>
#include "stdlib.h"
typedef int data_t;
typedef struct next_t{
data_t data;
struct next_t *next;
}linkstack;
linkstack *create_linkstack(void);
void push_linkstack(linkstack *l,data_t data);
data_t pop_linkstack(linkstack *l);
void show_linkstack(linkstack *l);
void clear_linkstack(linkstack *l);
LinkStack.c
#include "LinkStack.h"
linkstack *create_linkstack(void)
{
linkstack *l;
l = (linkstack *)malloc(sizeof(linkstack));
if(l == NULL)
{
printf("l is error\n");
exit(0);
}
l->data = 0;
l->next = NULL;
return l;
}
void push_linkstack(linkstack *l,data_t data)
{
if(l == NULL)
{
printf("l_push is error\n");
exit(0);
}
linkstack *p;
p = (linkstack *)malloc(sizeof(linkstack));
p->next = NULL;
p->data = data;
if(l->next == NULL)
{
l->next = p;
}
else
{
p->next = l->next;
l->next = p;
}
return ;
}
data_t pop_linkstack(linkstack *l)
{
if(l == NULL)
{
printf("l_pop is error\n");
exit(0);
}
linkstack *q;
data_t t;
q = l->next;
l->next = q->next;
t = q->data;
free(q);
q = NULL;
return t;
}
void show_linkstack(linkstack *l)
{
if(l == NULL)
{
printf("l_show is error\n");
exit(0);
}
linkstack *q;
q = l->next;
while(q)
{
printf("%d ",q->data);
q = q->next;
}
printf("\n");
return ;
}
void clear_linkstack(linkstack *l)
{
if(l == NULL)
{
printf("l_show is error\n");
exit(0);
}
linkstack *q;
q = l;
int value;
while(q->next)
{
value = pop_linkstack(q);
printf("free(%d)\n",value);
}
return ;
}
main.c
#include<stdio.h>
#include "LinkStack.h"
int main(int argc,char *argv[])
{
linkstack *l;
l = create_linkstack();
push_linkstack(l,0);
push_linkstack(l,1);
push_linkstack(l,2);
push_linkstack(l,3);
push_linkstack(l,4);
push_linkstack(l,5);
show_linkstack(l);
clear_linkstack(l);
show_linkstack(l);
}
本文介绍了如何在C语言中使用结构体和链表实现一个简单的链式栈,包括创建栈、入栈、出栈、遍历和清除功能,以及相应的错误处理机制。
577

被折叠的 条评论
为什么被折叠?



