堆栈结构代码以及指针数组代码
2016-12-15 09:16
169 查看
#include <stdio.h> #include <stdlib.h> #define N 13 char *month_name(char* month[],int n); int main() { int ret,n; char *result=NULL; char *month ={"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 ; }关于堆栈的第一版本
#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;
defaul
4000
t:
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
相关文章推荐
- 深入理解计算机系统(3.8)---数组、异质结构以及指针的详解(十分重要)(难度较高)【呕心沥血版】
- 深入理解计算机系统(3.8)---数组、异质结构以及指针的详解(十分重要)(难度较高)【呕心沥血版】
- 涉及到指针、数组、堆栈、以及printf
- 编写安全代码:数组和指针的本质以及何时不能互换
- 编写安全代码:数组和指针的本质以及何时不能互换
- 数组,异质结构以及指针的详解
- 编写安全代码:数组和指针的本质以及何时不能互换
- 第22节 C语言结构体之结构体嵌套、结构体指针与结构体数组的代码实现
- 彻底了解指针数组,数组指针,以及函数指针,以及堆中的分配规则
- 数组的指针、指针数组以及指向指针的指针
- 彻底了解指针数组,数组指针,以及函数指针,以及堆中的分配规则
- 彻底了解指针数组,数组指针,以及函数指针
- 学点 C 语言(25): 数据类型 - 结构数组与结构指针
- 看看软件开发人员在代码编写工作中为什么需要学习代码的优雅性,结构以及效率方面的内容。
- 【C++学习】指针数组,数组指针,以及函数指针,以及堆中的分配规则--有是一篇好文章,收到自己门下 慢慢学习
- 彻底了解指针数组,数组指针,以及函数指针,以及堆中的分配规则[转]
- 指针数组,数组指针,以及函数指针,以及堆中的分配规则
- 彻底了解指针数组,数组指针,以及函数指针,以及堆中的分配规则
- 指针,数组和结构
- 从一段代码来看指针和数组名的区别