赞
踩
以下程序仅供参考学习,请勿抄袭!
- void Swap(List head)
- {
- // 请在此添加代码,完成对已建立的单链表的逆置
- /********** Begin *********/
- position last;
- position new_l;
- int isprime=1;
- new_l=(position)malloc(sizeof(a));
- for(last=head->next;last;last=last->next)
- {
- position s;
- s=(position)malloc(sizeof(a));
- s->data=last->data;
- if(isprime)
- {
- s->next=NULL;
- isprime=0;
- }
- else
- {
- s->next=new_l->next;
- }
- new_l->next=s;
- }
- head->next=new_l->next;
- /********** End **********/
- }

- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node *ptrtonode; //为struct Node取别名为ptrtonode,可以直接用类型ptrtonode来定义指针变量
- typedef ptrtonode List; //为ptrtonode取别名为List,可以直接用类型List类型来定义指针变量
- typedef ptrtonode position;
-
- struct Node //定义单播结点,结点为结构体,包含字符成员data和指针变量成员next
- {
- char data; //结点的数据域,用来存储一个字符
- position next; //结点的指针域,用来指向下一个结点
- }a; //定义结构体变量a
-
-
- /*
- 尾插法创建带头结点的单链表,单链表头指针为L指针
- */
-
- position creat()
- {
- position L; //定义结构体指针变量L,用来指向单链表头部
- position p1,p2; //定义结构体指针变量p1,p2,作为临时指针指向结点
- char data;
- L=(position)malloc(sizeof(a)); //申请一个结点大小的空间,用指针L指向这个空间首地址
- p2=L; //p2指针也指向L指的结点
- while((data=getchar())!='#') //从键盘获取字符存入变量data中,如果从键盘获取的字符不是'\#'则循环
- {
- p1=(position)malloc(sizeof(a)); //
- p1->data=data; //
- p2->next=p1; //
- p2=p1; //
- }
-
- p2->next=NULL; //p2指向的结点的指针域置空,表示是最后一个结点
- return L; //
- }
-
- /*
- 遍历单链表
- */
-
- void print(List head) //传入单链表头指针head,完成对单链表的遍历
- {
- position p;
- p=head->next; //临时指针变量p指向单链表的第一个结点
- while(p!=NULL)
- {
- printf("%c",p->data); //获取单链表中p指针指向的结点的数据域的字符并输出
- p=p->next; //p指针指向下一个结点
- }
- printf("\n");
- }
-
- /*
- 把单链表中n和n+1位置上的结点进行交换;
- n范围从1到单链表长度-1
- */
- void SwapSinglist(List head,int n)
- {
- // 请在此添加代码,实现单链表中n和n+1位置元素的交换(通过交换指针实现)
- /********** Begin *********/
- position last;
- position p;
- int i=0;
- char temp;
- for(last=head;last&&i<n;last=last->next,i++);
- p=last->next;
- temp=last->data;
- last->data=p->data;
- p->data=temp;
- /********** End **********/
- }
-
-
- void main()
- {
-
- position L=NULL;
- printf("请输入链表节点,以“#”结束:\n");
- L=creat(); //调用creat函数,完成单链表的创建
- printf("初始链表为:\n");
- print(L); //调用print函数,完成单链表的遍历
-
- int n;
- printf("请输入要交换的n位置(n<单链表长度),如1,2,3......\n");
- scanf("%d",&n);
- SwapSinglist(L,n);
- printf("%d和%d位置交换后链表为:\n",n,n+1);
- print(L);
- }

- #include "queue.h"///data/workspace/myshixun/chp3/fatal.h
- #include "fatal.h"
- #include <stdlib.h>
-
- #define MinQueueSize ( 5 )
-
- struct QueueRecord
- {
- int Capacity; //队列容量
- int Front; //队首,进行出队操作
- int Rear; //队尾,进行入队操作
- int Size; //队列实际大小
- ElementType *Array; //队列数组指针
- };
-
- /* START: fig3_58.txt */
- int
- IsEmpty( Queue Q )
- {
- // 请在此添加代码,队列空,返回1;否则返回0
- /********** Begin *********/
- return Q->Size==0?1:0;
- /********** End **********/
- }
- /* END */
-
- int
- IsFull( Queue Q )
- {
- // 请在此添加代码,队列满,返回1;否则返回0
- /********** Begin *********/
- return Q->Size==Q->Capacity?1:0;
- /********** End **********/
- }
- /*
- 队列的创建
- */
- Queue
- CreateQueue( int MaxElements )
- {
- Queue Q;
-
- /* 1*/ if( MaxElements < MinQueueSize )
- /* 2*/ Error( "Queue size is too small" );
-
- /* 3*/ Q = malloc( sizeof( struct QueueRecord ) );
- /* 4*/ if( Q == NULL )
- /* 5*/ FatalError( "Out of space!!!" );
-
- /* 6*/ Q->Array = malloc( sizeof( ElementType ) * MaxElements );
- /* 7*/ if( Q->Array == NULL )
- /* 8*/ FatalError( "Out of space!!!" );
- /* 9*/ Q->Capacity = MaxElements;
- /*10*/ MakeEmpty( Q );
-
- /*11*/ return Q;
- }
-
- /*
- START: fig3_59.txt
- 队列创建后各变量置初态
- */
- void
- MakeEmpty( Queue Q )
- {
- // 请在此添加代码,初始化队列的Size,Front,Rear值
- /********** Begin *********/
- Q->Size=0;
- Q->Front=Q->Rear=0;
- /********** End **********/
- }
- /* END */
- /*
- 队列的销毁
- */
- void
- DisposeQueue( Queue Q )
- {
- if( Q != NULL )
- {
- free( Q->Array );
- free( Q );
- }
- }
-
- /*
- START: fig3_60.txt
- 队列Q的Value值加1
- */
- static int
- Succ( int Value, Queue Q )
- {
- if( ++Value == Q->Capacity )//指针value加1后返回,如果加1后等于容量了返回0
- Value = 0;
- return Value;
- }
- /*
- 元素X入Q队
- */
- void
- Enqueue( ElementType X, Queue Q )
- {
- if( IsFull( Q ) )
- Error( "Full queue" );
- else
- {
- // 请在此添加代码,完成入队操作
- /********** Begin *********/
- Q->Array[Q->Rear]=X;
- Q->Rear=(Q->Rear+1)%Q->Capacity;
- Q->Size++;
- /********** End **********/
- }
- }
- /* END */
-
- /*
- 取队首元素
- */
-
- ElementType
- Front( Queue Q )
- {
- if( !IsEmpty( Q ) )
- return Q->Array[ Q->Front ];//返回队头元素值
- Error( "Empty queue" );
- return 0; /* Return value used to avoid warning */
- }
- /*
- 出队,不保存出队元素
- */
- void
- Dequeue( Queue Q )
- {
- if( IsEmpty( Q ) )
- Error( "Empty queue" );
- else
- {
- // 请在此添加代码,完成出队操作
- /********** Begin *********/
- Q->Front=(Q->Front+1)%Q->Capacity;
- Q->Size--;
- /********** End **********/
- }
- }
- /*
- 队首的元素出队后保存在变量X中可返回
- */
- ElementType
- FrontAndDequeue( Queue Q )
- {
- ElementType X = 0;
-
- if( IsEmpty( Q ) )
- Error( "Empty queue" );
- else
- {
- Q->Size--;
- X = Q->Array[ Q->Front ];//队头元素放入X后被返回
- Q->Front = Succ( Q->Front, Q );
- }
- return X;
- }
- /*
- 测试主函数
- */
- main( )
- {
- Queue Q;
- int i;
-
- Q = CreateQueue( 12 ); //创建队列
-
- printf("第一次入队(0~9):\n");
- for( i = 0; i < 10; i++ ) //0到9入列
- // 请在此添加代码,调用函数完成入队操作
- /********** Begin *********/
- Enqueue(i,Q);
-
- /********** End **********/
- printf("出队,即入队元素有:\n");
- while( !IsEmpty( Q ) ) //遍历出队元素
- {
- printf( "%d ", Front( Q ) );
- Dequeue( Q );
- }
- printf( "\n");
-
- printf("第二次入队(0,3,6,9):\n");
- for( i = 0; i < 10; i+=3 ) //0到9中3的倍数入列
- Enqueue( i, Q );
- printf( "\n");
- printf("出队,即入队元素有:\n");
- while( !IsEmpty( Q ) ) //创建队列
- {
- printf( "%d ", Front( Q ) );
- // 请在此添加代码,调用函数完成出队操作
- /********** Begin *********/
- Dequeue(Q);
-
- /********** End **********/
- }
- printf( "\n");
- DisposeQueue( Q ); //销毁队列
- return 0;
- }

