Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <fstream>
- using namespace std;
- struct node
- {
- int key;
- node *next = NULL;
- };
- struct list
- {
- node *head = NULL;
- };
- bool list_search (list *L, node *k)
- {
- node *x=L->head;
- while ((x->key!=k->key) && (x!=NULL))
- x=x->next;
- if (x->key==k->key)
- return true;
- else
- return false;
- }
- void list_insert1(list *L, node *x)
- {
- x->next=L->head;
- L->head=x;
- }
- void list_insert2(list *L, node *x,node *y)
- {
- node *P=L->head;
- while (P != NULL)
- {
- if(P->key==x->key)
- {
- y->next=P->next;
- P->next=y;
- }
- P = P->next;
- }
- }
- int main ()
- {
- list *L = new list;
- for (int i=1;i<=5;i++)
- {
- node *z= new node;
- z->key = i;
- list_insert1(L,z);
- }
- int x,y;
- cout<<"Podaj dwie liczby calkowite: "<<endl;
- cin>>x>>y;
- node *a= new node;
- a->key=x;
- node *b=new node;
- b->key=y;
- if (list_search(L,a))
- list_insert2(L,a,b);
- else
- list_insert1(L,b);
- node *z= L->head;
- while (z!=NULL)
- {
- cout<< z->key <<endl;
- z=z->next;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement