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

麻烦帮解决一下可以上机实现的C语言程序要求用链表实现归并排

更新:03-17 整理:39baobao.com
字体:

[用C语言实现链表的算法]这个是我们数据结构上机实验的链表问题, #include<stdio.h> #include<malloc.h> #define LEN sizeof(LinkNode) typedef int Datatype; typedef int Status; typedef struct...+阅读

我之前写过一个,不过是C++的……

对C不太熟,怕改错,就把这个给你凑合一下吧……

跟数组的归并排序很像,你应该能看懂

#include

struct Node

{

int data;

Node *lnext,*rnext;

Node()

{

lnext=NULL;

rnext=NULL;

}

Node(int key)

{

lnext=NULL;

rnext=NULL;

data=key;

}

};

Node *Merge(Node *L,Node *R)

{

Node *p,*q,*ans;

ans=NULL;

while (L!=NULL || R!=NULL)

{

if (R==NULL || (L!=NULL & R!=NULL & L->datadata))

{

p=L;

L=L->lnext;

}

else

{

p=R;

R=R->lnext;

}

if (ans==NULL) ans=p;

else

{

p->rnext=q;

q->lnext=p;

}

q=p;

}

p=q=NULL;

return ans;

}

Node *Merge_Sort(Node *head,int size)

{

if (size==1) return head;

else

{

int m,i;

Node *p;

m=size/2;

p=head;

for (i=0;ilnext;

p->rnext->lnext=NULL;

p->rnext=NULL;

head=Merge_Sort(head,m);

p=Merge_Sort(p,size-m);

return Merge(head,p);

}

}

Node *head;

int n;

int main()

{

int data;

Node *p;

for (head=NULL,n=0;cin>>data & data!=0;n++)

{

Node *temp;

temp=new Node(data);

if (head==NULL) head=temp;

else

{

temp->rnext=p;

p->lnext=temp;

}

p=temp;

temp=NULL;

}

head=Merge_Sort(head,n);

for (p=head;p!=NULL;p=p->lnext) coutdatareturn 0;

}

C语言单向链表排序如何实现

struct student* printf_sort(struct student *head)

{

struct student *p1,*p2,*ptemp,*pfinished=NULL;

for(p1=head;p1->next!=pfinished;)//对链表进行从大到小排序(这里用冒泡法)

//p1使之总是指向头结点,pfinished使之总是指向已排序好的最前面的结点

//ptemp作为中介,保存p2的上一个结点

{

for(p2=p1;p2->next!=pfinished;)

{

if(p2->numnext->num)//p2的值小于p2->next的值,交换 {

if(p2==p1)//头结点要交换

{

p1=p2->next;

p2->next=p1->next;

p1->next=p2;

ptemp=p1;

}

else

{

ptemp->next=p2->next;

ptemp=p2->next;

p2->next=ptemp->next;

ptemp->next=p2;

}

}

else//不需要交换,则p2、ptemp前进1位

{

ptemp=p2;

p2=p2->next;

}

}

pfinished=p2;

}

}

利用链表实现二路归并排序算法。求完整的C程序急用今晚

这个链表类里包括增、删、查、改,一般来说应该够用了吧,希望对你有帮助。 把这里面的函数名改一改,再调用内部函数创建新的函数实现归并、拆分应该不难。 templateclass List { private: struct Node { T data; Node* next; Node(const T& t=T()):data(t) {next=NULL;} }; Node* head; Node* getPointer(int pos) { Node* p=head; for(int i=0;inext; return p; } public: List():head(NULL){}; void clear() { while(head!=NULL) { Node* p = head->next; delete head; head = p; } } ~List() { clear(); } void insert_front(const T& t) { Node* p = new Node(t); p->next = head; head = p; } void travel() { Node* p = head; while(p!=NULL) { coutnext != NULL) { p = p->next; } return p->data; } bool empty() { return head==NULL; } int find(const T& t) { int pos = 0; Node* p = head; while(p!=NULL) { if(p->data == t) return pos; pos++; p = p->next; } return -1; } bool updata(const T& o, const T& n) { int pos = find(o); if(pos == -1) return false; Node* p = getPointer(pos); p->data = n; return true; } bool erase(const T& t) { int pos = find(t); if(pos == -1) return false; if(pos == 0) { Node* p = head->next; delete head; head = p; } else { Node* pre = getPointer(pos-1); Node* cur = pre->next; pre->next = cur->next; delete cur; } return true; } };

求一个单链表归并排序算法 C语言的源代码急需!

//MergeSort.cpp#include#include#define MAXSIZE 20#define LENGTH 7 typedef int RedType; typedef struct //SqList structure { RedType r[MAXSIZE+1]; //Records Type int length; }SqList; typedef SqList RcdType; void Merge(RcdType SR,RcdType &TR,int i,int m,int n) //Merge() function { int j,k; for(j=m+1,k=i;i

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

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

以下为关联文档:

用C语言实现数据结构中常用算法如对链表的操作查找排序等#include <iostream.h> class ram { public: char wenzi[200]; ram *p; }; ram wo,*ai=&wo; int num=0;//我申请了几次内存了 void xie(void);//输入数据,然后分配内存为下次做...

求c语言实现图形界面c c c语言都可以那得学习windows编程了。。。用VC6.0新建一个wn32程序,而不DOS程序,输入如下代码即可:#include#include#include#includeint g_nYPos = 200; //文字的Y坐标 long WINAPI WndProc...

c语言里用递归实现链表反向打印打开NewContactList工程文件, 相关的修改的程序文件如下(未修改的没有post 上去)。 ContactList.h文件如下: /* *ContactList.h * * created on Jul 6, 2014 * Author: *** * *...

用汇编语言程序实现一下C效果mov ax, m mov dx, n cmp ax, dx je label1 jl label2 sub ax, dx jmp label1 label2: sub dx, ax label2: label1: 有点小错误,修改了 mov ax, m mov dx, n cmp ax, dx je l...

你好请问一下C语言怎么实现保存功能#include <stdio.h> 基本的文件操作 打开文件 FILE * fdopen(int fildes, const char *mode); 读文件 size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream)...

C语言编写的建立单向int链表连续输入10个结点创建链表并实现#include#include#define LEN sizeof(struct num) struct num { int n; long num; struct num *next; }; int n; struct num * creat(void) { struct num *head; struct num...

帮我随便写下用C语言实现并归排序的代码int a[100]; void Merge(int head,int mid,int tail) { int temp[tail-head+1]; int top=-1; int i=head,j=mid+1; while (i<=mid &amp;&amp; j<=tail) { if (a[i]>a[j]) te...

c语言动态链表如何实现排序c语言动态链表如何实现排序,利用链表怎样实现数据的插入排序统计更改等等:typedef struct node{ int data; struct node *next; }node; node *create() { node *head,*p,*q; i...

C语言定义一个双向链表不是双向循环列表编程实现其中两个节C语言定义一个双向链表不是双向循环列表编程实现其中两个节,C程序编写:输出双链表:#include <stdio.h> struct TNode { TNode pre; TNode next; int seq; }; int main() { TNod...