第四周專案5

2021-08-09 06:12:53 字數 4442 閱讀 3973

*/

檔名稱:linklist

作 者:劉思源

完成日期:2023年10月7日

問題描述:設非空線性表ha和hb都用帶頭節點的迴圈雙鏈表表示。

設計乙個演算法insert(ha,hb,i)。其功能是:i=0時,將線性表hb插入到線性表ha的最前面;

當i>0時,將線性表hb插入到線性表ha中第i個節點的後面;當i大於等於線性表ha的長度時,

將線性表hb插入到線性表ha的最後面。

輸入描述:無

輸出描述:ha和hb以及新的線性表

[.h]view plain

copy

#ifndef cdlinklist_h_included

#define cdlinklist_h_included

//迴圈雙鏈表基本運算函式

typedef

intelemtype;  

typedef

struct

dnode        

//定義雙鏈表結點型別

cdlinklist;  

void

createlistf(cdlinklist *&l,elemtype a,

intn); 

//頭插法建立迴圈雙鏈表

void

createlistr(cdlinklist *&l,elemtype a,

intn); 

//尾插法建立迴圈雙鏈表

void

initlist(cdlinklist *&l); 

//初始化迴圈雙鏈表

void

destroylist(cdlinklist *&l);  

//銷毀

bool

listempty(cdlinklist *l); 

//判斷是否為空

intlistlength(cdlinklist *l); 

//求鍊錶長度

void

displist(cdlinklist *l);  

//輸出鍊錶

bool

getelem(cdlinklist *l,

inti,elemtype &e);  

//取鍊錶元素

intlocateelem(cdlinklist *l,elemtype e);  

//查詢元素

bool

listinsert(cdlinklist *&l,

inti,elemtype e);  

//插入節點

bool

listdelete(cdlinklist *&l,

inti,elemtype &e);  

//刪除節點

#endif // cdlinklist_h_included

[cpp]view plain

copy

//迴圈雙鏈表基本運算函式

#include 

#include 

#include "linklist.h"

void

createlistf(cdlinklist *&l,elemtype a,

intn) 

//頭插法建立迴圈雙鏈表

s=l->next;  

while

(s->next!=null)           

//查詢尾結點,由s指向它

s=s->next;  

s->next=l;                      //尾結點next域指向頭結點

l->prior=s;                     //頭結點的prior域指向尾結點

}  void

createlistr(cdlinklist *&l,elemtype a,

intn) 

//尾插法建立迴圈雙鏈表

r->next=l;              //尾結點next域指向頭結點

l->prior=r;             //頭結點的prior域指向尾結點

}  void

initlist(cdlinklist *&l) 

//初始化迴圈雙鏈表

void

destroylist(cdlinklist *&l)  

//銷毀

free(p);  

}  bool

listempty(cdlinklist *l) 

//判斷是否為空

intlistlength(cdlinklist *l) 

//求鍊錶長度

return

(i);  

}  void

displist(cdlinklist *l)  

//輸出鍊錶

printf("\n"

);  

}  bool

getelem(cdlinklist *l,

inti,elemtype &e)  

//取鍊錶元素

else

//i不為1時

if(p==l)  

return

false

;  else

}  }  else

//雙鏈表為空表時

return

0;  

}  int

locateelem(cdlinklist *l,elemtype e)  

//查詢元素

if(p==null)  

return

(0);  

else

return

(n);  

}  bool

listinsert(cdlinklist *&l,

inti,elemtype e)  

//插入節點

else

if(i==1)                  

//原雙鏈表不為空表但i=1時

else

if(p==l)               

//未找到第i-1個結點

return

false

;  else

//找到第i-1個結點*p

}  }  bool

listdelete(cdlinklist *&l,

inti,elemtype &e)  

//刪除節點

else

//i不為1時

if(p==null)                

//未找到第i-1個結點

return

false

;  else

//找到第i-1個結點*p

}  }  else

return

false

;   

//原雙鏈表為空表時

}  

[main]view plain

copy

#include 

#include 

#include "linklist.h"

void

insert(cdlinklist *&ha, cdlinklist *&hb,

inti)  

if(i==0)   

//將hb的所有資料結點插入到ha的頭結點和第1個資料結點之間

else

if(i//將hb插入到ha中間

q=p->next;          //q指向*p結點的後繼結點/

p->next=hb->next;       //hb->prior指向hb的最後乙個結點

hb->next->prior=p;  

hb->prior->next=q;  

q->prior=hb->prior;  

}  else

//將hb鏈到ha之後

free(hb);   //釋放hb頭結點

}  int

main()  

;  initlist(ha);  

createlistf(ha, ha, 10);  

elemtype hb= ;  

initlist(hb);  

createlistf(hb, hb, 5);  

printf("ha: "

);  

displist(ha);  

printf("hb: "

);  

displist(hb);  

insert(ha, hb, 0);  //將0改為其他值,多次執行程式完成測試

printf("new ha: "

);  

displist(ha);  

destroylist(ha);  

return

0;  

}  

第四周專案二

檔名稱 array.cpp 作 者 李中意 完成日期 2014 年 3月 19 日 版本號 v1.0 輸入描述 無 include includeusing namespace std class void setb double y void setc double z double geta v...

第四周專案二

01.02.程式的版權和版本宣告部分 05.檔名稱 score.cpp 06.作 者 07.完成日期 2014年 3 月 25 日 08.版本號 v1.0 09.輸入描述 已經在程式中初始化 10.問題描述 11.輸出 12.include includeusing namespace std cl...

第四周專案 2

include using namespace std class cfraction cfraction cfraction int nu,int de void cfraction set int nu 0,int de 1 void cfraction input void cfraction...