#include #include #include using namespace std;
templatestruct circlistnode
circlistnode(t d, circlistnode*n = null)
:data(d)
,next(n)
{}};
templateclass circlist
circlist(const t& x)
circlist(circlist& l)
destptr->next = first;
} ~circlist()
void makeempty() //將鍊錶置空
circlistnode*p = null;
while(first->next != first)
}int length() const//計算迴圈鍊錶的長度
return count;
} bool isempty()//判空
return false; }
circlistnode*serach(t x)//搜尋含資料x的元素
circlistnode* p = first->next;
while(p != first)
p = p->next;
} return p;
} circlistnode*locate(int i)//搜尋第i個元素的位址
circlistnode* current = first->next;
int k = 0;
while(current != first && k < i)
return current;
} t *getdata(int i) //取出第i個元素的值
return &(current->data);
} void setdata(int i, t& x)//用x修飾第i個元素的值
current->data = x;
} bool insert(int i, t& x)//在第i個元素後插入x
circlistnode* s = new circlistnode(x);
s->next = current->next;
current->next = s;
return true;
} bool remove(int i, t& x)//刪除第i個元素,x 返回該元素的值
circlistnode* current = locate(i-1);
if(current == null && current->next == first)
circlistnode* del = current->next;
current->next = del->next;
x = del->data;
delete del;
return true;
} void pushback(const t& x)
circlistnode*s = new circlistnode(x);
p->next = s;
s->next = first;
} void input() //輸入 }
void output() //輸出
cout << "first" << endl;
} circlist&operator=(circlist&l)//過載函式,賦值
destptr->next = first;
return *this;
}private:
circlistnode* first;
};
還有其約瑟夫環的實現:
templatevoid josephus(circlist&js, int n, int m)
} cout << "出列的人是:" << p->data << endl;
pre->next = p->next;
delete p;
p = pre->next;
} cout <<"剩下的人是:" << p->data << endl;
}int main()
josephus(clist, n, m);
return 0;
}
C 雙向迴圈鍊錶(殷人昆版)
include include include using namespace std templatestruct dblnode dblnode t d,dblnode left null,dblnode right null data d llink left rlink right temp...
快速排序(殷人昆版)
快速排序的基本思想 任取待排序元素序列中的某個元素 例如取第乙個元素 作為基準,按照該元素的排序碼大小,將整個元素序列劃分為左右兩個子串行 左側子串行中元素的排序碼都小於基準元素的排序碼,右側子串行中的元素的排序碼都大於基準元素的排序碼。然後在左右序列中重複這種操作。void swap int a,...
迴圈鍊錶(C語言版)
circlelinklist.h define ok 1 define error 0 typedef int status typedef int elemtype typedef struct cnode cnode typedef struct cnode clinklist status i...