example: linked list a: 1->2->3 linked list b: 4->5->6
my task make function, passes list b list @ given position (n). instance: after "2" = 1->4->5->6->2->3 (output).
i wasn't sure, how this, used:
// function finds number in list , gets address of node.
node* list::find(int i) { (start();!end();next()) if(current->num==i) return current; return null; };
// function, puts freely desired number @ position n in list;
example cin >> i; // = 6;
1->2->6->desired number->...
node* list::addafter(int pos, int num) { node* p = null; current = find(pos); if (current != null) { p = new node(num); p->next = current->next; current->next = p; } if (last == current) last = p; current = p; return p; }
both things works, as:
cout << "after number?" << endl; cin >> i; // **2** l.addafter(i, 1); // before: 2->3->4 after: 2->1->3->4 l.print();
this works perfectly! if have 2 lists - l1 (2->3->4 , l2 6->7) how can put both using function?
node* list::addafter(int pos, i have pass l2 values here) how can give function l2 values parameter? there maybe better way, this? i'd appreciate help.
whole code:
#include <iostream> using namespace std; class node { public: int num; node *next; node (int n) { num = n; next = null; }; }; class list { protected: node *first, *last; public: node *current; public: list () { first = last = current = null; }; void add_element (int n); void delete_element (); void print(); // izdrukā sarakstu bool is_empty () { return (first == null); }; void start () { current = first; }; bool end () { return (current == null); }; void next(){if (!end())current = current -> next;}; node* find(int i); node* addafter(int i, list * l2); ~list(); }; int main() { list l, l2; int k, i; cout << "type number: (0,to stop):\n"; cin >> k; while (k!=0) { l.add_element (k); cout << "type number: (0, stop):\n"; cin >> k; }; cout << endl; cout << "type 2nd list numbers: (0,to stop):\n"; cin >> k; while (k!=0) { l2.add_element (k); cout << "type 2nd list numbers: (0,to stop):\n"; cin >> k; }; cout << endl; l.print(); l2.print(); cout << "after element want insert second list?" << endl; cin >> i; l.addafter(i, l2); // getting error here. l.print(); return 0; }
simplest , easiest form pour out both stacks array
const int size = stacka.size() + stackb.size(); const stackasize = stacka.size(); const stackbsize = stackb.size(); int arrayofstacka [size]; int arrayofstackb [stackbsize]; //pop stacka arraya for(int i=stackasize-1; i>0; i++) { arrayofstacka[i] = stacka.pop(); } //pop stackb arrayb for(int i=stackbsize-1; i>=0; i++) { arrayofstackb[i] = stackb.pop(); }
now find index want insert data in array a. in case want enter stack b after 1 in case of array index:1
int count = 0 for(int i=0;i<size;i++){ if(i<requiredindexnumber){ //do nothing } else if(i>=requiredindexnumber && count!=stackbsize){ int temp = arrayofstacka[i]; arrayofstacka[i] = arrayofstackb[count++]; arrayofstacka[stackasize+i] = temp; } }
this easiest of popping 1 stack other @ index
Comments
Post a Comment