主题:俺写了两个程序,一个链表的合并,一个数制的转换,已通过编译.贴出来望指点.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define ElemType int
typedef struct LNode
{ElemType data;
struct LNode *next;
}LNode, *LinkList;
int createlist(int n)
{int i;
LinkList p,p1,La;
La=(LinkList)malloc(sizeof(LNode));
p1=La;
scanf("%d",&p1->data);
La->next=NULL;
for(i=n-1;i>0;i--)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=NULL;
p1->next=p;
p1=p;}
return La;
}
void merglist(LinkList La,LinkList Lb,LinkList Lc)
{ LinkList pa,pb,pc;
pa=La;pb=Lb;
if(La->data>Lb->data){
Lc=Lb; pb=pb->next; }
else {
Lc=La;pa=pa->next;}
pc=Lc;
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else{
pc->next=pb;
pc=pb;
pb=pb->next;
}
}
if(pa)
pc->next=pa;
else
pc->next=pb;
while(Lc){
printf(" %d ",Lc->data);
Lc=Lc->next;
}
}
main()
{int n;
LinkList Linka,Linkb,Linkc;
clrscr();
printf("please input n");
scanf("%d",&n);
Linka= createlist(n);
printf("lease input the n again");
scanf("%d",&n);
Linkb=createlist(n);
merglist(Linka,Linkb,Linkc);
return 0;
}
#define STACK_INIT_SIZE 100
#define SElemType int
#define Status int
#define ERROR 0
#define OK 1
typedef struct STACK
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack **S)
{
(*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
if(!(*S)->base)exit(ERROR);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
return OK;
}
Status push(SqStack *s,SElemType e)
{*(s->top++)=e;
return OK;
}
Status pop(SqStack *s)
{int e;
if(s->top==s->base) return ERROR;
e=*(s->top-1); s->top--;
return e;
}
main()
{int N,i,k=1;
SqStack *S;
InitStack(&S);
scanf("%d",&N);
while(N)
{push(S,N%8);
N=N/8;
}
while(!(S->top==S->base))
{i=pop(S);
printf("%d ",i);
}
}
#include <stdlib.h>
#include <conio.h>
#define ElemType int
typedef struct LNode
{ElemType data;
struct LNode *next;
}LNode, *LinkList;
int createlist(int n)
{int i;
LinkList p,p1,La;
La=(LinkList)malloc(sizeof(LNode));
p1=La;
scanf("%d",&p1->data);
La->next=NULL;
for(i=n-1;i>0;i--)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=NULL;
p1->next=p;
p1=p;}
return La;
}
void merglist(LinkList La,LinkList Lb,LinkList Lc)
{ LinkList pa,pb,pc;
pa=La;pb=Lb;
if(La->data>Lb->data){
Lc=Lb; pb=pb->next; }
else {
Lc=La;pa=pa->next;}
pc=Lc;
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else{
pc->next=pb;
pc=pb;
pb=pb->next;
}
}
if(pa)
pc->next=pa;
else
pc->next=pb;
while(Lc){
printf(" %d ",Lc->data);
Lc=Lc->next;
}
}
main()
{int n;
LinkList Linka,Linkb,Linkc;
clrscr();
printf("please input n");
scanf("%d",&n);
Linka= createlist(n);
printf("lease input the n again");
scanf("%d",&n);
Linkb=createlist(n);
merglist(Linka,Linkb,Linkc);
return 0;
}
#define STACK_INIT_SIZE 100
#define SElemType int
#define Status int
#define ERROR 0
#define OK 1
typedef struct STACK
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack **S)
{
(*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
if(!(*S)->base)exit(ERROR);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
return OK;
}
Status push(SqStack *s,SElemType e)
{*(s->top++)=e;
return OK;
}
Status pop(SqStack *s)
{int e;
if(s->top==s->base) return ERROR;
e=*(s->top-1); s->top--;
return e;
}
main()
{int N,i,k=1;
SqStack *S;
InitStack(&S);
scanf("%d",&N);
while(N)
{push(S,N%8);
N=N/8;
}
while(!(S->top==S->base))
{i=pop(S);
printf("%d ",i);
}
}