Cracking The Coding Interview2.3
2014-04-08 22:09
344 查看
#include <iostream> #include <string> using namespace std; class linklist { private: class node { public: node(){} string data; node * next; }; int size; public: node *head; linklist() { head = new node; size = 0; } /***整表创建***/ void Create(string *s,int size) { if (s==NULL || size == 0) { return; } node *p = new node; p->data = s[0]; head->next = p; for (int i = 1; i<size; i++) { node *t = new node; t->data = s[i]; p->next = t; p=p->next; } p->next = NULL; this->size = size; } /***整表删除***/ void Clear() { for (int i = 1;i<size+1; i++) { Delete(i); } head = NULL; size = 0; } /***第i[i为1,2....size,下同]个元素前插入string型e***/ void Insert(int i, string e) { if (i<0 || i>size) { return; } int t = i - 1; node *p = head; node *pn = p->next; while(t!=0) { p = p->next; pn = pn->next; t--; } node *ee = new node; ee->data = e; ee->next = pn; p->next = ee; size ++; } /****获取第i个元素的值,并返回该值**/ string Get(int i) { if (i<=0 || i>size) { return "Error: You input wrong num."; } int t = i; node *p =head; while(t!=0) { p = p->next; t--; } return p->data; } /***删除第i个元素***/ void Delete(int i) { if (i<0 || i>size) { return; } int t = i - 1; node *p = head; node *pn = p->next; while(t!=0) { p = p->next; pn = pn->next; t--; } p->next = pn->next; delete pn; size --; } void Display() { if (head==NULL) { return; } node * p; p=head->next; /*for (int i =0; i<size; i++) { cout<<p->data<<" "; p=p->next; }*/ while(p!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<endl; } ~linklist() { } // Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node. // // EXAMPLE // //Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like a->b->d->e //未考虑c为尾节点的情况 void DeleteNode(node *c) { if (c==NULL || c->next == NULL) { return; } node *cn = c->next; string cc = cn->data; c->next = cn->next; delete cn; c->data = cc; } }; int main() { string str[8]={"sos","OMG","sos","OMG","OMG","OMG","fof","fof"}; linklist s; s.Create(str,8); s.Display(); //s.Insert(2,"bingo"); //s.Display(); //s.Delete(3); //s.Display(); //cout<<s.Get(1)<<endl; //s.Clear(); //s.Display(); s.DeleteNode(s.head->next->next->next); s.Display(); return 0; }
相关文章推荐
- Cracking the Coding Interview Q2.3
- 《Cracking the Coding Interview》——第17章:普通题——题目12
- 《Cracking the Coding Interview》——第18章:难题——题目13
- cracking the coding interview problem solution 1.5
- Cracking the coding interview--Q2.5
- Cracking the coding interview--Q9.7
- Cracking the coding interview--Q4.7
- Cracking the coding interview--Q5.4
- Cracking the coding interview--问题与解答
- Cracking the Coding Interview 10.7
- Cracking the coding interview--Q5.6
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q2
- Cracking the coding interview--Q1.2
- Cracking the coding interview--Q1.3
- Cracking the coding interview--Q8.6
- Cracking the coding interview--Q1.4
- Cracking the coding interview--Q2.2
- Cracking the coding interview--Q12.4
- 《Cracking the Coding Interview》——第2章:链表——题目6