- #include <stdio.h>
- #include <stdlib.h>
-
- //定义节点
- typedef struct node
- {
- char data;
- struct node * next;
- }node;
-
- //定义队列(保存队首和队尾指针)
- typedef struct queue_link
- {
- // 请在此添加代码,定义队列首尾指针
- /********** Begin *********/
- node* front,*rear;
-
- /********** End **********/
- }LinkQueue;
-
- //初始化队列
- LinkQueue * InitQueue()
- {
- LinkQueue * q = (LinkQueue*)malloc(sizeof(LinkQueue));
- // 请在此添加代码,完成队列首尾指针赋值,级队列初始化
- /********** Begin *********/
- q->front=q->rear=NULL;
-
- /********** End **********/
- return q;
- }
-
- //判断队列是否为空
- int EmptyQueue(LinkQueue * q)
- {
- return q->front == NULL;
- }
-
- //入队
- void InsertQueue(LinkQueue *q, char data)
- {
- node * n = (node *)malloc(sizeof(node));
- n->data = data;
- n->next = NULL; //尾插法,插入元素指向空
- if(q->rear == NULL) //只有空头结点,即队中无元素
- {
- q->front = n;
- q->rear = n;
- }
- else{
- // 请在此添加代码,完成把新结点插入到尾部
- /********** Begin *********/
- q->rear->next=n;
- q->rear=n;
-
- /********** End **********/
- }
-
- }
-
- //出队(删除队首元素)
- void DeleteQueue(LinkQueue *q)
- {
- node * n = q->front;
-
- if(q->front == q->rear)//是否只有一个元素
- {
- q->front = NULL;
- q->rear = NULL;
- }
- else{
- // 请在此添加代码,完成出队操作
- /********** Begin *********/
- q->front=n->next;
-
- /********** End **********/
- }
- free(n);
- }
-
- //打印队列
- void Display(LinkQueue * q)
- {
- node * n = (node *)malloc(sizeof(node));
- n = q->front;
- while(n != NULL)
- {
- // 请在此添加代码,完成打印队列元素操作
- /********** Begin *********/
- printf("%c ",n->data);
- n=n->next;
-
- /********** End **********/
- }
- printf("\n");
- }
- int main()
- {
- int i=5,j=5;
- LinkQueue * q;
- q = InitQueue();
- printf("开始入队:\n");
- while(i--)
- {
- printf("元素%c入队,队列为:",'A'+i);
- // 请在此添加代码,调用入队函数完成入队并遍历入队后队列元素
- /********** Begin *********/
- InsertQueue(q,'A'+i);
- Display(q);
- /********** End **********/
- }
- printf("开始出队:\n");
- while(j--)
- {
- printf("第%d个元素出队,队列为:",5-j);
- // 请在此添加代码,调用出队函数完成出队并遍历剩下的队列元素
- /********** Begin *********/
- DeleteQueue(q);
- Display(q);
-
- /********** End **********/
- }
- printf("\n");
- return 0;
- }

