您的位置:首页 > 编程语言

堆栈结构代码以及指针数组代码

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