单链表的基本操作:建立,求长度,打印,删除,插入,排序,逆置。
没什么好说的=。=
上代码
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <iostream>
- #include <string>
- #include <math.h>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- /************************************************************************/
- /* 单链表的:建立,求长度,打印,删除,插入,排序,逆置 */
- /************************************************************************/
- struct node {
- int value ;
- node* next ;
- node() {
- next = NULL ;
- }
- node(int v)
- {
- next = NULL ;
- value = v ;
- }
- };
- class SingleLinkList
- {
- public:
- SingleLinkList() {
- head = new node();
- tail = NULL ;
- }
- node* find(int value)
- {
- node* ans = head ;
- while(ans) {
- if( ans->value == value ) break ;
- ans = ans->next ;
- }
- return ans ;
- }
- void push(int value)
- {
- if(tail) {
- node* temp = new node(value);
- tail->next = temp ;
- tail = temp ;
- }
- else{
- head->value = value ;
- tail = head ;
- }
- }
- //插入在pos之后,通常先find,在插入
- void insert(node* pos,int value)
- {
- node *temp = new node(value) ;
- if( NULL == pos )
- {
- //插入链表首
- temp->next = head ;
- head = temp ;
- }
- else
- {
- temp->next = pos->next ;
- pos->next = temp ;
- }
- }
- void del(node* pos)
- {
- node *temp = head , *pre = NULL ;
- while( temp != NULL && temp != pos ) {
- pre = temp ;
- temp = temp->next ;
- }
- if( pre == NULL )
- {
- //删除队首元素
- node* temp = head ;
- head = head->next ;
- delete temp ;
- }
- else
- {
- pre->next = temp->next ;
- delete temp ;
- }
- }
- //冒泡排序
- void sort()
- {
- int i , j , k ;
- node *pre , *cur ;
- for ( i = 0 ; i < length() ; i++)
- {
- pre = head ;
- cur = head->next ;
- for( j = i+1 ; j < length() ; j++)
- {
- if( pre->value > cur->value )
- {
- k = pre->value ;
- pre->value = cur->value ;
- cur->value = k ;
- }
- pre = pre->next ;
- cur = cur->next ;
- }
- }
- }
- void reverse()
- {
- node *h = head , *t = tail , *temp = head->next , *tmp , *pre = head ;
- while(temp)
- {
- tmp = temp->next ;
- temp->next = pre ;
- pre = temp ;
- temp = tmp ;
- }
- head->next = NULL ;
- tmp = head ;
- head = tail ;
- tail = head ;
- }
- void print()
- {
- node *temp = head ;
- while(temp) printf("%d ",temp->value) , temp = temp->next ;
- puts("");
- }
- int length()
- {
- int i = 0 ;
- node *temp = head ;
- while(temp) i++,temp = temp->next ;
- return i ;
- }
- private:
- node *head,*tail;
- };
- int main()
- {
- SingleLinkList list ;
- list.push(3);
- list.push(2);
- list.push(9);
- list.push(1);
- list.push(4);
- puts("initial serial:");
- list.print();
- puts("after reverse:");
- list.reverse();
- list.print();
- printf("length:%d\n",list.length());
- puts("after sorting:");
- list.sort();
- list.print();
- list.del(list.find(1));
- puts("after delete 1:");
- list.print();
- list.del(list.find(3));
- puts("after delete 3:");
- list.print();
- list.del(list.find(9));
- puts("after delete 9:");
- list.print();
- list.insert(list.find(4),44);
- puts("after insert 44 after 4:");
- list.print();
- list.insert(list.find(1),0);
- puts("after insert 0 at the first:");
- list.print();
- }