careercup2.1
2012-04-24 11:07
337 查看
熟悉链表操作。
/*Write code to remove duplicates from an unsorted linked list FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? */ #include <iostream> using namespace std; class Node{ public: int data; Node* next; Node(){this->next = 0;} Node(int a):data(a),next(0){} }; class LinkList{ public: LinkList(){ head = new Node(-1);} LinkList(int ar[],int len){ head = new Node(-1); Node*p = FindTail(); for(int i = 0; i<len; i++) insertion(ar[i]); } Node* FindTail(); Node* head; void insertion(int); Node* del(Node* i, Node*p); void checkredu(); void print()const{ Node* p = head->next; while(p) { cout<<p->data<<" "; p = p->next; } } }; Node* LinkList::FindTail(){ Node* p = head; while( p->next!= 0){ p = p->next; } return p; } void LinkList::insertion(int k){ Node* p = FindTail(); p->next = new Node(k); } Node* LinkList::del(Node* i,Node*p){ p->next = i->next; delete(i); return p->next; } void LinkList::checkredu(){ Node* p = head->next; while(p){ if(p->data == 67) p = p; Node* temp = p->next; Node* par = p; while(temp){ while(temp && temp->data == p->data) { temp = del(temp,par); } if(!temp) break; //tail cut par = temp; temp = temp->next; } p = p->next; } } int main(){ int ar[]={1,3,5,67,8,9,45,78,9,33,12,3,5,4,67,67}; LinkList ll(ar,16); ll.checkredu(); ll.print(); }
相关文章推荐
- careercup-树与图 4.2
- careercup-C和C++ 13.9
- careercup3.2
- CareerCup 1.3
- Recursion 叠箱子最高问题 @CareerCup
- Moderate rand5()产生rand7() @CareerCup
- Hard 随机洗牌函数 @CareerCup
- CareerCup Fork Problem
- CareerCup calculate (x^y)%z without pow();
- CareerCup After the BIOS performs a successful power-on-self-test, describe everything
- CareerCup Generate all the possible substrings
- CareerCup Add two number
- Careercup - Facebook面试题 - 5733320654585856
- Careercup - Microsoft面试题 - 4639756264669184
- careercup-树与图 4.3
- careercup-中等难度 17.3
- CareerCup chapter 1 Arrays and Strings
- careercup2.2
- CareerCup2.5
- Recursion 计算表达式的括号组合 @CareerCup