当前位置:   article > 正文

头歌 数据结构_递归实现单链表的基本操作头歌

递归实现单链表的基本操作头歌

以下程序仅供参考学习,请勿抄袭!

数据结构---作业实训-chp3-T3.1&3.3(a)-单链表的建立,遍历,交换和逆置

本关任务: 编写函数,实现单链表的逆置。

  1. void Swap(List head)
  2. {
  3. // 请在此添加代码,完成对已建立的单链表的逆置
  4. /********** Begin *********/
  5. position last;
  6. position new_l;
  7. int isprime=1;
  8. new_l=(position)malloc(sizeof(a));
  9. for(last=head->next;last;last=last->next)
  10. {
  11. position s;
  12. s=(position)malloc(sizeof(a));
  13. s->data=last->data;
  14. if(isprime)
  15. {
  16. s->next=NULL;
  17. isprime=0;
  18. }
  19. else
  20. {
  21. s->next=new_l->next;
  22. }
  23. new_l->next=s;
  24. }
  25. head->next=new_l->next;
  26. /********** End **********/
  27. }

本关任务: 编写函数,通过改变指针的指向来实现单链表的n位置和n+1位置结点的交换。

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node *ptrtonode; //为struct Node取别名为ptrtonode,可以直接用类型ptrtonode来定义指针变量
  4. typedef ptrtonode List; //为ptrtonode取别名为List,可以直接用类型List类型来定义指针变量
  5. typedef ptrtonode position;
  6. struct Node //定义单播结点,结点为结构体,包含字符成员data和指针变量成员next
  7. {
  8. char data; //结点的数据域,用来存储一个字符
  9. position next; //结点的指针域,用来指向下一个结点
  10. }a; //定义结构体变量a
  11. /*
  12. 尾插法创建带头结点的单链表,单链表头指针为L指针
  13. */
  14. position creat()
  15. {
  16. position L; //定义结构体指针变量L,用来指向单链表头部
  17. position p1,p2; //定义结构体指针变量p1,p2,作为临时指针指向结点
  18. char data;
  19. L=(position)malloc(sizeof(a)); //申请一个结点大小的空间,用指针L指向这个空间首地址
  20. p2=L; //p2指针也指向L指的结点
  21. while((data=getchar())!='#') //从键盘获取字符存入变量data中,如果从键盘获取的字符不是'\#'则循环
  22. {
  23. p1=(position)malloc(sizeof(a)); //
  24. p1->data=data; //
  25. p2->next=p1; //
  26. p2=p1; //
  27. }
  28. p2->next=NULL; //p2指向的结点的指针域置空,表示是最后一个结点
  29. return L; //
  30. }
  31. /*
  32. 遍历单链表
  33. */
  34. void print(List head) //传入单链表头指针head,完成对单链表的遍历
  35. {
  36. position p;
  37. p=head->next; //临时指针变量p指向单链表的第一个结点
  38. while(p!=NULL)
  39. {
  40. printf("%c",p->data); //获取单链表中p指针指向的结点的数据域的字符并输出
  41. p=p->next; //p指针指向下一个结点
  42. }
  43. printf("\n");
  44. }
  45. /*
  46. 把单链表中n和n+1位置上的结点进行交换;
  47. n范围从1到单链表长度-1
  48. */
  49. void SwapSinglist(List head,int n)
  50. {
  51. // 请在此添加代码,实现单链表中n和n+1位置元素的交换(通过交换指针实现)
  52. /********** Begin *********/
  53. position last;
  54. position p;
  55. int i=0;
  56. char temp;
  57. for(last=head;last&&i<n;last=last->next,i++);
  58. p=last->next;
  59. temp=last->data;
  60. last->data=p->data;
  61. p->data=temp;
  62. /********** End **********/
  63. }
  64. void main()
  65. {
  66. position L=NULL;
  67. printf("请输入链表节点,以“#”结束:\n");
  68. L=creat(); //调用creat函数,完成单链表的创建
  69. printf("初始链表为:\n");
  70. print(L); //调用print函数,完成单链表的遍历
  71. int n;
  72. printf("请输入要交换的n位置(n<单链表长度),如1,2,3......\n");
  73. scanf("%d",&n);
  74. SwapSinglist(L,n);
  75. printf("%d和%d位置交换后链表为:\n",n,n+1);
  76. print(L);
  77. }

数据结构---作业实训-chp3-T3.25(实现队列:数组&链表实现)

本关任务: 编写队列基本程序,实现队列基本操作,创建队列、入队,出队,遍历,销毁队等,数组实现。

  1. #include "queue.h"///data/workspace/myshixun/chp3/fatal.h
  2. #include "fatal.h"
  3. #include <stdlib.h>
  4. #define MinQueueSize ( 5 )
  5. struct QueueRecord
  6. {
  7. int Capacity; //队列容量
  8. int Front; //队首,进行出队操作
  9. int Rear; //队尾,进行入队操作
  10. int Size; //队列实际大小
  11. ElementType *Array; //队列数组指针
  12. };
  13. /* START: fig3_58.txt */
  14. int
  15. IsEmpty( Queue Q )
  16. {
  17. // 请在此添加代码,队列空,返回1;否则返回0
  18. /********** Begin *********/
  19. return Q->Size==0?1:0;
  20. /********** End **********/
  21. }
  22. /* END */
  23. int
  24. IsFull( Queue Q )
  25. {
  26. // 请在此添加代码,队列满,返回1;否则返回0
  27. /********** Begin *********/
  28. return Q->Size==Q->Capacity?1:0;
  29. /********** End **********/
  30. }
  31. /*
  32. 队列的创建
  33. */
  34. Queue
  35. CreateQueue( int MaxElements )
  36. {
  37. Queue Q;
  38. /* 1*/ if( MaxElements < MinQueueSize )
  39. /* 2*/ Error( "Queue size is too small" );
  40. /* 3*/ Q = malloc( sizeof( struct QueueRecord ) );
  41. /* 4*/ if( Q == NULL )
  42. /* 5*/ FatalError( "Out of space!!!" );
  43. /* 6*/ Q->Array = malloc( sizeof( ElementType ) * MaxElements );
  44. /* 7*/ if( Q->Array == NULL )
  45. /* 8*/ FatalError( "Out of space!!!" );
  46. /* 9*/ Q->Capacity = MaxElements;
  47. /*10*/ MakeEmpty( Q );
  48. /*11*/ return Q;
  49. }
  50. /*
  51. START: fig3_59.txt
  52. 队列创建后各变量置初态
  53. */
  54. void
  55. MakeEmpty( Queue Q )
  56. {
  57. // 请在此添加代码,初始化队列的Size,Front,Rear值
  58. /********** Begin *********/
  59. Q->Size=0;
  60. Q->Front=Q->Rear=0;
  61. /********** End **********/
  62. }
  63. /* END */
  64. /*
  65. 队列的销毁
  66. */
  67. void
  68. DisposeQueue( Queue Q )
  69. {
  70. if( Q != NULL )
  71. {
  72. free( Q->Array );
  73. free( Q );
  74. }
  75. }
  76. /*
  77. START: fig3_60.txt
  78. 队列Q的Value值加1
  79. */
  80. static int
  81. Succ( int Value, Queue Q )
  82. {
  83. if( ++Value == Q->Capacity )//指针value加1后返回,如果加1后等于容量了返回0
  84. Value = 0;
  85. return Value;
  86. }
  87. /*
  88. 元素X入Q队
  89. */
  90. void
  91. Enqueue( ElementType X, Queue Q )
  92. {
  93. if( IsFull( Q ) )
  94. Error( "Full queue" );
  95. else
  96. {
  97. // 请在此添加代码,完成入队操作
  98. /********** Begin *********/
  99. Q->Array[Q->Rear]=X;
  100. Q->Rear=(Q->Rear+1)%Q->Capacity;
  101. Q->Size++;
  102. /********** End **********/
  103. }
  104. }
  105. /* END */
  106. /*
  107. 取队首元素
  108. */
  109. ElementType
  110. Front( Queue Q )
  111. {
  112. if( !IsEmpty( Q ) )
  113. return Q->Array[ Q->Front ];//返回队头元素值
  114. Error( "Empty queue" );
  115. return 0; /* Return value used to avoid warning */
  116. }
  117. /*
  118. 出队,不保存出队元素
  119. */
  120. void
  121. Dequeue( Queue Q )
  122. {
  123. if( IsEmpty( Q ) )
  124. Error( "Empty queue" );
  125. else
  126. {
  127. // 请在此添加代码,完成出队操作
  128. /********** Begin *********/
  129. Q->Front=(Q->Front+1)%Q->Capacity;
  130. Q->Size--;
  131. /********** End **********/
  132. }
  133. }
  134. /*
  135. 队首的元素出队后保存在变量X中可返回
  136. */
  137. ElementType
  138. FrontAndDequeue( Queue Q )
  139. {
  140. ElementType X = 0;
  141. if( IsEmpty( Q ) )
  142. Error( "Empty queue" );
  143. else
  144. {
  145. Q->Size--;
  146. X = Q->Array[ Q->Front ];//队头元素放入X后被返回
  147. Q->Front = Succ( Q->Front, Q );
  148. }
  149. return X;
  150. }
  151. /*
  152. 测试主函数
  153. */
  154. main( )
  155. {
  156. Queue Q;
  157. int i;
  158. Q = CreateQueue( 12 ); //创建队列
  159. printf("第一次入队(0~9):\n");
  160. for( i = 0; i < 10; i++ ) //0到9入列
  161. // 请在此添加代码,调用函数完成入队操作
  162. /********** Begin *********/
  163. Enqueue(i,Q);
  164. /********** End **********/
  165. printf("出队,即入队元素有:\n");
  166. while( !IsEmpty( Q ) ) //遍历出队元素
  167. {
  168. printf( "%d ", Front( Q ) );
  169. Dequeue( Q );
  170. }
  171. printf( "\n");
  172. printf("第二次入队(0,3,6,9):\n");
  173. for( i = 0; i < 10; i+=3 ) //0到9中3的倍数入列
  174. Enqueue( i, Q );
  175. printf( "\n");
  176. printf("出队,即入队元素有:\n");
  177. while( !IsEmpty( Q ) ) //创建队列
  178. {
  179. printf( "%d ", Front( Q ) );
  180. // 请在此添加代码,调用函数完成出队操作
  181. /********** Begin *********/
  182. Dequeue(Q);
  183. /********** End **********/
  184. }
  185. printf( "\n");
  186. DisposeQueue( Q ); //销毁队列
  187. return 0;
  188. }

