⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jiafa.c

📁 数据结构的链表加法
💻 C
字号:

#include <string.h>
#include <stdio.h>
#include <conio.h>

struct node;
typedef struct node *pnode;
struct node
{
 int coef;
 int exp;
 pnode next;
 };
struct linklist;
typedef struct linklist *plinklist;
struct linklist
{
 int n;
 pnode next;
 };
int flag;

plinklist create(void);
void data_in_link(plinklist head);
plinklist add(plinklist pa,plinklist pb);
void display(plinklist p);
extern array1(void);
extern array(int x,int y,int width,int height,int color,int bk);

plinklist create(void)
{
 plinklist p;
 p=(plinklist)malloc(sizeof(struct linklist));
 if(p==NULL)
   array1();
  else
   {p->n=0;
    p->next=NULL;}
  return(p);
 }

void data_in_link(plinklist head)
{
 pnode p,q;
 int s=3;
 char ch='y';
 p=(pnode)malloc(sizeof(struct node));
 if(p==NULL)array1();
 else{head->next=p;
      p->next=NULL;
      head->n++;
      gotoxy(46,18);scanf("%d",&p->coef);
      gotoxy(54,18);scanf("%d",&p->exp);
      gotoxy(64,18);getchar();
      gotoxy(64,18);ch=getch();
      gotoxy(46,18);printf("    ");
      gotoxy(54,18);printf("    ");
      while(ch!='n')
      {head->n++;
       q=(pnode)malloc(sizeof(struct node));
       if(p==NULL)
	  {array1();break;}
	   gotoxy(46,18);scanf("%d",&q->coef);
	   gotoxy(54,18);scanf("%d",&q->exp);
	   q->next=NULL;p->next=q;p=q;
	   gotoxy(64,18);getchar();
	   gotoxy(64,18);ch=getch();
           gotoxy(46,18);printf("    ");
           gotoxy(54,18);printf("    ");
           }
     }
}

plinklist add(plinklist pa,plinklist pb)
{
 pnode p,q,pre,u;
 int s,x;
 if(pa->n==0||pb->n==0)array1();
 else
 {
   p=pa->next;q=pb->next;pa->n+=pb->n;
   while(p&&q)
   {
    if(p->exp<q->exp)
     {pre=p;p=p->next;}
     else if(p->exp==q->exp)
	    {x=p->coef+q->coef;
	     if(x){p->coef=x;pre=p;pa->n--;}
	      else{pre->next=p->next;free(p);pa->n=pa->n-2;}
	     p=pre->next;u=q;q=q->next;free(u);}
      else{u=q->next;q->next=p;pre->next=q;pre=q;q=u;}
    }
   if(q)pre->next=q;
   free(pb);
   return(pa);
  }
}

void display(plinklist p)
{
 pnode q;
 int s=3,x,y;
 if(p->n==0)
  array1();
 else
 {
   x=11;
   q=p->next;
   if(flag==0)y=21;else if(flag==1)y=25;else y=29;
   while(q!=NULL)
   {gotoxy(x+5,y);printf("%d",q->coef);
    gotoxy(x+5,y+1);printf("%d",q->exp);
    x=x+4;
    q=q->next;}
   }
}


main()
{
 plinklist pa,pb,pc;
 int k=1;
 char ch[20];
 while(k)
 {
 window(1,1,80,50);
 textbackground(WHITE);
 clrscr();
 array(10,15,60,20,GREEN,BLUE);
 gotoxy(30,16);printf("poly addication");
 gotoxy(11,18);printf("jiang mi su ru!press 'n' over!coef:    exp:     over:");
 gotoxy(11,20);printf("input 1");
 gotoxy(11,21);printf("coef:");
 gotoxy(11,22);printf("exp:");
 gotoxy(11,24);printf("input 2");
 gotoxy(11,25);printf("coef:");
 gotoxy(11,26);printf("exp:");
 gotoxy(11,28);printf("output");
 gotoxy(11,29);printf("coef:");
 gotoxy(11,30);printf("exp:");
 gotoxy(11,32);printf("press 'exit' to exit,others to continue");
 gotoxy(11,34);printf("author:chengsha   number:020310601   time: 2004-03-29");
 gotoxy(54,32);printf("choose:");
 flag=0;
 pa=create();
 data_in_link(pa);
 display(pa);
 flag=1;
 pb=create();
 data_in_link(pb);
 display(pb);
 pc=add(pa,pb);
 flag=2;
 display(pc);
 gotoxy(61,32);gets(ch);
 if(strcmp(ch,"exit")==0)k=0;
 }}



extern array(int x,int y,int width,int height,int color,int bk)
 {
   int i,j;
   textbackground(BLACK);
   textbackground(bk);
   window(x,y,x+width,y+height);
   textcolor(color);
   clrscr();
   window(1,1,80,50);
   gotoxy(x,y);
   putch(218);
   for(i=0;i<width-1;i++)
      putch(196);
   putch(191);
   for(i=0;i<height;i++)
   {
      gotoxy(x,y+1+i);
      putch(179);
      gotoxy(x+width,y+1+i);
      putch(179);
   }
   gotoxy(x,y+2);
   putch(195);
   for(i=0;i<width-1;i++)
    putch(196);
    putch(180);
   for(i=4;i<=16;i=i+4)
   {
    gotoxy(x,y+i);
    putch(195);
    for(j=0;j<width-1;j++)
      putch(196);
      putch(180);
      }
   gotoxy(x,y+height);
   putch(192);
   for(i=0;i<width-1;i++)
      putch(196);
   putch(217);
   return;
}
extern array1(void)
{
 int i,x,y;
 x=29;y=10;
 textbackground(WHITE);
 textbackground(BLUE);
 textcolor(RED);
 window(x,y,x+20,y+3);
 clrscr();
 window(1,1,80,50);
 gotoxy(x,y);putch(218);
 for(i=1;i<20;i++)
  {gotoxy(x+i,y);putch(196);}
  putch(191);
 for(i=1;i<3;i++)
  { gotoxy(x,y+i);putch(179);
    gotoxy(x+20,y+i);putch(179);}
 gotoxy(x,y+3);putch(192);
  gotoxy(x+20,y+3);putch(217);
  for(i=1;i<20;i++)
   {gotoxy(x+i,y+3);putch(196);}
   gotoxy(36,y+1);printf("error!");

 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -