堆栈结构代码以及指针数组代码
来源:互联网 发布:sql identity 不跳 编辑:程序博客网 时间:2024/05/29 18:03
#include <stdio.h>#include <stdlib.h>#define N 13char *month_name(char* month[],int n);int main(){ int ret,n; char *result=NULL; char *month[N]={"Illegal month","January","February","March","April","May","July","August","September","October","November","December"}; result=(int*)malloc(sizeof(char)*20); if(result==NULL) { printf("there is no space\n"); exit(1); } printf("please input the number of month:\n"); ret=(scanf("%d",&n)); getchar(); if(ret!=1) { printf("the input is wrong!\n"); fflush(stdin); exit(2); } else { result=month_name(month,n); printf("The month name is %s\n",result); } return 0;}char *month_name(char* month[],int n){ if(n<1||n>12) return month[1]; else return month[n];}关于堆栈的第一版本
#include <stdio.h>#include <stdlib.h>struct link{ int data; struct link*next;};struct link *push(struct link*head,int nodedata);struct link *pop(struct link*head);void deletememory(struct link*head);int main(){ int n,ret,a; struct link*head; printf("what do you want do:1 represent push,2 represent pop\n"); ret=(scanf("%d",&n)); getchar(); if(ret!=1) { printf("the input is wrong!\n"); fflush(stdin); exit(1); } else { switch(n) { case 1: printf("please input the number you want:c\n"); ret=(scanf("%d",&a)); getchar(); if(ret!=1) { printf("the input is wrong!\n"); fflush(stdin); exit(2); } else { push(head,a); } break; case 2: pop(head); break; default: printf("the invalid input!"); } } return 0;}struct link *push(struct link*head,int nodedata){ struct link*q=head,*p=NULL,*temp=NULL; p=(struct link *)malloc(sizeof(struct link)); if(p==NULL) { printf("the memory is not enough!"); exit(1); } p->next=NULL; p->data=nodedata; if(head==NULL) { head=p; } else { while(q->next!=NULL) { temp=q; q=q->next; } q->next=p; p->next=NULL; }};struct link *pop(struct link*head){ struct link*p=head,*temp=NULL; if(head==NULL) { printf("link table is empty"); return 0; } temp=(struct link *)malloc(sizeof(struct link)); if(temp==NULL) { printf("the memory is not enough!"); exit(1); } while(p->next!=NULL) { temp=p; p=p->next; } temp->next=NULL; free(p);};void deletememory(struct link*head){ struct link*p=head,*pr=NULL; while(p!=NULL) { pr=p; p=p->next; free(pr); }}
关于堆栈第二版本#include <stdio.h>
#include <stdlib.h>
typedef struct link
{
int data;
struct link*next;
}STACK;
struct link *push(struct link*head,int nodedata);
struct link *pop(struct link*head);
void deletememory(struct link*head);
void print_stack(struct link*head);
int main()
{
int n,ret,a,nodedata;
struct link*head=NULL;
LOOP:
printf("******************************\n\n");
printf("-------1.向堆栈存储数据-------\n");
printf("-------2.从堆栈取出数据-------\n");
printf("-------3.退出-----------------\n\n");
printf("******************************\n\n");
printf("please input one");
ret=(scanf("%d",&n));
getchar();
if(ret!=1)
{
printf("the input is wrong!\n");
fflush(stdin);
exit(1);
}
else
{
switch(n)
{
case 1:
printf("please input the number you want:c\n");
ret=(scanf("%d",&nodedata));
getchar();
printf("The content of the stack(before top->bottom):\n");
print_stack(head);
if(ret!=1)
{
printf("the input is wrong!\n");
fflush(stdin);
exit(2);
}
else
{
head=push(head,nodedata);
printf("The content of the stack(after top->bottom):\n");
print_stack(head);
}
break;
case 2:
head=pop(head);
printf("The content of the stack(after pop):\n");
print_stack(head);
break;
default:
printf("the invalid input!");
case 3:
exit(0);
}
goto LOOP;
}
return 0;
}
struct link *push(struct link*head,int nodedata)
{
struct link*q=head,*p=NULL,*temp=NULL;
p=(struct link *)malloc(sizeof(struct link));
if(p==NULL)
{
printf("the memory is not enough!");
exit(1);
}
p->next=NULL;
p->data=nodedata;
if(head==NULL)
{
head=p;
}
else
{
p->next=head;
head=p;
return head;
}
};
struct link *pop(struct link*head)
{
struct link*p=head;
if(head==NULL)
{
printf("link table is empty");
}
else{
if(head->next!=NULL)
{
head=head->next;
free(p);
}
else{
head=NULL;
printf("link table is empty");
}
}
return head;
};
void deletememory(struct link*head)
{
struct link*p=head,*pr=NULL;
while(p!=NULL)
{
pr=p;
p=p->next;
free(pr);
}
}
void print_stack(struct link*head)
{
struct link*p=head;
while(p!=NULL)
{
printf("[%d] ",p->data);
p=p->next;
}
}
堆栈头文件:
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include<stdlib.h>
#include<list.h>
typedef List Stack;
#define stack_init List_init;
#define stack_destory List_destory;
int stack_push(Stack *stack,const void *data)
{
return list_ins_next(stack,NULL,data);
}
int stack_pop(Stack *stack,void **data)
{
return list_rem_next(stack,NULL,data);
}
#define stack_peek(stack)((stack)->head==NULL?NULL:(stack)->head->data)
#endif // STACK_H_INCLUDED
1 0
- 堆栈结构代码以及指针数组代码
- 涉及到指针、数组、堆栈、以及printf
- 数组指针《代码》
- 堆栈代码
- BSS段 数据段 代码段 堆栈 指针 vs 引用
- BSS段、数据段、代码段、堆栈、指针/引用
- BSS段、数据段、代码段、堆栈、指针/引
- BSS段、数据段、代码段、堆栈、指针/引
- 编写安全代码:数组和指针的本质以及何时不能互换
- PHP中使用数组实现堆栈数据结构的代码
- 数组指针的综合运用代码总结
- 数组和指针内存分配示例代码
- 数组和指针内存分配示例代码
- 字符数组与指针,日常抄代码
- 第22节 C语言结构体之结构体嵌套、结构体指针与结构体数组的代码实现
- Storm系统架构以及代码结构学习
- 数组,多维数组及指针数组代码验证
- 堆栈执行代码格式
- java定时器实现
- Spring 3之MVC & Security简单整合开发(三)
- 概览
- 学生成绩处理
- 第十六周项目—学生成绩处理1
- 堆栈结构代码以及指针数组代码
- DgitalOcean centos7+ss+Proxifiersh实现校园网免ipv4
- 关于static关键字的使用以及与成员方法、成员属性的总结
- java web项目发版js或css缓存问题解决方案
- Ajax删除记录,无刷新更改页面总记录数
- 学生成绩处理
- Netty 笔记整理
- MyEclipse6.5每次启动都要重新配置tomcat。解决方案
- Spring Security 2 配置精讲