本关任务: 编写队列基本程序,实现队列基本操作,创建队列、入队,出队,遍历,销毁队等,链表实现。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //定义节点
  4. typedef struct node
  5. {
  6. char data;
  7. struct node * next;
  8. }node;
  9. //定义队列(保存队首和队尾指针)
  10. typedef struct queue_link
  11. {
  12. // 请在此添加代码,定义队列首尾指针
  13. /********** Begin *********/
  14. node* front,*rear;
  15. /********** End **********/
  16. }LinkQueue;
  17. //初始化队列
  18. LinkQueue * InitQueue()
  19. {
  20. LinkQueue * q = (LinkQueue*)malloc(sizeof(LinkQueue));
  21. // 请在此添加代码,完成队列首尾指针赋值,级队列初始化
  22. /********** Begin *********/
  23. q->front=q->rear=NULL;
  24. /********** End **********/
  25. return q;
  26. }
  27. //判断队列是否为空
  28. int EmptyQueue(LinkQueue * q)
  29. {
  30. return q->front == NULL;
  31. }
  32. //入队
  33. void InsertQueue(LinkQueue *q, char data)
  34. {
  35. node * n = (node *)malloc(sizeof(node));
  36. n->data = data;
  37. n->next = NULL; //尾插法,插入元素指向空
  38. if(q->rear == NULL) //只有空头结点,即队中无元素
  39. {
  40. q->front = n;
  41. q->rear = n;
  42. }
  43. else{
  44. // 请在此添加代码,完成把新结点插入到尾部
  45. /********** Begin *********/
  46. q->rear->next=n;
  47. q->rear=n;
  48. /********** End **********/
  49. }
  50. }
  51. //出队(删除队首元素)
  52. void DeleteQueue(LinkQueue *q)
  53. {
  54. node * n = q->front;
  55. if(q->front == q->rear)//是否只有一个元素
  56. {
  57. q->front = NULL;
  58. q->rear = NULL;
  59. }
  60. else{
  61. // 请在此添加代码,完成出队操作
  62. /********** Begin *********/
  63. q->front=n->next;
  64. /********** End **********/
  65. }
  66. free(n);
  67. }
  68. //打印队列
  69. void Display(LinkQueue * q)
  70. {
  71. node * n = (node *)malloc(sizeof(node));
  72. n = q->front;
  73. while(n != NULL)
  74. {
  75. // 请在此添加代码,完成打印队列元素操作
  76. /********** Begin *********/
  77. printf("%c ",n->data);
  78. n=n->next;
  79. /********** End **********/
  80. }
  81. printf("\n");
  82. }
  83. int main()
  84. {
  85. int i=5,j=5;
  86. LinkQueue * q;
  87. q = InitQueue();
  88. printf("开始入队:\n");
  89. while(i--)
  90. {
  91. printf("元素%c入队,队列为:",'A'+i);
  92. // 请在此添加代码,调用入队函数完成入队并遍历入队后队列元素
  93. /********** Begin *********/
  94. InsertQueue(q,'A'+i);
  95. Display(q);
  96. /********** End **********/
  97. }
  98. printf("开始出队:\n");
  99. while(j--)
  100. {
  101. printf("第%d个元素出队,队列为:",5-j);
  102. // 请在此添加代码,调用出队函数完成出队并遍历剩下的队列元素
  103. /********** Begin *********/
  104. DeleteQueue(q);
  105. Display(q);
  106. /********** End **********/
  107. }
  108. printf("\n");
  109. return 0;
  110. }

