用链表实现栈和队列
来源:互联网 发布:微信使用调查数据 编辑:程序博客网 时间:2024/05/09 14:19
#include<stdio.h>
002
#include<stdlib.h>
003
#define N 10
004
005
typedef
struct
node {
006
char
val;
007
struct
node *next;
008
} link,*plink;
009
int
i=0,j=0;
010
char
a[N]=
"I am boy!"
;
011
012
link *make_node(
int
n)
013
{
014
link *p=NULL;
015
if
((p = (link *)
malloc
(
sizeof
(link))) == NULL) {
016
printf
(
"error!"
);
017
return
NULL;
018
}
019
p->next = NULL;
020
p->val=n;
021
return
p;
022
}
023
//头查
024
/*void insert1(plink *head,link* p)
025
{
026
027
p ->next=*head;
028
*head=p;
029
}*/
030
//进栈
031
/*void push (plink *head,link*p){
032
033
insert1(head,p);
034
}*/
035
//尾查
036
void
insert2(plink *head,link*p)
037
{
038
plink s=*head;
039
if
(*head==NULL)
040
*head=p;
041
else
042
{
043
while
(s->next)
044
s = s->next;
045
s->next=p;
046
047
}
048
}
049
//进队
050
void
enqueue(plink*head,link*p){
051
052
insert2(head,p);
053
}
054
//出队
055
link*dequeue(plink*head){
056
057
plink p=NULL;
058
if
(NULL==*head)
return
NULL;
059
p=*head;
060
*head=(*head)->next;
061
return
p;
062
063
}
064
//出栈
065
/*link* pop(plink *head){
066
067
plink *p=NULL;
068
if(NULL==*head)return NULL;
069
p=head;
070
*head=(*head)->next;
071
return *p;
072
}*/
073
//判空
074
int
is_empty(plink*head){
075
076
return
*head==NULL;
077
}
078
//查询
079
/*link* serach(plink head,int key){
080
081
plink p;
082
for (p = head; p; p = p->next)
083
if (p->val == key)
084
return p;
085
return NULL;
086
}*/
087
//遍历
088
void
traverse(
void
(*visit)(plink),plink head)
089
090
{
091
092
plink p;
093
for
(p = head; p; p = p->next)
094
visit(p);
095
}
096
void
visit(plink p)
097
{
098
printf
(
"%c"
,p->val);
099
}
100
/*
101
//删除
102
void delete(plink *head,plink p)
103
104
{
105
plink pre;
106
if (p == *head)
107
{
108
*head = p->next;
109
return;
110
}
111
for (pre = *head; pre; pre = pre->next)
112
if (pre->next == p)
113
{
114
pre->next = p->next;
115
return;
116
}
117
118
}
119
//释放
120
void destroy(plink *head)
121
{
122
plink q, p = *head;
123
*head = NULL;
124
while (p)
125
{
126
q = p;
127
p = p->next;
128
free_node(q);
129
}
130
}
131
132
free_node(link *p)
133
134
{
135
if (p)
136
free(p);
137
}
138
*/
139
int
main(
int
argc,
char
*argv[]) {
140
141
link *head=NULL, *p=NULL, *s=NULL;
142
for
(i = 0; i<N; i++)
143
// insert1(&head,make_node(a[i]));
144
// insert2(&head,make_node(a[i]));
145
// push(&head,make_node(a[i]));
146
// plink f=head;
147
// head=pop(&head);
148
enqueue(&head,make_node(a[i]));
149
150
plink f=head;
151
// is_empyt(head);
152
for
(i=0;i<N;i++)
153
while
((p=dequeue(&head))!=
'\0'
){
154
155
printf
(
"%c"
,p->val);
156
}
157
// printf("\n");
158
// printf("%d\n",serach(f,5)->val);
159
// traverse(visit,head);
160
// delete(&f,serach(f,5));
161
printf
(
"\n"
);
162
// destroy(&f);
163
164
// while(f)
165
// {
166
// printf("%c",f->val);
167
// f=f->next;
168
//
169
// }
170
// free_node(p);
171
}
举报
- 用链表实现栈和队列
- 队列实现栈和栈实现队列
- Java用链表实现栈和队列
- 栈和队列的实现
- C#实现栈和队列
- 栈和队列的实现
- LinkedList实现队列和栈
- java栈和队列实现
- Java实现栈和队列
- Java实现栈和队列
- Java实现栈和队列
- 栈和队列相互实现
- python实现栈和队列
- 队列和栈简单实现
- 栈和队列的实现
- Python实现栈和队列
- C实现栈和队列
- 栈和队列的实现
- 使用vs2010 分析程序性能
- 黑马程序员 Java(三)>单例设计模式<
- oracle非分区表删除
- github常见操作和常见错误!
- java实现的highcharts与ajax结合动态实时获取数据更新图表
- 用链表实现栈和队列
- HDU4642--Fliping game HDU(139)
- Mac电脑外接显示器全攻略
- CSS 颜色代码大全
- Tomcat去除端口号和项目名
- 如何用c#读写配置文件
- for循环中加载数据的一点小收获
- windows下android关于NDK开发流程简介
- 【How to read a paper】如何读论文