书75页,原理为数值不断%8,例如输入100,进行while循环,将余数push进链表,直到商等于0,当完成后,不断将尾指针的数值取出并将尾指针指向前一个指针,free()原来的尾指针
商 | 余 | |
100%8 | 12 | 4 |
12%8 | 1 | 4 |
1%8 | 0 | 1 |
#include<stdio.h>
#include<stdlib.h>
typedef int Ptype;
#define yes 1
#define no 0
typedef struct SNode/*定义结构体*/
{
Ptype data;
struct SNode *next;
}*Link;
typedef struct/*定义前指针和后指针*/
{
Link front;
Link rear;
}Linksnode;
Ptype Initial(Linksnode &t)/*初始化前指针*/
{
t.front=t.rear=new SNode;
t.front->next=NULL;/*定义首链表为空表*/
return yes;
}
Ptype Push(Linksnode &t,Ptype e)/*将e插在链表末尾*/
{
Link p;
p=new SNode;
p->data=e;
p->next=NULL;
t.rear->next=p;
t.rear=p;/*t.rear始终指向最后一个链表*/
return yes;
}
Ptype Pup(Linksnode &t,Ptype &e)/*将末尾链表的值取出*/
{
Link p,temp;
p=t.front;
if(t.front==t.rear)/*如果尾指针也指向头指针相同的链表,说明空表*/
return no;
e=t.rear->data;
temp=t.rear;
while(p->next!=t.rear)/*将p指针指向尾指针的前一个链表*/
p=p->next;
p->next=NULL;
t.rear=p;/*尾指针指向前一个链表8*/
free(temp);
return yes;
}
int main()
{
Linksnode t;
Initial(t);
Ptype x,e;
scanf("%d",&x);
while(x)
{
Push(t,x%8);
x/=8;
}
while((Pup(t,e)))
{
printf("%d",e);
}
system("pause");
}