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

📄 hua1.c

📁 数据结构的链表加法
💻 C
字号:
#include"stdio.h"
#include"conio.h"
#include"malloc.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;
 };
pnode initial(int x,int y)
{
 pnode p,q;
 plinklist head;
 int m=10;
 char ch;
 head=(plinklist)malloc(sizeof(struct linklist));
 p=(pnode)malloc(sizeof(struct node));
 if(head==NULL||p==NULL)exit(0);
 head->n=1;head->next=p;p->next=NULL;
 gotoxy(10,3);printf("coef:      exp:      ");
 gotoxy(10,4);printf("choose1:     choose2:     choose3:         ");
 gotoxy(x,y+1);printf("coef:");
 gotoxy(x,y+2);printf("exp:");
 gotoxy(15,3);scanf("%d",&p->coef);
 gotoxy(25,3);scanf("%d",&p->exp);
 gotoxy(x+5,y+1);printf("%d",p->coef);
 gotoxy(x+5,y+2);printf("%d",p->exp);
 gotoxy(18,4);getchar();
 gotoxy(18,4);ch=getchar();
 while(ch!='n')
  {
    gotoxy(10,3);printf("coef:      exp:      ");
    q=(pnode)malloc(sizeof(struct node));
    if(q==NULL)exit(0);
    gotoxy(15,3);scanf("%d",&q->coef);
    gotoxy(25,3);scanf("%d",&q->exp);
    while(p->exp>=q->exp)
    {
     gotoxy(33,3);printf("input error!");
     gotoxy(21,3);printf("exp:      ");
     gotoxy(25,3);scanf("%d",&q->exp);
     gotoxy(33,3);printf("                 ");
     }
      gotoxy(43,3);printf("               ");
      gotoxy(10,4);printf("choose1:     choose2:     choose3:         ");
      gotoxy(x+m,y+1);printf("%d",q->coef);
      gotoxy(x+m,y+2);printf("%d",q->exp);
      m=m+5;
      head->n++;p->next=q;p=q;q->next=NULL;
      gotoxy(18,4);getchar();
      gotoxy(18,4);ch=getchar();
     }
 return(head);
 }
void add_plinklist(plinklist pa,plinklist pb)
{
 pnode p,q,pre,u;
 int s,x;

   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);
 }
void display(plinklist pa,int x,int y)
{
 pnode p;
 int s=5,i;
 p=pa->next;
 gotoxy(x,y);printf("coef:");
 gotoxy(x,y+1);printf("exp:");
 while(p)
 {
  gotoxy(x+s,y);printf("%d",p->coef);
  gotoxy(x+s,y+1);printf("%d",p->exp);
  s=s+5;
  p=p->next;
  }
 }
main()
{
 plinklist pa,pb;
 int x,y,number=1;
 char ch,str[20];
 clrscr();
 gotoxy(10,3);printf("_____welcome to add ploy(huangjinhua)_____");
 gotoxy(10,5);printf("function:poly_1+ploy_2+ploy_3+.......poly_i");
 gotoxy(10,7);printf("warninig:");
 gotoxy(10,8);printf("1.sheng mi su ru");
 gotoxy(10,9);printf("2.press 'n' to end input a poly");
 gotoxy(10,10);printf("3.press 'n' to end input total poly");
 gotoxy(10,11);printf("4.press 'esc' to exit add");
 gotoxy(10,13);printf("press any key to continue");
 getch();
 do
 {
   x=10,y=6;
   clrscr();
   gotoxy(10,6);printf("input ");
   pa=initial(x,y);y+=2;
   pb=initial(x,y);y+=2;
   add_plinklist(pa,pb);
   gotoxy(31,4);getchar();gotoxy(31,4);ch=getchar();
   while(ch!='n')
    {pb=initial(x,y);
     add_plinklist(pa,pb);
     y=y+2;
     gotoxy(31,4);getchar();gotoxy(31,4);ch=getchar();}
   gotoxy(x,y+1);printf("output:");
   display(pa,x,y+2);
   gotoxy(44,4);scanf("%s",str);
  }while(strcmp(str,"esc")!=0);
}




⌨️ 快捷键说明

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