4月3号链栈练习

#include "head.h"
#include "test.c"

int main(int argc, const char *argv[])
{
#if 0
	linkstack top=create_head();
	datatype e;
	char a[10];
	while(1)
	{
		printf("请输入需要插入的元素:");
		scanf("%d",&e);
		linkstack_push(top,e);

		printf("是否继续插入元素(Y/N):");
		scanf("%s",a);
		if(strcmp(a,"N")==0)
		{
			break;
		}
	}
	linkstack_output(top);
	printf("头删:");
	linkstack_pop(top);
	linkstack_output(top);

	top = free_space(top);

#else
	
	linkstack top=create_head();

	int num = 0;
	int n;
	datatype e;
	printf("请输入你需要进行进制转换的十进制数:");
	scanf("%d",&num);
	printf("请输入需要进行几进制转换:");
	scanf("%d", &n);
	while(num>0)
	{
		int num1 = num%n;
		if(num1<=9)
		{
			e = num1 +'0';
		}
		else
		{
			e =(num1-10)+'a'; 
		}
		num=num/n;
		
		linkstack_push(top,e);
	}
	
	linkstack_output_num(top,n);
    top = free_space(top);

#endif
	return 0;
}

test.c

#include "head.h"
/*头结点的创建
 *
 */

linkstack create_head()
{
	linkstack top=(linkstack)malloc(sizeof(struct Node));
	if(top == NULL)
	{
		return NULL;
	}
	top->len=0;
	top->next=NULL;
	return top;
}
/*普通节点创建
 *
 */

linkstack create_node()
{
	linkstack p=(linkstack)malloc(sizeof(struct Node));
	if(p == NULL)
	{
		return NULL;
	}
	p->data=0;
	p->next=NULL;
	return p;
}
/*单链栈头插
 *
 *
 */
int linkstack_push(linkstack top, datatype e)
{
	if(top==NULL)
	{
		printf("头插失败\n");
		return -1;
	}
	linkstack s=create_node();
	s->data=e;
	s->next = top->next;
	top->next = s;
	top->len++;
	return 0;
}
/*头删
 *
 *
 */
int linkstack_pop(linkstack top)
{
	if(top == NULL || top->len==0)
	{
		printf("删除失败\n");
		return -1;
	}
	linkstack p=top->next;
	top->next = p->next;
	free(p);
	p=NULL;
	top->len--;
	return 0;
}

/*元素输出
 *
 *
 */
void linkstack_output(linkstack top)
{
	puts("");
	if(top == NULL)
		return;
	linkstack p=top;
	while(p->next != NULL)
	{
		p=p->next;
		printf("%d",p->data);
	}
	puts("");

}
/*空间释放
 *
 */
linkstack free_space(linkstack top)
{
	if(top==NULL)
	{
		return NULL;
	}
	int n=top->len;
	for(int i=0; inext != NULL)
	{
		p=p->next;
		printf("%c",p->data);
	}
	puts("");

}

4月3号链栈练习_第1张图片

你可能感兴趣的:(数据结构)