回 帖 发 新 帖 刷新版面

主题:俺写了两个程序,一个链表的合并,一个数制的转换,已通过编译.贴出来望指点.

#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); 



回复列表 (共5个回复)

沙发

等等 马上

板凳

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define ElemType int
#define OVERFLOW -1
#define  OK 0
typedef struct LNode
{ElemType data;
 struct LNode *next;
}LNode, *LinkList;

int createlist( LinkList *La, int n)
{int i;
 LNode *p,*p1;
  (*La)=(LinkList)malloc(sizeof(LNode));
 if(La==NULL)//  这点替你加上的 只要分配内存都要加上这个判断 养好良好的编程习惯
 { printf("out of memory");
   exit(OVERFLOW);
 }

  p1=(*La);
  (*La)->next=NULL;
  
 for(i=n;i>0;i--)//合在一起比好一些
 {p=(LinkList)malloc(sizeof(LNode));
   if(p==NULL)//  这点替你加上的 只要分配内存都要加上这个判断 养好良好的编程习惯
   { printf("out of memory");
     exit(OVERFLOW);
   }
 scanf("%d",&p->data);
  p->next=NULL;
  p1->next=p;
  p1=p;
 }
  return OK;
 }
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,La;
 //clrscr();//这个在那个头文件中 你掉了
 printf("please input n");
 scanf("%d",&n);
  createlist(&La,n);

 printf("lease input the n again");
 scanf("%d",&n);
 createlist(&La,n);
 
 merglist(Linka,Linkb,Linkc);//在执行前要初始化Linka Linkb
 system("pause");//让别人可以看到答案
 return 0;
}
替你改了改  还有一点 就是 执行前对Linka Linkb赋值 你自己搞定

3 楼

第2个程序就是要加头文件
其他的没错误  
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

4 楼

挺细心的!!!!!!!!第二个不加头文件也可以通过

5 楼

我知道 有些编译器不加也可以通过 例如WIN TC  而在visual C++ 6.0要加
楼主最好加上 养成好习惯  如果一直不加  到时候需要呢  不知道那个头文件包含那个函数 弄不清楚 又得查书  现在弄好了 以后就节约 很多时间的  养成好习惯很重要  You could instead write nc = nc + 1 but ++nc is more concise and often more efficient  come from <<The C programming Language>>

我来回复

您尚未登录,请登录后再回复。点此登录或注册