#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef int Elemtype;
typedef struct List
{
Elemtype cur;
struct List *next;
}sqlist, *Node;
void Init_List(Node &L,Elemtype &n)
{
Node s,k;
L=(Node)malloc(sizeof(sqlist));
L->next=NULL;
printf("請創表的長度:\n");
scanf("%d",&n);
int i;
s=(Node)malloc(sizeof(sqlist));
s->next=L->next;
L->next=s;
k=s;
for(i=0;i<n-1;i++)
{
s=(Node)malloc(sizeof(sqlist));
k->next=s;
k=s;
}
k->next=NULL;
}
Node Insert(Node &L,Node k,Node j)
{
L=(Node)malloc(sizeof(sqlist));
L->next=NULL;
printf("開始進行合并\n");
Node p,q,a,c,h;
k->next=p;
q=p;
j->next=a;
c=a;
if(p->cur>a->cur){L->next=p;p=q->next;q=p;}
else{L->next=a;a=c->next;c=a;}
h=L->next;
while(p!=NULL&&a!=NULL)
{
q=p->next;
c=a->next;
if(p->cur>a->cur){h->next=p;h=p;p=q;q=p->next;}
else{h->next=a;h=a;a=c;c=a->next;}
}
return(L);
}
void order(Node &L)
{
Node p,q,r;
printf("進行排序:\n");
p=L->next;
int t=0;
while(p!=NULL)
{
q=p->next;
r=q->next;
while(q!=NULL)
{
if(p->cur<=q->cur){t=q->cur;q->cur=p->cur;p->cur=t ;}
q=r;
r=q->next;
}
q=p->next;
p=q;
}
printf("結束\n");
}
void distory(Node L)
{Node s;
while(s!=NULL)
{ s=L->next;
free(L);
L=s;}
}
void output(Node L)
{
Node s,j;
s=L->next;
j=s;
printf("將得到結果:\n");
while(s!=NULL){printf(" %d\n",s->cur);s=j->next;j=s;}
}
void input(Node L)
{
Node s,j;
printf("單表輸入cur值:\n");
int x;
L->next=s;
j=s;
while(s!=NULL)
{
scanf("%d",&x);
s->cur=x;
j=s->next;
s=j;
}
printf("輸入結束\n");
}
void main()
{
Node t,r,h;
printf("創建兩個表\n");
int k;
Init_List(t,k);
Init_List(r,k);
input(t);
input(r);
order(t);
order(r);
h=Insert(h,t,r);
printf("最后得到\n");
output(h);
getchar();
getchar();
}
運行到半路,就出現press any key to contine,不知道怎么回事?
uj5u.com熱心網友回復:
問題太多了main函式中Init_List(t, k); k是int型的
void Init_List(Node &L, Elemtype &n)為什么變成了地址
這個函式中
L->next = NULL;
L->next = s;
給空指標賦值??
void input(Node L)
{
Node s, j;
printf("單表輸入cur值:\n");
int x;
L->next = s;
j = s;
while (s != NULL)
{
scanf("%d", &x);
s->cur = x;
j = s->next;
s = j;
}
s未初始化 怎么拿來做判斷?
其他的懶得檢查了 你再看看自己到底想干什么 搞清楚每個函式你的目的
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef int Elemtype;
typedef struct List
{
Elemtype cur;
struct List *next;
}sqlist, *Node;
void Init_List(Node &L,Elemtype &n)
{
Node s,k;
int i;
L = (Node)malloc(sizeof(sqlist));
L->next=NULL;
printf("請創表的長度:\n");
scanf("%d", &n);
/*
s=(Node)malloc(sizeof(sqlist));
s->next=L->next;
*/
k = L;
for(i=0;i<n;i++)
{
s=(Node)malloc(sizeof(sqlist));
k->next=s;
k=s;
}
k->next=NULL;
}
//Node Insert(Node &L,Node k,Node j)
Node merge(Node &L,Node a, Node b)
{
Node p, q, s, t;
L=(Node)malloc(sizeof(sqlist));
L->next=NULL;
printf("開始進行合并\n");
p = a->next;
q = b->next;
s = L;
while (p && q) {
if (p->cur < q->cur) {
s->next = p;
s = p;
p = p->next;
} else if (p->cur > q->cur) {
s->next = q;
s = q;
q = q->next;
} else {
s->next = p;
s = p;
t = q;
q = q->next;
p = p->next;
free(t);
}
}
s->next = NULL;
if (p) {
s->next = p;
}
if (q) {
s->next = q;
}
a->next = NULL;
b->next = NULL;
return L;
/*
L=(Node)malloc(sizeof(sqlist));
L->next=NULL;
printf("開始進行合并\n");
Node p,q,a,c,h;
k->next=p;
q=p;
j->next=a;
c=a;
if(p->cur>a->cur){L->next=p;p=q->next;q=p;}
else{L->next=a;a=c->next;c=a;}
h=L->next;
while(p!=NULL&&a!=NULL)
{
q=p->next;
c=a->next;
if(p->cur>a->cur){h->next=p;h=p;p=q;q=p->next;}
else{h->next=a;h=a;a=c;c=a->next;}
}
return(L);
*/
}
void order(Node &L)
{
Node p,q,r;
Elemtype tmp;
p = L->next;
printf("進行排序:\n");
while (p) {
q = p->next;
while (q) {
if (p->cur > q->cur) {
tmp = p->cur;
p->cur = q->cur;
q->cur = tmp;
}
q = q->next;
}
p = p->next;
}
#if 0
printf("進行排序:\n");
p=L->next;
int t=0;
while(p!=NULL)
{
q=p->next;
r=q->next;
while(q!=NULL)
{
if(p->cur<=q->cur){t=q->cur;q->cur=p->cur;p->cur=t ;}
q=r;
r=q->next;
}
q=p->next;
p=q;
}
printf("結束\n");
#endif
}
void distory(Node L)
{
Node s;
while(s!=NULL)
{ s=L->next;
free(L);
L=s;}
}
void output(Node L)
{
Node s,j;
s=L->next;
j=s;
printf("將得到結果:\n");
while(s!=NULL){printf(" %d\n",s->cur);s=j->next;j=s;}
}
void input(Node L)
{
Node s;//,j;
int x;
//L->next=s;
s = L->next;
//j=s;
printf("單表輸入cur值:\n");
while(s != NULL)
{
scanf("%d", &x);
s->cur = x;
s = s->next;
//j=s->next;
//s=j;
}
printf("輸入結束\n");
}
//void main()
int main()
{
Node t,r,h;
printf("創建兩個表\n");
int k;
Init_List(t,k);
Init_List(r,k);
input(t);
input(r);
order(t);
output(t);
order(r);
output(r);
//h=Insert(h,t,r);
h= merge(h,t,r);
printf("最后得到\n");
output(h);
getchar();
getchar();
}
供參考~
uj5u.com熱心網友回復:
資料結構對單鏈表進行資料排序 http://bbs.csdn.net/topics/392201633轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181467.html
標籤:C語言
上一篇:C++物件陣列元素可以復制嗎
下一篇:GDI+,Bitmap
