重学数据结构001——链表基本操作与一元多项式相加

1.链表的基本操作

        链表数据结构的定义:由于链表一方面需要在节点中存储数据,另一方面还需要存储"线索",因此,通常采用结构体定义链表节点数据类型。

 

 
 
  1. struct Node; 
  2. typedef struct Node *PtrToNode; 
  3. typedef PtrToNode List; 
  4. typedef PtrToNode Position; 
  5. typedef int ElementType; 
  6. struct Node  
  7.     ElementType Element; 
  8.     Position Next; 
  9. }; 

        链表的操作不算太多,下面是一些常用的操作:

 

 
 
  1. //创建链表 
  2. List CreateList(); 
  3. //遍历链表 
  4. void TraverseList(List L); 
  5. //清空链表 
  6. List MakeEmpty(List L); 
  7. //判断给定列表是否为空 
  8. int IsEmpty(List L); 
  9. //判断节点在给定链表中是否为空 
  10. int IsLast(Position P, List L); 
  11. //在指定的链表中查找给定元素。 
  12. //存在则返回其第一次出现的位置,不存在则返回NULL 
  13. Position Find(ElementType X, List L); 
  14. //删除链表中的某个元素 
  15. void Delete(ElementType X, List L); 
  16. //在指定链表中查找给定元素的前驱节点 
  17. Position FindPrevious(ElementType X, List L); 
  18. //在链表给定位置的后面插入元素 
  19. void Insert(ElementType X, List L, Position P); 
  20. //删除链表 
  21. void DeleteList(List L); 
  22. //返回链表的头结点 
  23. Position Header(List L); 
  24. //返回链表第一个数据元素节点 
  25. Position First(List L); 
  26. //返回当前位置的下一个位置 
  27. Position Advance(Position P); 
  28. //获取当前位置元素的值 
  29. ElementType Retrive(Position P); 

        下面是实现基本操作以及简单使用的一个完整的例子:

 

 
 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3.  
  4. struct Node; 
  5. typedef struct Node *PtrToNode; 
  6. typedef PtrToNode List; 
  7. typedef PtrToNode Position; 
  8. typedef int ElementType; 
  9. struct Node  
  10.     ElementType Element; 
  11.     Position Next; 
  12. }; 
  13.  
  14. //创建链表 
  15. List CreateList(); 
  16. //遍历链表 
  17. void TraverseList(List L); 
  18. //清空链表 
  19. List MakeEmpty(List L); 
  20. //判断给定列表是否为空 
  21. int IsEmpty(List L); 
  22. //判断节点在给定链表中是否为空 
  23. int IsLast(Position P, List L); 
  24. //在指定的链表中查找给定元素。 
  25. //存在则返回其第一次出现的位置,不存在则返回NULL 
  26. Position Find(ElementType X, List L); 
  27. //删除链表中的某个元素 
  28. void Delete(ElementType X, List L); 
  29. //在指定链表中查找给定元素的前驱节点 
  30. Position FindPrevious(ElementType X, List L); 
  31. //在链表给定位置的后面插入元素 
  32. void Insert(ElementType X, List L, Position P); 
  33. //删除链表 
  34. void DeleteList(List L); 
  35. //返回链表的头结点 
  36. Position Header(List L); 
  37. //返回链表第一个数据元素节点 
  38. Position First(List L); 
  39. //返回当前位置的下一个位置 
  40. Position Advance(Position P); 
  41. //获取当前位置元素的值 
  42. ElementType Retrive(Position P); 
  43.  
  44. int IsEmpty(List L) 
  45.      
  46.     return L->Next == NULL; 
  47.  
  48. int IsLast(Position P, List L) 
  49.     return P->Next == NULL; 
  50.  
  51. Position Find(ElementType X, List L) 
  52.     Position P = L->Next; 
  53.     while(P != NULL && P->Element != X) 
  54.     { 
  55.         P = P->Next; 
  56.     } 
  57.     return P; 
  58.  
  59. void Delete(ElementType X, List L) 
  60.     Position P,TmpCell; 
  61.     P = FindPrevious(X,L);   
  62.     if(!IsLast(P,L)) 
  63.     { 
  64.         TmpCell = P->Next; 
  65.         P->Next = TmpCell->Next; 
  66.         free(TmpCell); 
  67.     } 
  68.  
  69. Position FindPrevious(ElementType X, List L) 
  70.     Position P = L; 
  71.     while(P->Next != NULL && P->Next->Element != X) 
  72.     { 
  73.         P = P->Next; 
  74.     } 
  75.     return P; 
  76.  
  77. void Insert(ElementType X, List L, Position P) 
  78.     Position TmpCell; 
  79.     TmpCell = malloc(sizeof(struct Node)); 
  80.     if(TmpCell == NULL) 
  81.     { 
  82.         printf("Out of space!\n"); 
  83.         return
  84.     } 
  85.     TmpCell->Element = X; 
  86.     TmpCell->Next = P->Next; 
  87.     P->Next = TmpCell; 
  88.  
  89. void DeleteList(List L) 
  90.     Position P,Tmp; 
  91.     P = L->Next; 
  92.     L->Next = NULL; 
  93.     while(P != NULL) 
  94.     { 
  95.         Tmp = P->Next; 
  96.         free(P); 
  97.         P = Tmp; 
  98.     } 
  99.  
  100. Position Header(List L) 
  101.     return L; 
  102.  
  103. Position First(List L) 
  104.     return L->Next; 
  105.  
  106. Position Advance(Position P) 
  107.     return P->Next; 
  108.  
  109. ElementType Retrive(Position P) 
  110.     return P->Element; 
  111.  
  112. List CreateList() 
  113.     int i; 
  114.     Position P,Tmp; 
  115.     List L = malloc(sizeof(struct Node)); 
  116.     P = L; 
  117.     for(i = 0; i < 5; i++) 
  118.     { 
  119.         Tmp = malloc(sizeof(struct Node)); 
  120.         Tmp->Element = i; 
  121.         P->Next = Tmp; 
  122.         P = Tmp;         
  123.     } 
  124.     P->Next = NULL; 
  125.     return L; 
  126.  
  127. void TraverseList(List L) 
  128.     Position P; 
  129.     P = L->Next; 
  130.     while(P != NULL) 
  131.     { 
  132.         printf("%d\n",P->Element);   
  133.         P = P->Next; 
  134.     } 
  135.  
  136. int main(void
  137.     //创建链表 
  138.     List L = CreateList(); 
  139.     //查找元素1在链表中的位置 
  140.     Position P = Find(1,L); 
  141.     //在元素1后面插入元素8 
  142.     Insert(8,L,P); 
  143.     //查找元素8前驱结点 
  144.     P = FindPrevious(8,L); 
  145.     //遍历链表 
  146.     TraverseList(L); 
  147.     return 0; 

2.一元N次多项式相加

        对于两个一元多项式,如果需要对他们进行多项式相加操作,常见的两种思路如下:(1)对于一个多项式,保存其最高项次数HighPowder,以及一个该多项式对应次数分别为0-HighPowder的各项的系数的数组()。(2)多项式中系数不为零的每一项,保存其系数与该项的次数。下面分别用这两种思路实现一元多项式加法操作。

思路一:

数据结构定义:

 

 
 
  1. typedef struct Poly 
  2.     int CoeffArray[11]; 
  3.     int HighPower; 
  4. } *Polynomial; 

