Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node
- {
- int info;
- node *next;
- };
- struct list
- {
- node * head;
- node * tail;
- };
- node * creatnode(int x)
- {
- node *p=new node;
- p->info=x;
- p->next=NULL;
- return p;
- }
- void creatlist(list & l)
- {
- l.head=l.tail=NULL;
- }
- void themsau(list &l,node *p)
- {
- if (l.head==NULL)
- {
- l.head=p;
- l.tail=l.head;
- }
- else
- {
- l.tail->next=p;
- l.tail=p;
- }
- }
- void taolist(list &l,int n)
- {
- for (int i=1;i<=n;i++)
- {
- themsau(l,creatnode(i));
- }
- }
- void xoadau(list & l)
- {
- node * p;
- if (l.head!=NULL)
- {
- p=l.head;
- l.head=p->next;
- delete p;
- p=NULL;
- }
- else
- l.tail=NULL;
- }
- void xoa( list & l,node *&X)
- {
- if (l.head== X)
- {
- xoadau(l);
- X=l.head;
- return;
- }
- node *tr = l.head;
- while (tr->next != X)
- tr = tr->next;
- node *p = tr->next;
- tr->next = p->next;
- if (p == l.tail)
- l.tail = tr;
- X=tr->next;
- if(X==NULL)
- X=l.head;
- delete p;
- p=NULL;
- }
- void xuli(list & l,int n,int m)
- {
- node * p=new node;
- p=l.head;
- int k=1;
- while (n>0)
- {
- while (k%m!=0)
- {
- p=p->next;
- if(p==NULL&&p!=l.head)
- p=l.head;
- k++;
- }
- k++;
- cout<< p->info;
- xoa(l,p);
- n--;
- }
- }
- int main()
- {
- list l;
- int m,n;
- cout<<" nhap so nguoi : " ;
- cin>> n;
- cout<< " nhap thu tu nguoi bi giet : ";
- cin>>m;
- creatlist(l);
- taolist(l,n);
- xuli(l,n,m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement