軟件基礎(chǔ)第一次上機作業(yè).doc_第1頁
軟件基礎(chǔ)第一次上機作業(yè).doc_第2頁
軟件基礎(chǔ)第一次上機作業(yè).doc_第3頁
軟件基礎(chǔ)第一次上機作業(yè).doc_第4頁
軟件基礎(chǔ)第一次上機作業(yè).doc_第5頁
免費預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

.1、設(shè)有一個線性表E=e1, e2, , en-1, en,設(shè)計一個算法,將線性表逆置,即使元素排列次序顛倒過來,成為逆線性表E= en , en-1 , , e2 , e1 ,要求逆線性表占用原線性表空間,并且用順序表和單鏈表兩種方法表示,分別用兩個程序來完成。(文件夾:順序表逆置、單鏈表逆置)線性表:#include #includetypedef char datatype; const int maxsize=1024; typedef struct datatype datamaxsize; int last; sequenlist; void create(sequenlist*&); void print(sequenlist*); void invert(sequenlist*); void main()sequenlist*L; create(L); printf(建立的順序表是:);print(L); invert(L); printf(逆置后的順序表是:);print(L);void create(sequenlist*&L) L=(sequenlist*)malloc(sizeof(sequenlist); L-last=0; printf(請輸入數(shù)據(jù):);char ch; while(ch=getchar()!=n) L-last+;L-dataL-last=ch;void print(sequenlist*L) for(int i=1;ilast;i+) printf(%2c,L-datai); printf(n);void invert(sequenlist*L) int n=L-last/2; for(int i=1;idatai; L-datai=L-dataL-last-i+1; L-dataL-last-i+1=temp; 鏈表:#include #include #include typedef struct nodeint data;struct node *next;LinkNode, *LinkList;LinkList Creat(LinkList head);void Inverse(LinkList head);void Output(LinkList head);LinkList Creat(LinkList head)/頭插法建表char ch;LinkList p = NULL;LinkList q = NULL;head = (LinkList) malloc (sizeof(LinkNode);head-next = NULL;q = head;while (ch=getchar()!=n)p = (LinkList) malloc (sizeof(LinkNode);p-data=ch;p-next=q-next;q-next = p;return head;void Inverse(LinkList head)/逆置LinkList p = head-next;LinkList tmp = NULL;head-next = NULL;while (p!=NULL )tmp = p-next;p-next = head-next;head-next = p;p = tmp;void Output(LinkList head)/輸出LinkList p = head-next;while (p!= NULL)printf(%d , p-data);p = p-next;printf(n);int main(void)LinkList head = NULL;head = Creat(head);printf(建立的單鏈表是:);Output(head);Inverse(head);printf(逆置后的單鏈表是:);Output(head);system(pause); return 0;2、將兩個非遞減的有序鏈表合并為一個非遞減的有序鏈表。要求結(jié)果鏈表仍使用原來兩個鏈表的存儲空間, 不另外占用其它的存儲空間。表中允許有重復(fù)的數(shù)據(jù)。#include using namespace std; typedef int ElemType; /兩個遞增的鏈表合并成遞增的鏈表。 typedef struct LNode ElemType data; struct LNode *next; LNode; typedef LNode *LinkList; void CreatList(LinkList &L,int n) LinkList p,q; L=new LNode; L-next=NULL; q=L; cout請從小到大輸入鏈表的元素:; for (int i=1;ip-data; p-next=q-next; q-next=p; q=q-next; coutnext; for (int j=1;j=n;j+) coutdatanext; coutnext; pb=B-next; / C=t=A;/ A while (pa|pb) if (!pb|(pa&pb)&(pa-datadata) /將A的元素插入新表 pc=pa; q=pa-next; pa-next=pre; pa=q; else pc=pb; q=pb-next; pb-next=pre; pb=q; /將B的元素插入新表 pre=pc; coutnext=pc; pa=pc; for (int j=1;j=n;j+) coutdatanext; coutendl; getchar(); void main() LinkList A,B,C

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論