实现代码:

 

 
 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3.  
  4. typedef struct Poly 
  5.     int CoeffArray[11]; 
  6.     int HighPower; 
  7. } *Polynomial; 
  8.  
  9. void ZeroPolynomial(Polynomial Poly) 
  10.     int i; 
  11.     for(i = 0; i < 11; i++) 
  12.     { 
  13.         Poly->CoeffArray[i] = 0; 
  14.     } 
  15.     Poly->HighPower = 0; 
  16.  
  17. void AddPolynomial(Polynomial Poly1,Polynomial Poly2, Polynomial PolySum) 
  18.     int i; 
  19.     ZeroPolynomial(PolySum); 
  20.     PolySum->HighPower = Poly1->HighPower > Poly2->HighPower? 
  21.         Poly1->HighPower:Poly2->HighPower; 
  22.     for(i = PolySum->HighPower; i >= 0 ; i--) 
  23.     { 
  24.         PolySum->CoeffArray[i] = Poly1->CoeffArray[i] + Poly2->CoeffArray[i]; 
  25.     } 
  26.  
  27. int main(void
  28.     int i,j,k; 
  29.     Polynomial P1,P2,Sum; 
  30.     P1 = malloc(sizeof(struct Poly)); 
  31.     P2 = malloc(sizeof(struct Poly)); 
  32.     Sum = malloc(sizeof(struct Poly)); 
  33.     //初始化 
  34.     ZeroPolynomial(P1); 
  35.     ZeroPolynomial(P2); 
  36.     P1->HighPower = 10; 
  37.     for(i = 10; i >= 0; i--) 
  38.     { 
  39.         P1->CoeffArray[i] = i; 
  40.     } 
  41.      
  42.     P2->HighPower = 8; 
  43.     for(j = 8; j >=0; j--) 
  44.     { 
  45.         P2->CoeffArray[j] = j; 
  46.     } 
  47.     P2->CoeffArray[8] = 8; 
  48.     AddPolynomial(P1,P2,Sum); 
  49.  
  50.     printf("The high power of the Polynomial is %d\n",Sum->HighPower); 
  51.     for(k = 0; k <= 10; k++) 
  52.     { 
  53.         printf("The Coeff of power %d is %d\n",k,Sum->CoeffArray[k]); 
  54.     } 
  55.  
  56.     return 0; 

思路二:

数据结构:

 

 
 
  1. typedef struct PolyNode *PtrToNode; 
  2.  
  3. //定义链表节点,也就是多项式中的某一项; 
  4. typedef struct PolyNode 
  5.     int Coeff; 
  6.     int Exponent; 
  7.     PtrToNode Next; 
  8. } PolyNode; 

