用链表实现栈和队列

来源:互联网 发布:微信使用调查数据 编辑:程序博客网 时间:2024/05/09 14:19
#include<stdio.h>
002#include<stdlib.h>
003#define N 10
004 
005typedef struct node {
006    charval;
007    structnode *next;
008} link,*plink;
009int i=0,j=0;
010char a[N]="I am boy!";
011 
012link *make_node(intn)
013{
014    link *p=NULL;
015        if((p = (link *) malloc(sizeof(link))) == NULL) {
016            printf("error!");
017            returnNULL;
018        }
019        p->next = NULL;
020        p->val=n;
021        returnp;
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 voidinsert2(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//进队
050void enqueue(plink*head,link*p){
051 
052    insert2(head,p);
053}
054//出队
055link*dequeue(plink*head){
056 
057    plink p=NULL;
058    if(NULL==*head)returnNULL;
059    p=*head;
060    *head=(*head)->next;
061    returnp;
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//判空
074int is_empty(plink*head){
075 
076return  *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//遍历
088void traverse(void (*visit)(plink),plink head)
089 
090{
091 
092        plink p;
093    for(p = head; p; p = p->next)
094                visit(p);
095}
096void visit(plink p)
097{
098printf("%c",p->val);
099}
100/*
101//删除
102void 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//释放
120void 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 
132free_node(link *p)
133 
134{
135    if (p)
136        free(p);
137}
138*/
139int 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}
举报


原创粉丝点击