軟件基礎(chǔ)第一次上機(jī)作業(yè)_第1頁(yè)
軟件基礎(chǔ)第一次上機(jī)作業(yè)_第2頁(yè)
軟件基礎(chǔ)第一次上機(jī)作業(yè)_第3頁(yè)
軟件基礎(chǔ)第一次上機(jī)作業(yè)_第4頁(yè)
軟件基礎(chǔ)第一次上機(jī)作業(yè)_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、1、設(shè)有一個(gè)線性表e=e1, e2, , en-1, en,設(shè)計(jì)一個(gè)算法,將線性表逆置,即使元素排列次序顛倒過來(lái),成為逆線性表e= en , en-1 , , e2 , e1 ,要求逆線性表占用原線性表空間,并且用順序表和單鏈表兩種方法表示,分別用兩個(gè)程序來(lái)完成。(文件夾:順序表逆置、單鏈表逆置)線性表:#include #includetypedef char datatype; const int maxsize=1024; typedef struct datatype datamaxsize; int last; sequenlist; void create(sequenlist*&

2、); 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(請(qǐng)輸入數(shù)據(jù):);char ch; while(ch=getchar()!=n) l-last+;l-datal-las

3、t=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 c

4、reat(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-ne

5、xt=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

6、);int main(void)linklist head = null;head = creat(head);printf(建立的單鏈表是:);output(head);inverse(head);printf(逆置后的單鏈表是:);output(head);system(pause); return 0;2、將兩個(gè)非遞減的有序鏈表合并為一個(gè)非遞減的有序鏈表。要求結(jié)果鏈表仍使用原來(lái)兩個(gè)鏈表的存儲(chǔ)空間, 不另外占用其它的存儲(chǔ)空間。表中允許有重復(fù)的數(shù)據(jù)。#include using namespace std; typedef int elemtype; /兩個(gè)遞增的鏈表合并成遞增的鏈表。 ty

7、pedef 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請(qǐng)從小到大輸入鏈表的元素:; 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 mai

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論