#include
using namespace std;
class linknode{
public:
int data;
linknode *next;
typedef linknode* nodepointer;
class linklist{
public:
int insertdata();
int insert(int i,int e);
int deleteelem(int i,int &e);
void deleterepeat();
int getelem(int i,int &e);
int getlength();
bool isempty();
void display();
linklist();
int clear();
virtual ~linklist();
protected:
nodepointer head;
linklist::linklist(){
head=new linknode; //設頭結點
head->next=null;
linklist::~linklist(){
clear();
int linklist::clear(){
nodepointer p,q;
q=head;
while(q){
p=q;
q=q->next;
delete p;
head->next=null;
return 0;
int linklist::insertdata(){
int e;
nodepointer p;
nodepointer s;
p=head;
cout<<"請輸入10個整形結點並以enter鍵結束 "s=new linknode;
cin>>e;
s->data=e;
s->next=p->next;
p->next=s;
p=p->next;
return 0;
int linklist::insert(int i,int e){
int j=1;
nodepointer p,s;
p=head;
for( j;jp=p->next;
if(is=new linknode;
s->data=e;
s->next=p->next;
p->next=s;
return 0;
int linklist::deleteelem(int i,int &e){
int j=1;
nodepointer p,q;
p=head;
//p=p->next;
for(j;jp=p->next;
if(i<1||!p) return -1;
q=p->next;
p->next=q->next;
e=q->data;
delete q;
return 0;
int linklist::getelem(int i,int &e){
linknode *p;
p=head;
p=p->next;
for(int j=1;jp=p->next;
e=p->data;
return 0;
void linklist::display(){
linknode *p;
p=head;
p=p->next;
cout<<"鍊錶為:"coutp=p->next;
int main(){
int i;
int e;
int b;
int f;
int g;
int e1;
linklist a;
a.insertdata();
a.display();
coutcin>>i>>e1;
a.insert(i,e1);
a.display();
coutcin>>f;
a.deleteelem(f,e);
cout<<"刪除元素為:"coutcin>>g;
a.getelem(g,b);
cout<<"你查詢位置的結點資料域為:"//我沒有把每乙個函式都實現,自己可以實現一下
單鏈表基本操作
include include include include includeusing namespace std typedef struct node node,plinklist plinklist createfromhead node pstnode node malloc sizeof...
單鏈表基本操作
單鏈表的初始化,建立,插入,查詢,刪除。author wang yong date 2010.8.19 include include typedef int elemtype 定義結點型別 typedef struct node node,linkedlist 單鏈表的初始化 linkedlist...
單鏈表基本操作
include using namespace std define namelenth 20 define ok 0 define error 1 typedef struct flagnode node 生成結點 inline node newnode 銷毀化煉表 void destroylin...