数据结构---作业实训-chp3-T3.21(一个数组实现共享栈)

本关任务: 一个数组实现两个共享栈。

  1. //T3.21:一个数组实现两个共享栈
  2. //版权声明:本文为CSDN博主「雪碧柠七」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
  3. //原文链接:https://blog.csdn.net/ihaha233/article/details/80520823
  4. //rfh改编
  5. #include<stdio.h>
  6. #define Max_size 1000
  7. typedef char DataType;
  8. typedef struct shareStack
  9. {
  10. DataType data[Max_size];
  11. int top1;
  12. int top2;
  13. }shareStack;
  14. //初始化
  15. void shareStackInit(shareStack* stack)
  16. {
  17. if(stack == NULL)
  18. {
  19. //非法输入
  20. return;
  21. }
  22. stack->top1 = 0;
  23. stack->top2 = Max_size;
  24. return;
  25. }
  26. //打印栈1
  27. void shareStackPrint1(shareStack* stack,const char* msg)
  28. {
  29. printf("[%s]\n",msg);
  30. if(stack == NULL)
  31. {
  32. //非法输入
  33. return;
  34. }
  35. int i = 0;
  36. for(i = 0; i< stack->top1;i++)
  37. {
  38. printf("%c ",stack->data[i]);
  39. }
  40. printf("\n");
  41. }
  42. //打印栈2
  43. void shareStackPrint2(shareStack* stack,const char* msg)
  44. {
  45. printf("[%s]\n",msg);
  46. if(stack == NULL)
  47. {
  48. //非法输入
  49. return;
  50. }
  51. int i = Max_size;
  52. for(;i > stack->top2;i--)
  53. {
  54. // 请在此添加代码,输出栈2栈顶的内容
  55. /********** Begin *********/
  56. printf("%c ",stack->data[i]);
  57. /********** End **********/
  58. }
  59. printf("\n");
  60. }
  61. //栈1插入元素
  62. void shareStackPush1(shareStack* stack,DataType value)
  63. {
  64. if(stack == NULL)
  65. {
  66. //非法输入
  67. return;
  68. }
  69. if(stack->top1 == stack->top2)
  70. {
  71. //达到共享栈上限
  72. return;
  73. }
  74. // 请在此添加代码,完成栈1的入栈操作
  75. /********** Begin *********/
  76. stack->data[stack->top1++]=value;
  77. /********** End **********/
  78. return;
  79. }
  80. //栈2插入元素
  81. void shareStackPush2(shareStack* stack,DataType value)
  82. {
  83. if(stack == NULL)
  84. {
  85. return;
  86. }
  87. if(stack->top1 == stack->top2)
  88. {
  89. //达到共享栈上限
  90. return;
  91. }
  92. // 请在此添加代码,完成栈2的入栈操作
  93. /********** Begin *********/
  94. stack->data[stack->top2--]=value;
  95. /********** End **********/
  96. return;
  97. }
  98. //栈1出栈
  99. void shareStackPop1(shareStack* stack)
  100. {
  101. if(stack == NULL)
  102. {
  103. printf("空栈不能进行出栈\n");//非法输入
  104. return;
  105. }
  106. // 请在此添加代码,完成栈1的出栈操作
  107. /********** Begin *********/
  108. stack->top1--;
  109. /********** End **********/
  110. return;
  111. }
  112. //栈2出栈
  113. void shareStackPop2(shareStack* stack)
  114. {
  115. if(stack == NULL)
  116. {
  117. printf("空栈不能进行出栈\n");//非法输入
  118. return;
  119. }
  120. // 请在此添加代码,完成栈2的出栈操作
  121. /********** Begin *********/
  122. stack->top2++;
  123. /********** End **********/
  124. return;
  125. }
  126. //栈1取栈顶元素
  127. int shareStackGet1(shareStack* stack,char* value)
  128. {
  129. if(stack == NULL)
  130. {
  131. return 0;
  132. }
  133. *value = stack->data[stack->top1-1];
  134. return 1;
  135. }
  136. //栈2取栈顶元素
  137. int shareStackGet2(shareStack* stack,char* value)
  138. {
  139. if(stack == NULL)
  140. {
  141. return 0;
  142. }
  143. *value = stack->data[stack->top2+1];
  144. return 1;
  145. }
  146. //测试函数
  147. void TestshareStack()
  148. {
  149. char value;
  150. shareStack stack;
  151. shareStackInit(&stack);
  152. char ch1; //接收栈号
  153. char ch2; //接收入栈字符
  154. int m; //接收出栈次数
  155. int i=4;
  156. while(i--)
  157. {
  158. printf("\n************************************\n");
  159. printf("\n(1)请输入字符1,选择栈1入栈:\n(2)请输入字符2,选择栈2入栈:\n");
  160. printf("\n(3)请输入字符3,选择栈1出栈:\n(4)请输入字符4,选择栈2出栈:\n");
  161. printf("\n************************************\n");
  162. ch1=getchar();
  163. getchar(); //避免二次入栈吸收回车符
  164. switch(ch1)
  165. {
  166. case '1':
  167. {
  168. printf("请输入入栈字符,以'#'结束:\n");
  169. while((ch2=getchar())!='#')
  170. {
  171. shareStackPush1(&stack,ch2);
  172. }
  173. shareStackPrint1(&stack,"入栈后共享栈1栈内字符");
  174. break;
  175. }
  176. case '2':
  177. {
  178. printf("请输入入栈字符,以'#'结束:\n");
  179. while((ch2=getchar())!='#')
  180. {
  181. shareStackPush2(&stack,ch2);
  182. }
  183. shareStackPrint2(&stack,"入栈后共享栈2栈内字符");
  184. break;
  185. }
  186. case '3':
  187. {
  188. printf("请输入出栈次数(需小于入栈字符):\n");
  189. scanf("%d",&m);
  190. while(m>0)
  191. {
  192. shareStackPop1(&stack);
  193. m--;
  194. }
  195. shareStackPrint1(&stack,"出栈后共享栈1栈内字符");
  196. break;
  197. }
  198. case '4':
  199. {
  200. printf("请输入出栈次数(需小于入栈字符):\n");
  201. scanf("%d",&m);
  202. while(m>0)
  203. {
  204. shareStackPop2(&stack);
  205. m--;
  206. }
  207. shareStackPrint2(&stack,"出栈后共享栈2栈内字符");
  208. break;
  209. }
  210. }
  211. getchar();//避免显示两次菜单
  212. }
  213. }
  214. int main()
  215. {
  216. TestshareStack();
  217. return 0;
  218. }

