有关数据结构问题(急救)
- 提问者网友:趣果有间
- 2021-07-24 23:46
- 五星知识达人网友:十鸦
- 2021-07-25 00:21
#include<stdlib.h>
typedef struct node
{int data;
struct node*next;
}lnode,*linklist;
linklist creat_linklist()
{linklist l=NULL;
lnode*s,*r=NULL;
int x;
scanf("%d",&x);
while(x!=-1)
{s=malloc(sizeof(lnode));
s->data=x;
if(l=NULL) l=s;
else r->next=s;
r=s;
scanf("%d",&x);
}
if(r!=NULL) r->next=NULL;
return l;
}
void print_linklist(linklist l)
{lnode*s=l;
while(s!=NULL)
{printf("%4d",s->data);
s=s->next;
}
}
linklist merge(linklist a,linklist b)
{linklist c;
lnode *p,*q,*s;
p=a->next;
q=b->next;
c=a;
c->next=NULL;
free(b);
while(p&&q)
{if(p->data<q->data)
{s=p;
p=p->next;}
else
{s=q;
q=q->next;}
s->next=c->next;
c->next=s;
}
if(p==NULL)p=q;
while(p)
{s=p;
p=p->next;
s->next=c->next;
c->next=s;
}
}
void main()
{linklist a;
linklist b;
a=creat_linklist();
b=creat_linklist();
print_linklist(a);
print_linklist(b);
merge(a,b);
}
- 1楼网友:愁杀梦里人
- 2021-07-25 02:37
define maxsize 20 #include<stdlib.h> typedef struct {int data[maxsize]; int last;}seqlist; seqlist *int_list() {seqlist *l; l=malloc(sizeof(seqlist)); l->last=-1; return l; } void input_list(seqlist *s) {int i; for(i=0;i<6;i++) {s->data[i]=i+1; s->last++;} void print_list(seqlist *l) {int i; print("\n"); for (i=0;i<=last; i++) printf("%4d",data[i]); void merge(seqlist a,seqlist b,seqlist c) {int i,j,k; i=0;j=0;k=0; while(i<=a.last&&j<=b.last) {if(a.data[i]==b.data[i]) c->data[k++]=a.data[i++]} c->last=k-1;} void main() {seqlist *a;seqlist *a; a=init_seqlist(); b=init_seqlist(); intput_list(a); print_list(a); intput_list(b); print_list(b); merge(a,b,c); print_list(c); }
- 2楼网友:鱼忧
- 2021-07-25 01:35
#include <iostream> #include <stdlib.h> using namespace std;
typedef struct lnode { int data; struct lnode *next; }lnode,*linklist;
linklist CreateList(int m) { linklist l,p; int i; l = (linklist)malloc(sizeof(lnode)); l->next = NULL; cout <<"请按从大到小的顺序输入元素的值" << endl; for(i = m; i > 0; --i) { p = (linklist)malloc(sizeof(lnode)); cin >> p->data; p->next = l->next; l->next = p; } return l; }
void MergeList(linklist la, linklist lb, linklist lc) { linklist pa,pb,pc; pa = la->next; pb = lb->next; pc = lc; while (pa && pb) { if(pa->data <= pb->data) { pc->next = pa; pc = pa; pa = pa->next; } else { pc->next = pb; pc = pb; pb = pb->next; } pc->next = pa ? pa : pb; free(la); free(lb); } }
void view(linklist l) { linklist p; p = l->next; for(; p->next!=NULL; p = p->next) { cout << p->data << ","; } cout << p->data << endl; }
void main() { linklist CreateList(int); void MergeList(linklist); void view(linklist);
linklist la,lb,lc; int m,n; cout << "请输入链表la的元素个数" << endl; cin >> m; la = CreateList(m); view(la); cout << "请输入链表lb的元素个数" << endl; cin >> n; lb = CreateList(n); view(lb);
MergeList(la, lb, lc); view(lc); }
- 3楼网友:胯下狙击手
- 2021-07-25 00:46
看看我写的:
list*CreateC(list*A,list*B) { list*pa=A, *pb=B, *tc,*C; C=(list*)malloc(sizeof(list)); tc=C; while(pa!=NULL&&pb!=NULL) {if(pa->data <pb->data) { tc->next=pa; tc=pa; pa=pa->next; }else { tc->next=pb; tc=pb; pb=pb->next; } }
hc->next= NULL; if(pa!=NULL) tc->next=pa; if(pb!=NULL) tc->next=pb; tc=C; C=C->next; free(tc); return C; } [/Quote]