链栈的操作
main.c
#include"head.h"
int main(int argc, const char* argv[])
{
int n;
datatype data;
linkstack_pointer head = linkstack_create_head();
printf("请输入插入数据的个数:");
scanf("%d", &n);
for(int i = 0; i<n; i++)
{
printf("请输入要插入的数据:");
scanf("%d", &data);
linkstack_push(head, data);
}
linkstack_output(head);
while(NULL != head->next)
{
linkstack_pop(head);
linkstack_output(head);
}
linkstack_free(head);
printf("请输入要转换成二进制的数值:\n");
scanf("%d", &data);
number_system_deTobi(data);
return 0;
}
head.c
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int datatype ;
typedef struct LinkStack_Node
{
union
{
int data;
int len;
};
struct LinkStack_Node * next;
} * linkstack_pointer;
linkstack_pointer linkstack_create_head(void);
linkstack_pointer linkstack_create_node(void);
int linkstack_push(linkstack_pointer head, datatype data);
void linkstack_output(linkstack_pointer head);
datatype linkstack_pop(linkstack_pointer head);
int linkstack_free(linkstack_pointer head);
linkstack_pointer number_system_deTobi(int data);
#endif
fun.c
#include"head.h"
linkstack_pointer linkstack_create_head(void)
{
linkstack_pointer head = (linkstack_pointer)malloc(sizeof(struct LinkStack_Node));
if(NULL == head)
{
return NULL;
}
head->len = 0;
head->next = NULL;
return head;
}
linkstack_pointer linkstack_create_node(void)
{
linkstack_pointer node = (linkstack_pointer)malloc(sizeof(struct LinkStack_Node));
if(NULL == node)
{
return NULL;
}
node->data = 0;
node->next = NULL;
return node;
}
int linkstack_push(linkstack_pointer head, datatype data)
{
if(NULL == head)
{
printf("入栈失败!\n");
return -1;
}
linkstack_pointer p = linkstack_create_node();
if(NULL == p)
{
printf("入栈失败!\n");
return -1;
}
p->data = data;
p->next = head->next;
head->next = p;
head->len++;
return 0;
}
void linkstack_output(linkstack_pointer head)
{
if(NULL == head || 0 == head->len)
{
printf("遍历失败!\n");
return;
}
linkstack_pointer p = head;
if(NULL == p)
{
printf("遍历失败!\n");
return;
}
while(NULL != p->next)
{
p = p->next;
printf("%d\t", p->data);
}
printf("\n");
}
datatype linkstack_pop(linkstack_pointer head)
{
if(NULL == head || head->len == 0)
{
printf("出栈失败!\n");
return -1;
}
datatype data;
linkstack_pointer p = head->next;
if(NULL == p)
{
printf("出栈失败!\n");
return -1;
}
data = p->data;
printf("%d出栈\n", data);
head->next = p->next;
free(p);
p = NULL;
head->len--;
printf("\n");
return data;
}
int linkstack_free(linkstack_pointer head)
{
if(NULL == head)
{
printf("销毁失败!\n");
return -1;
}
linkstack_pointer p = head;
if(NULL == p)
{
printf("销毁失败!\n");
return -1;
}
while(NULL != head->next)
{
p = head->next;
head->next = p->next;
free(p);
head->len--;
}
free(head);
p = NULL;
return 0;
}
linkstack_pointer number_system_deTobi(int data)
{
linkstack_pointer head = linkstack_create_head();
if(data<0 || head == NULL)
{
printf("转换失败\n");
return NULL;
}
linkstack_pointer temp;
while(data)
{
linkstack_push(head, data%2);
data = data/2;
}
while(NULL != head->next)
{
linkstack_pop(head);
}
return 0;
}