数据结构---作业实训-chp3-T3.6-多项式相加(单链表实现)

本关任务: 编写函数,实现多项式链表的建立和遍历。

  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. typedef struct node
  4. {
  5. int xishu;
  6. int zhishu;
  7. struct node* next;
  8. }Node,*List;
  9. void Init(List* L)
  10. {
  11. *L=(Node*)malloc(sizeof(Node));
  12. (*L)->next=NULL;
  13. }
  14. void Add(List L,int xishu,int zhishu)
  15. {
  16. Node* last=L->next;
  17. Node*p=(Node*)malloc(sizeof(Node));
  18. p->xishu=xishu;
  19. p->zhishu=zhishu;
  20. p->next=NULL;
  21. for(last=L;last->next;last=last->next);
  22. last->next=p;
  23. }
  24. void Put(List L)
  25. {
  26. Node* last;
  27. for(last=L->next;last;last=last->next)
  28. {
  29. printf("%d*x^%d",last->xishu,last->zhishu);
  30. if(last->next) printf("+");
  31. }
  32. }
  33. int main()
  34. {
  35. List L;
  36. Init(&L);
  37. int i,n,xishu,zhishu;
  38. printf("请输入P1多项式项数:\n");
  39. scanf("%d",&n);
  40. printf("请依次输入多项式每项的系数和指数:\n");
  41. for(i=0;i<n;i++)
  42. {
  43. scanf("%d %d",&zhishu,&xishu);
  44. Add(L,xishu,zhishu);
  45. }
  46. printf("第一个多项式P1为:\n");
  47. Put(L);
  48. }

