連結串列 (自寫模板)和一個簡單的應用(多項式相加和相乘)
CreateList(list_own *&L,int n) 建立連結串列
print(list_own *L) 列印連結串列
List_insert(list_own *L,int i,int num) 連結串列插入,插入在i之後。
select(list_own *L,int i) 查詢第i個元素
revise(list_own *L,int i,int num) 將第i個元素替換為
list_spot_delete(list_own *L,int i) 將連結串列中第i個節點刪除
DestroyList(list_own* &L) 刪除連結串列
ListEmpty(list_own* &L) 判斷連結串列是否為空
ListLength(list_own* L) 返回連結串列長度
select_0(list_own *L,int num) 查詢連結串列中是否含有某個節點的值
Connect(list_own *&L1,list_own *&L2) 將倆個連結串列連線在一起
Insert(list_own*&L,list_own*&num,int n) 將一個節點插入到連結串列中
#include<bits/stdc++.h> using namespace std; struct list_own { int num; list_own *next; }; void CreateList(list_own *&L,int n) { L=(list_own*)malloc(sizeof(list_own)); list_own* p1=(list_own*)malloc(sizeof(list_own)); L->next=NULL; p1=L; list_own* p; for(int i=0;i<n;i++) { int tmp_num; p=(list_own*)malloc(sizeof(list_own)); scanf("%d",&tmp_num); p->num=tmp_num; p->next=p1->next; p1->next=p; p1=p; } } void print(list_own *L) { for(list_own* p_i=L->next;p_i!=NULL;p_i=p_i->next) { printf("%d ",p_i->num); } printf("\n"); } void List_insert(list_own *L,int i,int num) { list_own *tmp_p=L->next; int t=0; if(i==0) { list_own*s=(list_own*)malloc(sizeof(list_own)); s->next=tmp_p; L->next=s; s->num=num; return; } for(list_own* p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==i) { list_own*s=(list_own*)malloc(sizeof(list_own)); s->next=p_i->next; s->num=num; p_i->next=s; break; } } } int select(list_own *L,int i) { list_own *tmp_p=L->next; int t=0; bool falg=false; for(list_own* p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==i) { falg=true; return p_i->num; } } if(!falg) { printf("i>0&&i<=n"); return 0; } } void revise(list_own *L,int i,int num) { list_own *tmp_p=L->next; int t=0; bool falg=false; for(list_own *p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==i) { p_i->num=num; falg=true; return ; } } if(!falg) { printf("i>0&&i<=n"); return ; } } void list_spot_delete(list_own *L,int i) { list_own *tmp_p=L->next; int t=0; if(i==1) { L->next=(tmp_p->next); free(tmp_p); return ; } for(list_own *p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==(i-1)) { list_own *q=p_i->next; p_i->next=((p_i->next)->next); q->next=NULL; free(q); return ; } } } void DestroyList(list_own* &L) { list_own* p; while(L) { p=L->next; free(L); L=p; } printf("連結串列空間釋放\n"); } bool ListEmpty(list_own* &L) { if(L->next==NULL) return true; else { return false; } } int ListLength(list_own* L) { int ans=0; for(list_own* p_i=L->next;p_i!=NULL;p_i=p_i->next) { ans++; } return ans; } void select_0(list_own *L,int num) { int t=0; for(list_own* p_i=L->next;p_i!=NULL;p_i=p_i->next) { t++; if(p_i->num==num) { printf("尋找成功,在%d位置",t); return ; } } printf("沒有找到\n"); } void Connect(list_own *&L1,list_own *&L2) { for(list_own *tmp_L1=L1->next;;tmp_L1=tmp_L1->next) { if((tmp_L1->next)==NULL) { tmp_L1->next=L2->next; break; } } DestroyList(L2); printf("連結串列連線成功,頭節點為第一個連結串列的頭節點\n"); } void Insert(list_own*&L,list_own*&num,int n) { if(num>0) { int t=0; for(list_own *i=L->next;i!=NULL;i=i->next) { t++; if(t==n-1) { num->next=i->next; i->next=num; break; } } } } /* 功能: CreateList(list_own *&L,int n) 建立連結串列 print(list_own *L) 列印連結串列 List_insert(list_own *L,int i,int num) 連結串列插入,插入在i之前。 select(list_own *L,int i) 查詢第i個元素 revise(list_own *L,int i,int num) 將第i個元素替換為 list_spot_delete(list_own *L,int i) 將連結串列中第i個節點刪除 DestroyList(list_own* &L) 刪除連結串列 ListEmpty(list_own* &L) 判斷連結串列是否為空 ListLength(list_own* L) 返回連結串列長度 select_0(list_own *L,int num) 查詢連結串列中是否含有某個節點的值 Connect(list_own *&L1,list_own *&L2) 將倆個連結串列連線在一起 Insert(list_own*&L,list_own*&num,int n) 將一個節點插入到連結串列中 */ int main () { }
多項式相加與相乘:
#include<bits/stdc++.h> using namespace std; struct list_own { int num; list_own *next; }; const int maxn=1e5; int a[maxn]; void CreateList(list_own *&L,int n) { L=(list_own*)malloc(sizeof(list_own)); list_own* p1=(list_own*)malloc(sizeof(list_own)); L->next=NULL; p1=L; list_own* p; for(int i=0;i<n;i++) { int tmp_num; p=(list_own*)malloc(sizeof(list_own)); scanf("%d",&tmp_num); p->num=tmp_num; p->next=p1->next; p1->next=p; p1=p; } } void print(list_own *L) { for(list_own* p_i=L->next;p_i!=NULL;p_i=p_i->next) { printf("%d ",p_i->num); } printf("\n"); } void List_insert(list_own *L,int i,int num) { list_own *tmp_p=L->next; int t=0; if(i==0) { tmp_p=NULL; list_own*s=(list_own*)malloc(sizeof(list_own)); s->next=tmp_p; L->next=s; s->num=num; return; } for(list_own* p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==i) { list_own*s=(list_own*)malloc(sizeof(list_own)); s->next=p_i->next; s->num=num; p_i->next=s; break; } } } int select(list_own *L,int i) { list_own *tmp_p=L->next; int t=0; bool falg=false; for(list_own* p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==i) { falg=true; return p_i->num; } } if(!falg) { printf("i>0&&i<=n"); return 0; } } void revise(list_own *L,int i,int num) { list_own *tmp_p=L->next; int t=0; bool falg=false; for(list_own *p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==i) { p_i->num=num; falg=true; return ; } } if(!falg) { printf("i>0&&i<=n"); return ; } } void list_spot_delete(list_own *L,int i) { list_own *tmp_p=L->next; int t=0; if(i==1) { L->next=(tmp_p->next); free(tmp_p); return ; } for(list_own *p_i=tmp_p;p_i!=NULL;p_i=p_i->next) { t++; if(t==(i-1)) { list_own *q=p_i->next; p_i->next=((p_i->next)->next); q->next=NULL; free(q); return ; } } } void DestroyList(list_own* &L) { list_own* p; while(L) { p=L->next; free(L); L=p; } printf("連結串列空間釋放\n"); } bool ListEmpty(list_own* &L) { if(L->next==NULL) return true; else { return false; } } int ListLength(list_own* L) { int ans=0; for(list_own* p_i=L->next;p_i!=NULL;p_i=p_i->next) { ans++; } return ans; } void select_0(list_own *L,int num) { int t=0; for(list_own* p_i=L->next;p_i!=NULL;p_i=p_i->next) { t++; if(p_i->num==num) { printf("尋找成功,在%d位置",t); return ; } } printf("沒有找到\n"); } void Connect(list_own *&L1,list_own *&L2) { for(list_own *tmp_L1=L1->next;;tmp_L1=tmp_L1->next) { if((tmp_L1->next)==NULL) { tmp_L1->next=L2->next; break; } } DestroyList(L2); printf("連結串列連線成功,頭節點為第一個連結串列的頭節點\n"); } void Insert(list_own*&L,list_own*&num,int n) { if(num>0) { int t=0; for(list_own *i=L->next;i!=NULL;i=i->next) { t++; if(t==n-1) { num->next=i->next; i->next=num; break; } } } } /* 功能: CreateList(list_own *&L,int n) 建立連結串列 print(list_own *L) 列印連結串列 List_insert(list_own *L,int i,int num) 連結串列插入,插入在i之後。 select(list_own *L,int i) 查詢第i個元素 revise(list_own *L,int i,int num) 將第i個元素替換為 list_spot_delete(list_own *L,int i) 將連結串列中第i個節點刪除 DestroyList(list_own* &L) 刪除連結串列 ListEmpty(list_own* &L) 判斷連結串列是否為空 ListLength(list_own* L) 返回連結串列長度 select_0(list_own *L,int num) 查詢連結串列中是否含有某個節點的值 Connect(list_own *&L1,list_own *&L2) 將倆個連結串列連線在一起 Insert(list_own*&L,list_own*&num,int n) 將一個節點插入到連結串列中 */ int main () { list_own *La; list_own *Lb; int n,m,x; cout<<"規則如下:"<<endl<<"1:格式為:a*x^0+b*x^1+c*x^2+......"<<endl; cout<<"2:輸入1進入多項式相加,輸入2進入多項式相乘"<<endl; scanf("%d",&x); if(x==1) { cout<<"請分別輸入倆個多項式的最高次冪是多少"<<endl; scanf("%d%d",&n,&m); cout<<"輸入係數a,b,c........."<<endl; CreateList(La,n); CreateList(Lb,m); list_own *Lc; Lc=(list_own*)malloc(sizeof(list_own)); int t=0; int num1,num2; for(list_own *p1=La->next,*p2=Lb->next;p1!=NULL||p2!=NULL;) { if(p1!=NULL&&p2==NULL) { num2=0; num1=p1->num; p1=p1->next; } else if(p2!=NULL&&p1==NULL) { num1=0; num2=p2->num; p2=p2->next; } else { num1=p1->num; num2=p2->num; p1=p1->next; p2=p2->next; } List_insert(Lc,t++,num1+num2); } t=0; for(list_own *p1=Lc->next;p1!=NULL;p1=p1->next) { if(p1->next!=NULL) printf("%d*x^%d+",p1->num,t++); else { printf("%d*x^%d=0\n",p1->num,t++); } } } else if(x==2) { cout<<"請分別輸入倆個多項式的最高次冪是多少"<<endl; scanf("%d%d",&n,&m); cout<<"輸入係數a,b,c........."<<endl; CreateList(La,n); CreateList(Lb,m); list_own *Lc; Lc=(list_own*)malloc(sizeof(list_own)); int tmp=0; int t=0; int t1=1,t2=1; for(list_own *p1=La->next;p1!=NULL;p1=p1->next) { int tmp_num=0; for(list_own *p2=La->next;p2!=NULL;p2=p2->next) { tmp_num=(p1->num*p2->num); a[t1+t2]=tmp_num; t2++; } t1++; t2=1; } t2=ListLength(Lb); t1=ListLength(La); cout<<"t2:"<<t2<<endl; cout<<"t1:"<<t1<<endl; for(int i=2;i<=t1+t2;i++) { List_insert(Lc,i-2,a[i]); } t=0; for(list_own *p1=Lc->next;p1!=NULL;p1=p1->next) { if(p1->next!=NULL) { printf("%d*x^%d+",p1->num,t++); } else { printf("%d*x^%d\n",p1->num,t++); } } } return 0; }