大二数据结构2(线性表)

it2026-02-11  12

大二数据结构2(线性表)

锐格上的题型

8559

# include <iostream> # include <bits/stdc++.h> using namespace std; # define maxsize 100 typedef int ElemType; typedef struct{ ElemType elem[maxsize]; int sizeth; }linelist; void initlist (linelist &head) {if(!head.elem) exit(0); head.sizeth=0; } void T(linelist &head,ElemType m) {int n; int i; for(i=0;i<m/2;i++) { n=head.elem[i]; head.elem[i]=head.elem[m-i-1]; head.elem[m-i-1]=n; } for(i=0;i<m;i++) {printf("%d ",head.elem[i]); } } int main() { int n,m,i,j; scanf("%d",&n); linelist(head); initlist(head); for(i=0;i<n;i++) { scanf("%d",&m); head.elem[i]=m; } for(i=0;i<n;i++) { printf("%d ",head.elem[i]); } printf("\n"); T(head,n); return 0; }

#include <iostream> #include <bits/stdc++.h> using namespace std; # define maxsize 100 typedef int ElemType; typedef struct node { int elem; struct node *next; }linelist; linelist *creat() { int m; linelist *tp; linelist *head; head=new linelist; head->next=NULL; tp=head; while(~scanf("%d",&m)) { if(m==0) break; linelist *p; p=new linelist; p->elem=m; p->next=tp->next; tp->next=p; tp=p; } return head; } int main() { int i,j; linelist *head; head=creat(); head=head->next; while(head!=NULL) { printf("%d ",head->elem); head=head->next; } /* for(i=n-1;i>=0;i--) { printf("%d ",head.elem[i]); }*/ return 0; }

#include <iostream> #include <bits/stdc++.h> using namespace std; # define maxsize 100 typedef int ElemType; typedef struct node { int elem; struct node *next; }linelist; linelist *creat() { int m; linelist *tp; linelist *head; head=new linelist; head->next=NULL; tp=head; while(~scanf("%d",&m)) { if(m==0) break; linelist *p; p=new linelist; p->elem=m; p->next=tp->next; tp->next=p; } return head; } int main() { int i,j; linelist *head; head=creat(); head=head->next; while(head!=NULL) { printf("%d ",head->elem); head=head->next; } return 0; }

#include <iostream> #include <bits/c++io.h> using namespace std; typedef struct List{ int data; struct List *next; }linelist; void init(linelist *Head,int num){ linelist *tp; linelist*now; tp=Head; now=Head->next; while(now!=NULL){ if(num<=now->data){ linelist *p; p=new linelist; p->data=num; p->next=now; tp->next=p; break; } now=now->next; tp=tp->next; } if(!now){ linelist *node; node=new linelist; node->data=num; node->next=NULL; tp->next=node; } } int main() { linelist *Head; Head=new linelist; Head->next=NULL; int m; while(~scanf("%d",&m)&&m!=0){ init(Head,m); } linelist *p; p=Head->next; while(p!=NULL){ printf("%d ",p->data); p=p->next; } printf("\n"); return 0; }

#include <iostream> #include <bits/stdc++.h> using namespace std; # define maxsize 100 typedef int ElemType; typedef struct node { int elem; struct node *next; }linelist; linelist *creat() { int m; linelist *tp; linelist *head; head=new linelist; head->next=NULL; tp=head; while(~scanf("%d",&m)) { if(m==0) break; linelist *p; p=new linelist; p->elem=m; p->next=tp->next; tp->next=p; tp=p; } linelist *tp1; tp1=head; linelist *now; now=head->next; while(now) { if(now->elem%2==0) { tp1->next=now->next; free(now); now=tp1->next; } else{ now=now->next; tp1=tp1->next; } } linelist *l; l=head->next; while(l!=NULL) { printf("%d ",l->elem); l=l->next; } return head; } int main() { int i,j; linelist *head; head=creat(); /* for(i=n-1;i>=0;i--) { printf("%d ",head.elem[i]); }*/ return 0; }

#include <iostream> #include <bits/stdc++.h> using namespace std; typedef int ElemType; typedef struct node { int elem; struct node *next; }linelist; linelist *creat() { int m; linelist *tp; linelist *head; head=new linelist; head->next=NULL; tp=head; while(~scanf("%d",&m)) { if(m==0) break; linelist *p,*pre,*p2; pre=head; p=head->next; while(p!=NULL&&p->elem>m) { pre=p; p=p->next; } p2=new linelist; p2->elem=m; p2->next=p; pre->next=p2; } return head; } void print(linelist *head) { linelist *p; p=head->next; while(p!=NULL) { printf("%d ",p->elem);p=p->next; } } void mergelist(linelist *head,linelist *head1,linelist *Head) { linelist *p1,*p2,*Head1; p1=head->next; p2=head1->next; Head=new linelist; Head1=Head; while(p1&&p2) { if(p1->elem>p2->elem) { Head1->next=p1; Head1=p1; p1=p1->next; } else { Head1->next=p2; Head1=p2; p2=p2->next; } } if(p2!=NULL) { Head1->next=p2; } if(p1!=NULL) { Head1->next=p1; } print(Head); } int main() { linelist *p; linelist *head,*head1; head=new linelist; head1=new linelist; linelist *Head; head=creat(); head1=creat(); mergelist(head,head1,Head); return 0; }

#include <iostream> using namespace std; #include <bits/stdc++.h> typedef struct student { int data; struct student *next; }stu; void creat(stu *a,stu *b) {int m; struct student *p1,*p2,*p3,*p4; p1=a;p3=b; while(~scanf("%d",&m)&&m!=0) {if(m%2!=0) {p2=(stu*)malloc(sizeof(stu)); p2->data=m; p1->next=p2;p1=p2; } else if(m%2==0) {p4=(stu*)malloc(sizeof(stu)); p4->data=m; p3->next=p4;p3=p4; } } p1->next=NULL; p3->next=NULL; } void pr(stu *a) {stu *b; b=a->next; while(b!=NULL) {printf("%d ",b->data); b=b->next; } } int main() {int i,n; stu *d,*b; d=(stu*)malloc(sizeof(stu)); d->next=NULL; b=(stu*)malloc(sizeof(stu)); b->next=NULL; creat(d,b); pr(d); printf("\n"); pr(b); return 0; }

#include <iostream> #include <bits/stdc++.h> using namespace std; typedef struct l { int coef; int expn; struct l *next; }list1; list1 *creat() { list1*head,*r; head=new list1; r=head; int n,i,x,y; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d,%d",&x,&y); list1 *p; p=new list1; p->coef=x;p->expn=y; r->next=p;r=p; } r->next=NULL; return head; } list1 *hebing(list1 *L1,list1 *L2) { int sum; list1*p1,*p2,*q,*p; p1=L1->next; p2=L2->next; q=L1; while(p1!=NULL&&p2!=NULL) { if(p1->expn==p2->expn) { sum=p1->coef+p2->coef; if(sum==0) { p=p1;p1=p1->next; free(p); p=p2;p2=p2->next; free(p); } else { p1->coef=sum; q->next=p1;q=q->next; p1=p1->next; p=p2;p2=p2->next; free(p); } } else if(p1->expn<p2->expn) { q->next=p1;q=q->next; p1=p1->next; } else if(p1->expn>p2->expn) { q->next=p2;q=q->next; p2=p2->next; } } if(p1!=NULL) q->next=p1; else q->next=p2; return L1; } void print(list1 *L) { list1 *p; p=L->next; while(p!=NULL) { printf("%d*x^%d ",p->coef,p->expn); p=p->next; } } int main() { list1 *L1,*L2,*L; L1=creat(); L2=creat(); L=hebing(L1,L2); print(L); return 0; }

好不容易写完了,总不能让我不发吧,啊哈哈哈哈哈

最新回复(0)