本关任务: 编写函数,实现链式链表的建立和存储并进行遍历。

  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. typedef struct node
  4. {
  5. int xishu;
  6. int zhishu;
  7. struct node* next;
  8. }Node, *List;
  9. void Init(List* L)
  10. {
  11. *L=(Node*)malloc(sizeof(Node));
  12. (*L)->next=NULL;
  13. }
  14. void Add(List L, int xishu, int zhishu)
  15. {
  16. Node* last=L->next;
  17. Node* p=(Node*)malloc(sizeof(Node));
  18. p->xishu=xishu;
  19. p->zhishu=zhishu;
  20. p->next=NULL;
  21. for(last=L;last->next;last=last->next);
  22. last->next=p;
  23. }
  24. void Put(List L)
  25. {
  26. Node* last;
  27. for(last=L->next;last;last=last->next)
  28. {
  29. printf("%d*x^%d",last->xishu,last->zhishu);
  30. if(last->next) printf("+");
  31. }
  32. }
  33. void Insert(List L1, List L2)
  34. {
  35. Node* last1,*last2,*temp;
  36. temp=L2;
  37. for(last1=L1;last1->next;last1=last1->next)
  38. {
  39. for(last2=temp;last2->next;last2=last2->next)
  40. {
  41. if(last1->zhishu==last2->zhishu)
  42. {
  43. last1->xishu+=last2->xishu;
  44. temp=last2;
  45. break;
  46. }
  47. }
  48. }
  49. last1->next=temp->next;
  50. }
  51. int main()
  52. {
  53. List L1,L2;
  54. Init(&L1);
  55. Init(&L2);
  56. int i,n,xishu,zhishu;
  57. printf("请输入P1多项式项数:\n");
  58. scanf("%d",&n);
  59. printf("请依次输入多项式每项的系数和指数(按指数升序):\n");
  60. for(i=0;i<n;i++)
  61. {
  62. scanf("%d %d",&xishu,&zhishu);
  63. Add(L1,xishu,zhishu);
  64. }
  65. printf("第一个多项式P1为:\n");
  66. Put(L1);
  67. printf("\n请输入P2多项式项数:\n");
  68. scanf("%d",&n);
  69. printf("请依次输入多项式每项的系数和指数(按指数升序):\n");
  70. for(i=0;i<n;i++)
  71. {
  72. scanf("%d %d",&xishu,&zhishu);
  73. Add(L2,xishu,zhishu);
  74. }
  75. printf("第二个多项式P2为:\n");
  76. Put(L2);
  77. printf("\nP1+P2的和PP多项式为:\n");
  78. Insert(L1,L2);
  79. Put(L1);
  80. }

数据结构---教材例题实训-chp2-最大子序和算法

