C 實現單鏈表 不含頭結點

2021-05-27 10:01:37 字數 1676 閱讀 5999

vs2005執行通過,如有問題,請各位大牛指正。

注意:單鏈表不含有頭結點

#include using namespace std;

template//定義結點

struct node

;//定義鍊錶

templateclass linklist

;templatelinklist::linklist() //初始化時,只有乙個頭結點,有head指向

templatelinklist::linklist(const linklist&otherlist)

else

otherlistcurrent = otherlistcurrent->next;

} } }

templateconst linklist& linklist::operator=(const linklist&otherlist)//賦值函式

if(otherlistcurrent!=null)

else

otherlistcurrent = otherlistcurrent->next;

} } }

return *this;//為了連續賦值

}templatelinklist::~linklist()

templatevoid linklist::createlistforward()//頭插法

}templatevoid linklist::createbackward()//尾插法

else

}}templatevoid linklist::initlist() //所有結點都銷毀

templatebool linklist::isemptylist()

else }

templateint linklist::length()

templatevoid linklist::destorylist()//銷毀包括頭結點

head=null;

len=0;

}templatevoid linklist::getfirstdata(type& firstitem)

else

newitem = current->data; }}

templatevoid linklist::insertfirst(type newitem)

templatevoid linklist::insertlast(type newitem)

node*newnode = new node;

newnode->data = newitem;

if (current==null)//鍊錶為空

else

len++;

}templatevoid linklist::deletefirst(type& data)

else

node* nextcurrent = current->next;

current->next = nextcurrent->next;

delete nextcurrent;

} len--; }

templatevoid linklist::reverse() }}

templateostream& operator<< <>(ostream& cout,const linklist& list)

} else

{ cout<<"鍊錶為空!"<

c實現無頭結點單鏈表

標頭檔案 ifndef linklist h define linklist h include include include include typedef int datatype typedef struct node node,pnode,plist void initlinklist p...

有頭結點的單鏈表(java實現)

class node node int data class list public void insert int data node.next tmp.next tmp.next node public void delete int data tmp tmp.next public void ...

單鏈表操作 頭結點方式

單鏈表 訪問時,只能通過表頭遍歷進行訪問,遍歷結束的條件是最後乙個節點的為null。單鏈表中可以分為資料域和指標域。資料域為使用者儲存資料的變數。指標域則指向下乙個節點。一般單鏈表操作可以分為頭節結方式和頭指標方式 struct node root null 方式。由於單鏈表訪問節點只有一條路徑,因...