当前位置:三九宝宝网 → 宝宝教育 → 教学论文 → 正文

用C语言实现链表的算法

更新:12-24 整理:39baobao.com
字体:

[计算机二级C语言考试]计算机二级C语言相对于 VB,VFP难一点,但含金量高点 个人觉得c语言还是不难通过的首先介绍一下考试结构:笔试100分,上机100分笔试有选择,填空2种题型(前10个选择题为计算机公共基...+阅读

这个是我们数据结构上机实验的链表问题, #include #include #define LEN sizeof(LinkNode) typedef int Datatype; typedef int Status; typedef struct LinkNode{ Datatype data; struct LinkNode *next; } LinkNode,*LinkList; typedef struct OrderedList { LinkNode *head,*tail; int Listsize; } OrderedList;//有序循环链表的头节点head,尾接接节点 tail及长度Listsize Status InitList(OrderedList *List)//生成循环链表头节点 { List->tail=List->head=(LinkList)malloc(LEN); if(List->head==NULL) return 0; else { List->head->next=List->tail; List->tail->next=List->head; List->Listsize=0; return 1; } } void OrderedInsert(OrderedList *List,Datatype data)//每调用一次有序插入data形成有序的(从小到大)的链表 { LinkNode *p ,*q; if(List->head==List->tail->next) { p=(LinkNode*)malloc(LEN); p->data = data; List->head->next=p; p->next=List->tail; List->Listsize++; } else { p=List->head->next; q = List->head; while(p->datatail) { q = p; p=p->next; } if(p->data==data) {printf("YOu have input the same datas %d\n\t YOu should input another data \n",data); scanf("%d",&data); OrderedInsert(List,data); } else { p=(LinkNode*)malloc(LEN); p->data = data; p->next = q->next; q->next = p; List->Listsize++; } } } void Creatset(OrderedList *List)//多次调用OrderedInsert()生成有序链表即集合List { Datatype data; int setsize , i=0; printf("Please input the setsize you want to creat:\n"); scanf("%d",&setsize); InitList(List); if(setsize==0) printf("You needen't input any data\n"); else if(setsize==1) printf("Please input a single data\n"); else printf("Please input %d different datas;\n",setsize); while(iList->Listsize)//当循环次数i小于setsize或者集合内实际元素数List.Listsize小于setsize时一直循环下去 { scanf("%d",&data); OrderedInsert(List,data); i++; } } void Append(OrderedList *List,Datatype data)//在循环链表的最后面追加 一个data { LinkNode *p; p=(LinkNode*)malloc(LEN); p->data=data; List->tail=List->tail->next=p; List->tail->next=List->head; List->Listsize+=1; } void MergeList(OrderedList La,OrderedList Lb,OrderedList *Lc)//有序循环链表ListLa,ListLb求并集生成ListLc { LinkList Pa,Pb; Pa=La.head->next;Pb=Lb.head->next; while(Pa!=La.tail&&Pb!=Lb.tail) { if(Pa->data<=Pb->data) { Append(Lc,Pa->data); Pa=Pa->next; } else { Append(Lc,Pb->data);Pb=Pb->next; } } while(Pa!=La.tail) { Append( Lc,Pa->data);Pa=Pa->next;} while(Pb!=Lb.tail) { Append(Lc,Pb->data);Pb=Pb->next;} } void Print(OrderedList List) { LinkNode *p; p=List.head->next; if(p->next==List.head) printf("No Elem\n"); while(p!=List.head) { printf("%5d",p->data);p=p->next; } printf("\n"); } void main() { OrderedList ListLa,ListLb,ListLc; Creatset(&ListLa); Creatset(&ListLb); InitList(&ListLc); MergeList(ListLa,ListLb,&ListLc); printf("The orgnial list ListLa,ListLb:\n"); Print(ListLa); Print(ListLb); printf("The Merge list ListLc;\n"); Print(ListLc); }

本文地址:https://www.39baobao.com/show/29_14141.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

计算机2级C语言考试在初学C语言时,可能会遇到有些问题理解不透,或者表达方式与以往数学学习中不同(如运算符等),这就要求不气馁,不明白的地方多问多想,鼓足勇气进行学习,待学完后面的章节知识,前面的问...

计算机C语言二级等级考试我二级三级都考过! 先回答你第一个问题,基础理论部分内容是很多,但是考试的题型和知识点都很固定,只要你把历年的考试题目的基础部分都做一遍,公共基础部分对百分之80应该可以了,...

全国计算机等级考试二级C语言(1)计算机二级一般没有在县级报名的,应该是在市级所以你的情况应该是去重庆报名并且考试 (2)考试大纲近几年都不会有什么大的变动,所以不用担心,即使有变动也是换汤不换药,现在...

c语言程序设计int k=4,t=100,n; inta[11]={0,1,2,3,4,5,6,7,8,9,} for(n=0,n<=9;n++)printf(%5d",a[n]); printf("\n"); for(n=10;n>=k+1;n-)a[n]=a[n-1]; a[k]=t for(n=0;n<=10;n++)printf(...

关于C语言程序设计#include "stdafx.h" #include#include#include#includeclass DoubleNode//栈节点数据结构 { friend class DoubleStack; private: double value; DoubleNode *next; DoubleNo...

c语言程序编写插入C输出那个❤太麻烦了,输出*吧 #include int main(){ char p[5000]; int i; scanf("%s",p); for(i=0;p[i+1]!=0;i++){ if(p[i]printf("*%c%c",p[i],p[i+1]); i++;} else printf("*%...

C语言程序设计输入4个整数要求按由小到大的顺序输出怎么做啊//本实例采用冒泡排序法对整数型数组元素进行排序。 //冒泡排序法的基本思想:(以升序为例)含有n个元素的数组原则上要进行n-1次排序。对于每一躺的排序,从第一个数开始,依次比较...

C语言程序设计实现输入20个整数数据按从大到小排序计算出#include<stdio.h> int main() { int a[20],i,j,s; double sum=0,average; for(i=0;i<20;i++){ scanf("%d",&a[i]); sum+=a[i]; } average=sum/20; for(i=1;i<20;i++) for(j=0...

C语言程序设计输入4个数字要求由小到大顺序输出#include<stdio.h> void main() { int a[4],i,j,k; printf("please input four numbers:\n"); for(i=0;i<4;i++) scanf("%d",&a[i]); printf("before Sorting the number is:\n");...