记录我的数据结构(C语言)学习历程(2017年3月30号开始)

//List.c 

#include "List.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

/*struct Node
{
    ElementType Element;
    Position Next;
};*/

void CreateList(List *L,int n)
{
    Position P,P1;
    int i;
    *L=(Position)malloc(sizeof(struct Node));
    (*L)->Next=NULL;
    P1=*L;
    printf("请输入%d对数据(以空格隔开):\n",n);                    //没问题 
    for(i=n;i>0;i--)
    {
        //P=(Position)malloc(sizeof(struct Node));
        //scanf("%d",&P->Element);                //前插 
        //P->Next=(*L)->Next;
        //(*L)->Next=P;
        
        P=(Position)malloc(sizeof(struct Node));
        scanf("%d",&P->Cofficient);
        scanf("%d",&P->Exponent);
        P->Next=P1->Next;                       //后插 
        P1->Next=P;
        P1=P;
    
    }
}

void PrintList(List L)
{
    printf("已保存链表\n");
    Position P;
    P=L->Next;
    while(P->Next!=NULL)
    {
        printf("%d ",P->Cofficient);              //没问题 
        printf("%d ",P->Exponent);
        P=P->Next;
    }
    printf("%d ",P->Cofficient);              //没问题 
    printf("%d ",P->Exponent);
}

void FatalError(char a[])
{
    printf("%s",a);                           //没问题 
}

int IsEmpty(List L)
{
    return L->Next==NULL;                     //没问题 
}

int IsLast(Position P,List L)
{
    return P->Next==NULL;                   //没问题 
}

List MakeEmpty(List L)
{
    List q,p;
    p=L->Next;
    while(p!=NULL)
    {                                            //没问题 
        q=p->Next;
        free(p);
        p=q;
    }
    L->Next=NULL;
    
    return L;
}

Position Find(ElementType X,List L)
{
    Position P;
    P=L->Next;
    while((P!=NULL)&&(P->Exponent!=X))        //没问题 
    {
        P=P->Next;
    }
    
    return P;
}

Position findPrevious(ElementType X,List L)
{
    Position P;
    P=L;
    while((P->Next!=NULL)&&(P->Next->Exponent!=X))    //没问题 
    {
        P=P->Next;
    }
    return P;
}

void Delete(ElementType X,ElementType Y,List L)
{
    Position P,TmpCell;
    P=findPrevious(X,L);
    if(!IsLast(P,L))
    {
        TmpCell=P->Next;
        P->Next=TmpCell->Next;                   //没问题 
        free(TmpCell);
    }
}
void DeleteList(List L)
{
    Position P,Q;
    P=L->Next;
    L->Next=NULL;
    while(P!=NULL)
    {
        Q=P->Next;                               //没问题 
        free(P);
        P=Q;
    }
}
void Insert(ElementType X,ElementType Y,List L,Position P)
{
    if(L==NULL)
    {
        return;
    } 
    Position TmpCell;
    TmpCell=malloc(sizeof(struct Node));
    if(TmpCell!=NULL)
    {
        TmpCell->Next=P->Next;                   //前插,没问题 
        TmpCell->Cofficient=X;
        TmpCell->Exponent=Y;
        P->Next=TmpCell;
    }
    else
    {
        FatalError("out of space!!!");
    } 
}
void InsertH(ElementType X,ElementType Y,List L,Position P)
{
    if(L==NULL)
    {
        return;
    } 
    Position TmpCell;
    List q;
    TmpCell=malloc(sizeof(struct Node));
    q=L;                                            //后插,没问题 
    while(q->Next!=NULL)
    {
    
        q=q->Next;
    }
    q->Next=TmpCell;
    TmpCell->Next=NULL;
    TmpCell->Cofficient=X;
    TmpCell->Exponent=Y;
}
void CreateEmptyList(List *L)
{
    *L=(Position)malloc(sizeof(struct Node));           //没问题 
    (*L)->Next=NULL;
}
List MultPolynomial(List L1,List L2)
{
    List p=L1->Next;
    List q=L2->Next;
    List L3;
    List SOM;
    
    int Cof,Exp;
    CreateEmptyList(&L3);
    
    while(p!=NULL)
    {                                                  //没问题 
        q=L2->Next;
        while(q!=NULL)
        {
            Cof=p->Cofficient*q->Cofficient;
            Exp=p->Exponent+q->Exponent;
            
            //printf("1-1") ;
            if((SOM=Find(Exp,L3))==NULL)
            {
                InsertH(Cof,Exp,L3,L3);
            }
            else
            {
                SOM->Cofficient=+Cof;
                SOM->Exponent=+Exp;
            }
            q=q->Next;
        } 
        p=p->Next;
    }
    return L3;
}
-------------------------我是分割线--------------------------