实现代码:

 

 
 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3.  
  4. typedef struct PolyNode *PtrToNode; 
  5.  
  6. //定义链表节点,也就是多项式中的某一项; 
  7. typedef struct PolyNode 
  8.     int Coeff; 
  9.     int Exponent; 
  10.     PtrToNode Next; 
  11. } PolyNode; 
  12.  
  13.  
  14. typedef PtrToNode Polynomial; 
  15.  
  16. /************************************************************ 
  17. *多项式相加的函数: 
  18. *P、Q为存储两个多项式各项的单链表(含头结点) 
  19. *Sum为多项式相加结果存放的单链表 
  20. * 
  21. ************************************************************/ 
  22. void AddPolynomial(Polynomial P,Polynomial Q,Polynomial Sum) 
  23.     Polynomial PIndex,QIndex,SumIndex; 
  24.     PIndex = P->Next; 
  25.     QIndex = Q->Next; 
  26.     SumIndex = Sum; 
  27.     while(!(PIndex == NULL && QIndex == NULL)) 
  28.     { 
  29.         if(PIndex==NULL) 
  30.         { 
  31.             SumIndex->Next = QIndex; 
  32.             QIndex = QIndex->Next; 
  33.             SumIndex = SumIndex->Next; 
  34.         } 
  35.         else if(QIndex == NULL) 
  36.         { 
  37.             SumIndex->Next = PIndex; 
  38.             PIndex = PIndex->Next; 
  39.             SumIndex = SumIndex->Next; 
  40.         } 
  41.         else 
  42.         { 
  43.             if(PIndex->Exponent > QIndex->Exponent) 
  44.             { 
  45.                 SumIndex->Next = PIndex; 
  46.                 PIndex = PIndex->Next; 
  47.                 SumIndex = SumIndex->Next; 
  48.                 //continue在判断下面if条件时会有异常,类似Java 
  49.                 //的空引用异常 
  50.                 continue
  51.             } 
  52.             if(PIndex->Exponent == QIndex->Exponent) 
  53.             { 
  54.                 Polynomial PP = malloc(sizeof(struct PolyNode)); 
  55.                 PP->Exponent = PIndex->Exponent; 
  56.                 PP->Coeff = PIndex->Coeff + QIndex->Coeff; 
  57.                 SumIndex->Next = PP; 
  58.                 PIndex = PIndex->Next; 
  59.                 QIndex = QIndex->Next; 
  60.                 SumIndex = SumIndex->Next; 
  61.                 continue
  62.             } 
  63.             if(PIndex->Exponent < QIndex->Exponent) 
  64.             { 
  65.                 SumIndex->Next = QIndex; 
  66.                 QIndex = QIndex->Next; 
  67.                 SumIndex = SumIndex->Next; 
  68.                 continue
  69.             } 
  70.         } 
  71.     } 
  72.     SumIndex->Next = NULL; 
  73.  
  74. /************************************************************ 
  75. *遍历单链表(含头结点)函数: 
  76. *P:待遍历的链表 
  77. *************************************************************/ 
  78. void TraversePolynomial(Polynomial P) 
  79.     Polynomial Tmp = P->Next; 
  80.     while(Tmp != NULL) 
  81.     { 
  82.         printf("Coeff is %d and Exponent is %d\n",Tmp->Coeff,Tmp->Exponent); 
  83.         Tmp = Tmp->Next; 
  84.     } 
  85.  
  86.  
  87.  
  88. int main(void
  89.     Polynomial Poly1,Poly2,Poly3,Poly11,Poly22; 
  90.     int i,j; 
  91.     Poly1 = malloc(sizeof(struct PolyNode)); 
  92.     Poly2 = malloc(sizeof(struct PolyNode)); 
  93.     Poly3 = malloc(sizeof(struct PolyNode)); 
  94.     Poly11 = Poly1; 
  95.     Poly22 = Poly2; 
  96.  
  97.     //创建两个链表时,需要保证是按照指数递减的方式构造的 
  98.     for(i = 5;i >= 1;i--) 
  99.     { 
  100.         Polynomial Tmp  = malloc(sizeof(struct PolyNode)); 
  101.         Tmp->Coeff = i; 
  102.         Tmp->Exponent = i; 
  103.         Poly11->Next = Tmp; 
  104.         Poly11 = Poly11->Next; 
  105.     } 
  106.     Poly11->Next = NULL; 
  107.     for(j = 11;j >= 3;j--) 
  108.     { 
  109.         Polynomial Tmp  = malloc(sizeof(struct PolyNode)); 
  110.         Tmp->Coeff = j; 
  111.         Tmp->Exponent = j; 
  112.         Poly22->Next = Tmp; 
  113.         Poly22 = Poly22->Next; 
  114.     } 
  115.     Poly22->Next = NULL; 
  116.     TraversePolynomial(Poly1); 
  117.     printf("*****************************************\n"); 
  118.     TraversePolynomial(Poly2); 
  119.     AddPolynomial(Poly1,Poly2,Poly3); 
  120.     printf("*****************************************\n"); 
  121.     TraversePolynomial(Poly3); 
  122.     return 0; 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值