本关任务: 用分治法求解一个整型数组元素的最大子序和并输出。

  1. #include <stdio.h>
  2. int Max3(int x,int y,int z)
  3. {
  4. //在此编写代码,求解左子序、右子序和中间子序的最大值
  5. /***************************/
  6. return x>y?x>z?x:z:y>z?y:z;
  7. /***************************/
  8. }
  9. static int MaxSubSum( const int A[],int Left,int Right)
  10. {
  11. int MaxLeftSum,MaxRightSum;
  12. int MaxLeftBorderSum,MaxRightBorderSum;
  13. int LeftBorderSum,RightBorderSum;
  14. int Center,i;
  15. if(Left==Right)
  16. if(A[Left]>0)
  17. return A[Left];
  18. else
  19. return 0;
  20. Center =(Left+Right)/2;
  21. //在此输入代码,递归依次求解左、右边区间的数据的最大子序和
  22. /***************************/
  23. MaxLeftSum=MaxSubSum(A,Left,Center);
  24. MaxRightSum=MaxSubSum(A,Center+1,Right);
  25. /***************************/
  26. MaxLeftBorderSum=0;
  27. LeftBorderSum=0;
  28. for(i=Center;i>=Left;i--)//求左边区间的数据的最大子序和
  29. {
  30. LeftBorderSum+=A[i];
  31. if(LeftBorderSum>MaxLeftBorderSum)
  32. MaxLeftBorderSum=LeftBorderSum;
  33. }
  34. MaxRightBorderSum=0;
  35. RightBorderSum=0;
  36. for(i=Center+1;i<=Right;i++)
  37. {
  38. //在此编写代码,求右边区间的数据的最大子序和
  39. /***************************/
  40. RightBorderSum+=A[i];
  41. if(RightBorderSum>MaxRightBorderSum) MaxRightBorderSum=RightBorderSum;
  42. /***************************/
  43. }
  44. return Max3(MaxLeftSum,
  45. MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);
  46. }
  47. int MaxSubsequenceSum(const int A[],int N)
  48. {
  49. return MaxSubSum(A,0,N-1);
  50. }
  51. void main()
  52. {
  53. int MaxSum,i=0,N;
  54. int a[100];
  55. scanf("%d",&a[i]);
  56. while(a[i]!=0) //以0结束输入
  57. {
  58. i++;
  59. scanf("%d",&a[i]);
  60. }
  61. N= i;
  62. printf("a数组有N=%d个元素,依次为:",N);
  63. for(i=0;i<N;i++)
  64. printf(" %d,",a[i]);
  65. printf("\n则a数组最大子序和为:");
  66. MaxSum=MaxSubsequenceSum( a,N);
  67. printf(" MaxSum=%d\n",MaxSum);
  68. }

最大子序和求解之方法4---方法2之改进---线性级(联机算法)

  1. #include <stdio.h>
  2. int MaxSubsequenceSum(const int A[],int N)
  3. {
  4. int ThisSum,MaxSum,i;//当前和,最大和,循环变量
  5. ThisSum=MaxSum=0;
  6. //在此输入教材29页联机算法代码,求最大子序和,线性级经典算法,并好好理解
  7. /***************************/
  8. for(i=0;i<N;i++)
  9. {
  10. ThisSum+=A[i];
  11. if(ThisSum>MaxSum) MaxSum=ThisSum;
  12. else if(ThisSum<0)ThisSum=0;
  13. }
  14. /***************************/
  15. return MaxSum;
  16. }
  17. void main()
  18. {
  19. int MaxSum,i=0,N;
  20. int a[100];
  21. scanf("%d",&a[i]);
  22. while(a[i]!=0) //以0结束输入
  23. {
  24. i++;
  25. scanf("%d",&a[i]);
  26. }
  27. N= i;
  28. printf("a数组有N=%d个元素,依次为:",N);
  29. for(i=0;i<N;i++)
  30. printf(" %d,",a[i]);
  31. printf("\n则a数组最大子序和为:");
  32. MaxSum=MaxSubsequenceSum( a,N);
  33. printf(" MaxSum=%d\n",MaxSum);
  34. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/549114
推荐阅读
相关标签
  

闽ICP备14008679号