//List.h
#ifndef _LIST_H_
typedef int ElementType;

struct Node;

typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

/*struct Node
{
    ElementType Element;
    Position Next;
};*/
struct Node
{
    ElementType Cofficient;
    ElementType Exponent;
    Position Next;
};

void CreateList(List *L,int n);
void PrintList(List L);

List MakeEmpty(List L);
void CreateEmptyList(List *L);
int IsEmpty(List L);
int IsLast(Position P,List L);
Position Find(ElementType X,List L);
void Delete(ElementType X,ElementType Y,List L);
Position FindPrevious(ElementType X,List L);
void Insert(ElementType X,ElementType Y,List L,Position P);
void InsertH(ElementType X,ElementType Y,List L,Position P);
void DeleteList(List L);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
ElementType Retrieve(Position P);

List MultPolynomial(List L1,List L2); 

#endif

-------------------------我是分割线--------------------------

//main.c
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include "List.h"

int main(void)
{
    //char a[]="Error"; 
    //int i;
//    FatalError(a);
    //List L,q;
//    CreateList(&L,8);
//    PrintList(L);
//    InsertH(6,L,L);
    //printf("%d\n",IsEmpty(L)); 
    //q=Find(7,L); 
    //i=q->Element;
    //printf("%d",i);
//    PrintList(L);
//    MakeEmpty(L); 
    //DeleteList(L);
    //printf("%d\n",IsEmpty(L));
    List L1_1,L2_1,L3_1;
    ElementType n1,n2;
    
    printf("请输入第一个多项式的项数:");
    scanf("%d",&n1); 
    printf("\n");
    printf("请输入第一个多项式的系数和指数:"); 
    printf("\n");
    CreateList(&L1_1,n1);
    PrintList(L1_1);
    printf("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
    printf("\n请输入第二个多项式的项数:");
    scanf("%d",&n2); 
    printf("\n");
    printf("请输入第二个多项式的系数和指数:"); 
    printf("\n");
    CreateList(&L2_1,n2);
    PrintList(L2_1);
    
    printf("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
    L3_1=MultPolynomial(L1_1,L2_1);
    PrintList(L3_1);
}

-------------------------我是分割线--------------------------

多项式1:10 x^1000+5 x^14+1 x^0

多项式2:3 x^1990-2 x^1492+11 x^1+5 x^0

运行结果:

请输入第一个多项式的项数:3

请输入第一个多项式的系数和指数:
请输入3对数据(以空格隔开):
10 1000 5 14 1 0
已保存链表
10 1000 5 14 1 0
%%%%%%%%%%%%%%%%%%
请输入第二个多项式的项数:4

请输入第二个多项式的系数和指数:
请输入4对数据(以空格隔开):
3 1990 -2 1492 11 1 5 0
已保存链表
3 1990 -2 1492 11 1 5 0
%%%%%%%%%%%%%%%%%%已保存链表
30 2990 -20 2492 110 1001 50 1000 15 2004 -10 1506 55 15 25 14 3 1990 -2 1492 11 1 5 0
--------------------------------
Process exited after 40.15 seconds with return value 2
请按任意键继续. . .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值