- //T3.21:一个数组实现两个共享栈
- //版权声明:本文为CSDN博主「雪碧柠七」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
- //原文链接:https://blog.csdn.net/ihaha233/article/details/80520823
- //rfh改编
-
- #include<stdio.h>
-
- #define Max_size 1000
-
- typedef char DataType;
- typedef struct shareStack
- {
- DataType data[Max_size];
- int top1;
- int top2;
- }shareStack;
-
- //初始化
- void shareStackInit(shareStack* stack)
- {
- if(stack == NULL)
- {
- //非法输入
- return;
- }
- stack->top1 = 0;
- stack->top2 = Max_size;
- return;
- }
-
- //打印栈1
- void shareStackPrint1(shareStack* stack,const char* msg)
- {
- printf("[%s]\n",msg);
- if(stack == NULL)
- {
- //非法输入
- return;
- }
- int i = 0;
- for(i = 0; i< stack->top1;i++)
- {
- printf("%c ",stack->data[i]);
- }
- printf("\n");
- }
-
- //打印栈2
- void shareStackPrint2(shareStack* stack,const char* msg)
- {
- printf("[%s]\n",msg);
- if(stack == NULL)
- {
- //非法输入
- return;
- }
- int i = Max_size;
- for(;i > stack->top2;i--)
- {
- // 请在此添加代码,输出栈2栈顶的内容
- /********** Begin *********/
- printf("%c ",stack->data[i]);
-
- /********** End **********/
- }
- printf("\n");
- }
-
- //栈1插入元素
- void shareStackPush1(shareStack* stack,DataType value)
- {
- if(stack == NULL)
- {
- //非法输入
- return;
- }
- if(stack->top1 == stack->top2)
- {
- //达到共享栈上限
- return;
- }
- // 请在此添加代码,完成栈1的入栈操作
- /********** Begin *********/
- stack->data[stack->top1++]=value;
-
- /********** End **********/
- return;
- }
-
-
-
- //栈2插入元素
- void shareStackPush2(shareStack* stack,DataType value)
- {
- if(stack == NULL)
- {
- return;
- }
- if(stack->top1 == stack->top2)
- {
- //达到共享栈上限
- return;
- }
- // 请在此添加代码,完成栈2的入栈操作
- /********** Begin *********/
- stack->data[stack->top2--]=value;
-
- /********** End **********/
- return;
- }
-
- //栈1出栈
- void shareStackPop1(shareStack* stack)
- {
- if(stack == NULL)
- {
- printf("空栈不能进行出栈\n");//非法输入
- return;
- }
- // 请在此添加代码,完成栈1的出栈操作
- /********** Begin *********/
- stack->top1--;
-
- /********** End **********/
- return;
- }
-
- //栈2出栈
- void shareStackPop2(shareStack* stack)
- {
- if(stack == NULL)
- {
- printf("空栈不能进行出栈\n");//非法输入
- return;
- }
- // 请在此添加代码,完成栈2的出栈操作
- /********** Begin *********/
- stack->top2++;
-
- /********** End **********/
- return;
- }
-
- //栈1取栈顶元素
- int shareStackGet1(shareStack* stack,char* value)
- {
- if(stack == NULL)
- {
- return 0;
- }
- *value = stack->data[stack->top1-1];
- return 1;
- }
-
-
- //栈2取栈顶元素
- int shareStackGet2(shareStack* stack,char* value)
- {
- if(stack == NULL)
- {
- return 0;
- }
- *value = stack->data[stack->top2+1];
- return 1;
- }
-
- //测试函数
- void TestshareStack()
- {
- char value;
- shareStack stack;
- shareStackInit(&stack);
- char ch1; //接收栈号
- char ch2; //接收入栈字符
- int m; //接收出栈次数
- int i=4;
- while(i--)
- {
- printf("\n************************************\n");
- printf("\n(1)请输入字符1,选择栈1入栈:\n(2)请输入字符2,选择栈2入栈:\n");
- printf("\n(3)请输入字符3,选择栈1出栈:\n(4)请输入字符4,选择栈2出栈:\n");
- printf("\n************************************\n");
- ch1=getchar();
- getchar(); //避免二次入栈吸收回车符
-
-
- switch(ch1)
- {
-
- case '1':
- {
- printf("请输入入栈字符,以'#'结束:\n");
- while((ch2=getchar())!='#')
- {
- shareStackPush1(&stack,ch2);
- }
- shareStackPrint1(&stack,"入栈后共享栈1栈内字符");
-
- break;
- }
- case '2':
- {
- printf("请输入入栈字符,以'#'结束:\n");
- while((ch2=getchar())!='#')
- {
- shareStackPush2(&stack,ch2);
- }
- shareStackPrint2(&stack,"入栈后共享栈2栈内字符");
- break;
- }
-
- case '3':
- {
- printf("请输入出栈次数(需小于入栈字符):\n");
- scanf("%d",&m);
- while(m>0)
- {
- shareStackPop1(&stack);
- m--;
- }
- shareStackPrint1(&stack,"出栈后共享栈1栈内字符");
- break;
-
- }
- case '4':
- {
- printf("请输入出栈次数(需小于入栈字符):\n");
- scanf("%d",&m);
- while(m>0)
- {
- shareStackPop2(&stack);
- m--;
- }
- shareStackPrint2(&stack,"出栈后共享栈2栈内字符");
- break;
-
- }
-
- }
- getchar();//避免显示两次菜单
- }
- }
-
-
- int main()
- {
- TestshareStack();
- return 0;
- }

