1. How can I exchange two adjacent elements by adjusting the chain instead of the data?
//Unidirectional linked listNode*P*Afterp; P=Beforep -Next Afterp=P -Next P -Next=Afterp -Next Beforep -Next=Afterp; Afterp -Next=P//doubly linked listNode*Beforep,*Afterp; Beforep=P -Prev Afterp=P -Next P -Next=Afterp -Next Beforep -Next=Afterp; Afterp -Next=P P -Next -Prev=P P -Prev=Afterp; Afterp -Prev=Beforep;
2. How to find the intersection and the set of two sorted tables L1 and L2.
//IntersectionTemplate<TypeNameObject> list<Object>Intersection (Const list<Object>& L1,Const list<Object>& L2) { list<Object>IntersectTypeName list<Object>:: Const_iterator iterL1 = L1.begin ();TypeName list<Object>:: Const_iterator iterL2 = L2.begin (); while(IterL1! = L1.end () && iterL2! = L2.end ()) {if(*ITERL1 = = *ITERL2) {Intersect.push_back (*ITERL1); iterl1++; iterl2++; }Else if(*iterl1 < *ITERL2) iterl1++;Elseiterl2++; }returnintersect;}
//and set//Assumes both input lists is sortedTemplate<TypeNameObject> list<Object>Listunion (Const list<Object>& L1,Const list<Object>& L2) { list<Object>ResultTypeName list<Object>:: Const_iterator iterL1 = L1.begin ();TypeName list<Object>:: Const_iterator iterl2= l2.begin (); while(IterL1! = L1.end () && iterL2! = L2.end ()) {if(*ITERL1 = = *ITERL2) {Result.push_back (*ITERL1); iterl1++; iterl2++; }Else if(*iterl1 < *ITERL2) {Result.push_back (*ITERL1); iterl1++; }Else{Result.push_back (*ITERL2); iterl2++; } }returnResult;}
3. A one-way list with a header node, no trailing nodes, and a pointer to a table header node, write a class that includes the following functions:
Returns the size of the linked list,
Print linked list,
The detection value x is in the linked list,
If x is not in the linked list, then join the list,
If X is deleted in the linked list.
Template <typenameObject>structnode{ObjectData Node *next; Node (Const Object& d =Object(), Node *n=null):d ata (d), Next (n) {}};template <typenameObject> class singlelist{ Public: Singlelist () {init ();} ~singlelist () {eraselist (head); } singlelist (ConstSinglelist & RHS) {eraselist (head); Init (); * This=RHS; }BOOLAddObjectx) {if (contains (x)) {return false; }Else{node<Object> *ptr =Newnode<Object> (x); ptr->next=head->next; head->next=ptr; thesize++; }return true; }BOOLRemoveObjectx) {if (!contains (x))return false;Else{node<Object>*ptr=head->next; node<Object>*trailer; while(ptr->data!=x) {trailer=ptr; ptr=ptr->next; } trailer->next=ptr->next; Delete ptr; thesize--; }return true; }intSize () {returnthesize; }voidPrint () {node<Object> *ptr=head->next; while(Ptr!=null) {count<<ptr->data<<" "; ptr=ptr->next; } count<<endl; }BOOLContainsConst Object& x) {node<Object> * ptr=head->next; while(Ptr!=null) {if (x==ptr->data)return true;Elseptr=ptr->next; }return false; }voidInit () {thesize=0; Head=Newnode<Object>; head->next=null; }voidEraselist (node<Object> * h) {node<Object> *ptr=h; node<Object> *nextptr; while(Ptr!=null) {nextptr=ptr->next; Delete ptr; Ptr=nextptr; } }Private: node<Object> *head;intThesize;};
4. How to add support for operator– to the list iterator class.
operator-- (){ current=current->prev; return *thisoperator-- (int){ const_iterator old=*this; --(*this); return old;}
5. The double-ended queue (deque) mentioned earlier adds the following features to it:
function |
Notes |
Push (x) |
Inserting item X into the front segment of a double-ended queue |
Pop () |
Delete the previous item from the double-ended queue and return |
Inject (x) |
Inserting item X into the end of a double-ended queue |
Eject () |
Remove the end item from the double-ended queue and return it |
template <typename Object>classdeque{public: deque(){ l();} void push (Object obj) {l.push_front(obj);} Object pop () {Object obj=l.front();pop_front();return obj;} void inject (Object obj) {l.push_back(obj);} Object eject() {pop_back(obj);}private: list<Object> l;}
6. Does not include the table header node and the tail node, uses the unidirectional list to implement the Stack class efficiently.
Template<typenameObject>structnode{node () {next=NULL;} NodeObjectOBJ): Data(obj){} NodeObjectObj,node * ptr): Data(obj), next(ptr){} Object data;node *next;};Template<typenameObject>classStack{public:stack(){head=NULL;} ~stack(){While(head)Pop();} void push(Object obj){node<object> *ptr=new node<Object>(obj,head); Head=ptr; }ObjectTop(){return(head-to-data); } void Pop(){node<Object> *ptr=head->next; Delete head; Head=ptr; }private:node<Object> *head;}
7. Does not include the table header node and the tail node, uses the unidirectional list to implement the queue class efficiently.
Template<TypeName Object>ClassQueue{ Public:Queue() {Front=NULL; rear=NULL;} ~Queue(){ while(front) deque ();}voidDeque (Object obj) {node<Object> *Ptr=NewNode<Object>(obj,NULL);if(rear) rear=Rear -Next=ptrElseFront=Rear=ptr } object Deque () {Object Temp=Front -Data; Node<Object> *Ptr=Frontif(Front -Next==NULL) Front=Rear=NULL;ElseFront=Front -Next Delete ptr;returnTemp }Private: node<Object> *Front Node<Object> *Rear;}
8. Use the vector as a basic data structure of the loop array to implement the queue class efficiently.
Template<TypeNameObject>class Queue{ Public:Queue(ints): MaxSize (s), Front (0), Rear (0) {elements.resize (maxSize);}Queue() {maxsize= -; front=0; rear=0; Elements.resize (maxSize);} ~Queue(){ while(front!=rear)deque();}voidEnque (Object obj) {if(!full ()) {elements[rear]=obj; Rear= (rear+1)%maxsize; }} Objectdeque() {Object temp;if(!empty ()) {Temp=elements[front]; Front= (front+1)%maxsize;returnTemp } }BOOLEmpty () {returnFront==rear;}BOOLFull () {return(rear+1)%maxsize==front;}Private:intFront,rear;intMaxSize; vector<Object>elements;}
Several operations on tables, stacks, and queues