1. 程式人生 > 實用技巧 >鏈式線性表——實驗及提升訓練

鏈式線性表——實驗及提升訓練

#ifndef _LAB1_H_
#define  _LAB1_H_
#include <stdlib.h>
#include <stdio.h>



//存放多項式某項的結點結構 
struct node
{
    int exp ;  // 表示指數
    int coef ; //表示係數
    struct node *next;  //指向下一個結點的指標
};

typedef  struct node * PNODE ;

/*
  函式功能:生成多項式
  函式名:createPoly
  函式引數:無
  返回值:指向多項式的頭指標 
*/
PNODE createPoly(
void) { //在此處填寫程式碼,能實現建立一個多項式並返回多項式頭指標的函式 //注意:頭指標不存放多項式的項。 /********** Begin **********/ PNODE head=(PNODE)malloc(sizeof(struct node)); head->next=NULL; PNODE p=head; int e; int c; scanf("%d%d",&c,&e); for(;c!=0;){ PNODE node=(PNODE)malloc(sizeof
(struct node)); node->exp=e; node->coef=c; node->next=NULL; p->next=node; p=p->next; // fflush(stdin); scanf("%d%d",&c,&e); } return head; /********** End **********/ } /* 函式功能:進行多項式相加 函式名:addPoly 函式引數:polyAddLeft :加法左邊多項式頭指標, polyAddRight:加法右邊多項式頭指標 返回值:指向結果多項式的頭指標
*/ PNODE addPoly(PNODE polyAddLeft , PNODE polyAddRight) { //在此處填寫程式碼,能實現創兩個多項式相加並返回結果多項式頭指標的函式 /********** Begin **********/ PNODE ans=(PNODE)malloc(sizeof(struct node)); ans->next=NULL; PNODE p,q; p=polyAddLeft->next; q=polyAddRight->next; PNODE n=ans; for(;p!=NULL&&q!=NULL;){ if(q->exp>p->exp){ PNODE node=(PNODE)malloc(sizeof(struct node)); node->next=NULL; n->next=node; n=n->next; node->exp=p->exp; node->coef=p->coef; p=p->next; continue; } else if(q->exp<p->exp){ PNODE node=(PNODE)malloc(sizeof(struct node)); node->next=NULL; n->next=node; n=n->next; node->exp=q->exp; node->coef=q->coef; q=q->next; continue; } else{ if(p->coef+q->coef!=0){ PNODE node=(PNODE)malloc(sizeof(struct node)); node->next=NULL; n->next=node; n=n->next; node->exp=q->exp; node->coef=q->coef+p->coef; } q=q->next; p=p->next; } } if(p!=NULL){ q=p; } while(q!=NULL){ PNODE node=(PNODE)malloc(sizeof(struct node)); node->next=NULL; n->next=node; n=n->next; node->exp=q->exp; node->coef=q->coef; q=q->next; } return ans; /********** End **********/ } /* 函式功能:輸出多項式 函式名:printPoly 函式引數:待輸出多項式的頭指標poly 返回值:無 */ void printPoly(PNODE poly) { //在此處填寫程式碼,能實現按格式輸出多項式的功能,輸出格式樣例見說明 /********** Begin **********/ PNODE p; p=poly->next; while(p!=NULL){ printf("%dx^%d",p->coef,p->exp); if(p->next!=NULL){ printf("+"); } p=p->next; } /********** End **********/ } void destroyPoly(PNODE poly) { //釋放儲存多項式的連結串列空間 PNODE q,p; p=q=poly; while(p!=NULL){ p=p->next; free(q); q=p; } free(poly); } #endif
View Code
#ifndef _LINKSET_H_
#define _LINKSET_H_
#include <stdio.h>
#include <stdlib.h>

typedef int DataType;

struct  node
{
    DataType  element;
    struct node  *next;
};
typedef struct node * SET;
void insert(DataType datax, SET set);

/*
  函式名: InitSet
  函式功能:根據引數num,初始化集合
  函式引數:集合元素的個數 
  返回值:集合頭指標 
*/
SET InitSet(int num)
{

    SET p;
    p = ( struct  node *)malloc(sizeof(struct node)) ;
    p->next = NULL;
    p->element = num;
    int temp;
    for(int i =0;i<num;i++)
    {
        scanf("%d",&temp);
        insert(temp, p); //呼叫insert函式,將輸入資料插入集合
    }
    return p;
}


/*
  函式名: find
  函式功能:在集合中查詢值為datax的成員 
  函式引數:datax:待查詢的值 ; set:集合的頭結點 
  返回值:找到值為datax的成員返回1,否則返回0 
*/
int find(DataType datax, SET  set)
{
    //請在此處填寫程式碼,在set集合中查詢值為datax的成員,若找到返回1,否則返回0
    /**********  Begin  **********/

    if(set==NULL){
        return 0;
    }
    SET p;
    p=set->next;
    while(p!=NULL){
        if(p->element==datax){
            return 1;
        }
        p=p->next;
    }
    return  0;
    /**********   End   **********/
}

/*
  函式名: insert
  函式功能:在集合set中插入值為datax的成員 ,插入位置在表頭 
  函式引數:datax:待插入的值 ; set:集合的頭結點 
  返回值:無 
  時間複雜度:O(1)
*/
void insert(DataType datax, SET set)
{
    //請在此處填寫程式碼,將datax插入集合set, 注意因集合元素是無序的,只需將新成員插入表頭
    /**********  Begin  **********/

    if(set->element<=0){
        return;
    }
    SET s=( struct  node *)malloc(sizeof(struct node)) ;
    s->element=datax;
    s->next=NULL;
    SET p=set;
    //p=p->next;
    while(p->next!=NULL){
        p=p->next;
    }

    p->next=s;

    /**********   End   **********/
}

/*
  函式名: copyList
  函式功能:將集合setA複製生成集合setB 
  函式引數:setA 、setB的頭結點 
  返回值:無 
*/
void copySet(SET setA, SET setB)
{
    //請在此處填寫程式碼,實現將集合setA的成員複製到集合setB的功能
    /**********  Begin  **********/
    if(setA==NULL){
        return;
    }
    SET p=setA->next;

    while (p->next!=NULL){
        insert(p->element,setB);
        setB->element++;
        p=p->next;
    }



    /**********   End   **********/
}

/*
  函式名: printSet
  函式功能:輸出集合的元素,以空格作為元素之間分界符 
  函式引數:set的頭結點 
  返回值:無 
*/
void printSet(SET set)
{
    //請在此處填寫程式碼,實現輸出集合元素的功能,元素之間以空格為分界符
    /**********  Begin  **********/
 SET p=set->next;
 while(p!=NULL){
     int n=p->element;
     printf("%d ",n);
     p=p->next;
 }

    /**********  End  **********/
}

/*
  函式名: setUnion
  函式功能:求兩個集合setA 和 setB的並集 
  函式引數:setA和setB的頭結點 
  返回值:並集集合的頭結點 
*/
SET setUnion(SET setA ,SET setB)
{
    //請在此處填寫程式碼,可直接使用上面已經實現的各操作
    /**********  Begin  **********/
    //copySet(setA,setB);
    SET p,q;
    p=setA;
    p=p->next;
    q=setB;
    q=q->next;
    SET ret=(SET)malloc(sizeof(struct node));
    ret->next=NULL;
    ret->element=0;
    SET tmp=ret;
        while (q!=NULL){//將B中非A元素放入鏈棧
            if(!find(q->element,setA)) {
                SET n = (SET) malloc(sizeof(struct node));
                n->element = q->element;
                n->next = NULL;
                ret->element++;
                tmp->next = n;
                tmp = tmp->next;
            }
                q = q->next;
        }
    while (p!=NULL){//將A所有(非鏈棧)元素放入鏈棧
        if(!find(p->element,ret)){
        SET n=(SET)malloc(sizeof(struct node));
        n->element=p->element;
        n->next=NULL;
        ret->element++;
        tmp->next=n;
        tmp=tmp->next;
        }
        p=p->next;
    }

    return ret;

    /**********  End  **********/
}

/*
  函式名: setIntersect
  函式功能:求兩個集合setA 和 setB的交集 
  函式引數:setA和setB的頭結點 
  返回值:交集集合的頭結點 
*/
SET setIntersect(SET setA ,SET setB)
{
    //請在此處填寫程式碼,可直接使用上面已經實現的各操作
    /**********  Begin  **********/
SET p,q,ans;


ans=( struct  node *)malloc(sizeof(struct node)) ;
    ans->next = NULL;
    ans->element=0;
p=ans;
        for (q=setB->next;q!=NULL;q=q->next){
    if(find(q->element,setA)){
        SET node=( struct  node *)malloc(sizeof(struct node)) ;
        node->element=q->element;
        node->next=NULL;
    p->next=node;
    p=p->next;
    ans->element++;
    }
        }
    return ans;
    /**********  End  **********/
}

/*
  函式名: setExcept
  函式功能:求兩個集合setA 和 setB的差 
  函式引數:setA和setB的頭結點 
  返回值:結果集合的頭結點 
*/
SET setExcept(SET setA ,SET setB)
{
    //請在此處填寫程式碼,可直接使用上面已經實現的各操作
    /**********  Begin  **********/
    SET p,q,ans;

//返回A有B無!!!
    ans=( struct  node *)malloc(sizeof(struct node)) ;
    ans->next = NULL;
    ans->element=0;
    p=ans;
    for (q=setA->next;q!=NULL;q=q->next){
        if(!find(q->element,setB)){
            SET node=( struct  node *)malloc(sizeof(struct node)) ;
            node->element=q->element;
            node->next=NULL;
            p->next=node;
            p=p->next;
            ans->element++;
        }
    }
    return ans;

    /**********  End  **********/
}

void destroySet(SET set)
{
//釋放儲存集合的連結串列空間,表頭為set
SET p,q;
p=q=set;
while (p->next!=NULL){
    p=p->next;
    free(q);
    q=p;
}
//free(set);
}

#endif
View Code