- #include"stdio.h"
- #include"stdlib.h"
-
- typedef struct node
- {
- int xishu;
- int zhishu;
- struct node* next;
- }Node,*List;
- void Init(List* L)
- {
- *L=(Node*)malloc(sizeof(Node));
- (*L)->next=NULL;
- }
- void Add(List L,int xishu,int zhishu)
- {
- Node* last=L->next;
- Node*p=(Node*)malloc(sizeof(Node));
- p->xishu=xishu;
- p->zhishu=zhishu;
- p->next=NULL;
- for(last=L;last->next;last=last->next);
- last->next=p;
- }
- void Put(List L)
- {
- Node* last;
- for(last=L->next;last;last=last->next)
- {
- printf("%d*x^%d",last->xishu,last->zhishu);
- if(last->next) printf("+");
- }
- }
- int main()
- {
- List L;
- Init(&L);
- int i,n,xishu,zhishu;
- printf("请输入P1多项式项数:\n");
- scanf("%d",&n);
- printf("请依次输入多项式每项的系数和指数:\n");
- for(i=0;i<n;i++)
- {
- scanf("%d %d",&zhishu,&xishu);
- Add(L,xishu,zhishu);
- }
- printf("第一个多项式P1为:\n");
- Put(L);
- }

- #include"stdio.h"
- #include"stdlib.h"
-
- typedef struct node
- {
- int xishu;
- int zhishu;
- struct node* next;
- }Node, *List;
- void Init(List* L)
- {
- *L=(Node*)malloc(sizeof(Node));
- (*L)->next=NULL;
- }
- void Add(List L, int xishu, int zhishu)
- {
- Node* last=L->next;
- Node* p=(Node*)malloc(sizeof(Node));
- p->xishu=xishu;
- p->zhishu=zhishu;
- p->next=NULL;
- for(last=L;last->next;last=last->next);
- last->next=p;
- }
- void Put(List L)
- {
- Node* last;
- for(last=L->next;last;last=last->next)
- {
- printf("%d*x^%d",last->xishu,last->zhishu);
- if(last->next) printf("+");
- }
- }
- void Insert(List L1, List L2)
- {
- Node* last1,*last2,*temp;
- temp=L2;
- for(last1=L1;last1->next;last1=last1->next)
- {
- for(last2=temp;last2->next;last2=last2->next)
- {
- if(last1->zhishu==last2->zhishu)
- {
- last1->xishu+=last2->xishu;
- temp=last2;
- break;
- }
- }
- }
- last1->next=temp->next;
- }
- int main()
- {
- List L1,L2;
- Init(&L1);
- Init(&L2);
- int i,n,xishu,zhishu;
- printf("请输入P1多项式项数:\n");
- scanf("%d",&n);
- printf("请依次输入多项式每项的系数和指数(按指数升序):\n");
- for(i=0;i<n;i++)
- {
- scanf("%d %d",&xishu,&zhishu);
- Add(L1,xishu,zhishu);
- }
- printf("第一个多项式P1为:\n");
- Put(L1);
- printf("\n请输入P2多项式项数:\n");
- scanf("%d",&n);
- printf("请依次输入多项式每项的系数和指数(按指数升序):\n");
- for(i=0;i<n;i++)
- {
- scanf("%d %d",&xishu,&zhishu);
- Add(L2,xishu,zhishu);
- }
- printf("第二个多项式P2为:\n");
- Put(L2);
- printf("\nP1+P2的和PP多项式为:\n");
- Insert(L1,L2);
- Put(L1);
- }

- #include <stdio.h>
- int Max3(int x,int y,int z)
- {
- //在此编写代码,求解左子序、右子序和中间子序的最大值
- /***************************/
- return x>y?x>z?x:z:y>z?y:z;
-
-
-
- /***************************/
- }
- static int MaxSubSum( const int A[],int Left,int Right)
- {
- int MaxLeftSum,MaxRightSum;
- int MaxLeftBorderSum,MaxRightBorderSum;
- int LeftBorderSum,RightBorderSum;
- int Center,i;
-
- if(Left==Right)
- if(A[Left]>0)
- return A[Left];
- else
- return 0;
-
- Center =(Left+Right)/2;
- //在此输入代码,递归依次求解左、右边区间的数据的最大子序和
- /***************************/
- MaxLeftSum=MaxSubSum(A,Left,Center);
- MaxRightSum=MaxSubSum(A,Center+1,Right);
-
-
- /***************************/
- MaxLeftBorderSum=0;
- LeftBorderSum=0;
-
- for(i=Center;i>=Left;i--)//求左边区间的数据的最大子序和
- {
- LeftBorderSum+=A[i];
- if(LeftBorderSum>MaxLeftBorderSum)
- MaxLeftBorderSum=LeftBorderSum;
- }
-
- MaxRightBorderSum=0;
- RightBorderSum=0;
-
- for(i=Center+1;i<=Right;i++)
- {
- //在此编写代码,求右边区间的数据的最大子序和
- /***************************/
- RightBorderSum+=A[i];
- if(RightBorderSum>MaxRightBorderSum) MaxRightBorderSum=RightBorderSum;
-
-
-
- /***************************/
- }
-
- return Max3(MaxLeftSum,
- MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);
- }
-
- int MaxSubsequenceSum(const int A[],int N)
- {
- return MaxSubSum(A,0,N-1);
- }
-
- void main()
- {
- int MaxSum,i=0,N;
- int a[100];
- scanf("%d",&a[i]);
- while(a[i]!=0) //以0结束输入
- {
- i++;
- scanf("%d",&a[i]);
- }
- N= i;
- printf("a数组有N=%d个元素,依次为:",N);
- for(i=0;i<N;i++)
- printf(" %d,",a[i]);
- printf("\n则a数组最大子序和为:");
- MaxSum=MaxSubsequenceSum( a,N);
- printf(" MaxSum=%d\n",MaxSum);
-
- }

- #include <stdio.h>
-
- int MaxSubsequenceSum(const int A[],int N)
- {
- int ThisSum,MaxSum,i;//当前和,最大和,循环变量
-
- ThisSum=MaxSum=0;
- //在此输入教材29页联机算法代码,求最大子序和,线性级经典算法,并好好理解
- /***************************/
- for(i=0;i<N;i++)
- {
- ThisSum+=A[i];
- if(ThisSum>MaxSum) MaxSum=ThisSum;
- else if(ThisSum<0)ThisSum=0;
- }
-
-
-
- /***************************/
- return MaxSum;
- }
-
-
- void main()
- {
- int MaxSum,i=0,N;
- int a[100];
- scanf("%d",&a[i]);
- while(a[i]!=0) //以0结束输入
- {
- i++;
- scanf("%d",&a[i]);
- }
- N= i;
- printf("a数组有N=%d个元素,依次为:",N);
- for(i=0;i<N;i++)
- printf(" %d,",a[i]);
- printf("\n则a数组最大子序和为:");
- MaxSum=MaxSubsequenceSum( a,N);
- printf(" MaxSum=%d\n",MaxSum);
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。