kej

Списки емое)

kej
Apr 15th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include<iomanip>
  4. using namespace std;
  5. struct node
  6. {
  7.     int data;
  8.     node* next;
  9. };
  10. void create_list (node* &top,int n)
  11. {
  12.     node* p;
  13.     top = new node;
  14.     top = NULL;
  15.     for (int i=0;i<n;i++)
  16.     {
  17.         p=new node;
  18.         cin>>p->data;
  19.         p->next=top;
  20.         top=p;
  21.     }
  22. }
  23. void show_list (node* &top)
  24. {
  25.     node* p;
  26.     p=top;
  27.     while(p)
  28.     {
  29.         cout<<p->data;
  30.         p=p->next;
  31.     }
  32.     cout<<endl;
  33. }
  34. void push (node* &top,int data)
  35. {
  36.     node* p;
  37.     p = new node;
  38.     p->data = data;
  39.     p->next = top;
  40.     top=p;
  41. }
  42. node* previous (node* top,node* p)
  43. {
  44.     node* pr,*q;
  45.     q=top;
  46.     pr = NULL;
  47.     while (q!=p&&q)
  48.     {
  49.         pr=q;
  50.         q=q->next;
  51.     }
  52.     if (q==p)
  53.     {
  54.         return pr;
  55.     }
  56.     return NULL;
  57.    
  58. }
  59. node* find_adress (node* top,int x)
  60. {
  61.     node* p = top;
  62.     while (p!=NULL)
  63.     {
  64.         if (p->data == x) {
  65.             return p;
  66.            
  67.         }
  68.           p=  p->next;
  69.     }
  70.     return NULL;
  71. }
  72. void insert_list_after(node* top,node* &q,int x)
  73. {
  74.     node *p;
  75.     p = new node;
  76.     p->data = x;
  77.     p->next= q->next;
  78.     q->next=p;
  79. }
  80. void insert_list_before (node* top,node* &q,int x)
  81. {
  82.     node* p,*r ;
  83.     p = new node;
  84.     r=previous(top,q);
  85.     if (q==top) push(top,x);
  86.     else {
  87.     p->data=x;
  88.     p->next=r->next;
  89.     r->next=p;
  90.         }
  91.     }
  92.      
  93. int main ()
  94. {
  95.     int n;
  96.     int d1;
  97.     int d2;
  98.     int d3;
  99.     node* list;
  100.     node* pt = NULL;
  101.     cout<<"Enter n: ";
  102.     cin>>n;
  103.     create_list(list, n);
  104.     show_list(list);
  105.     cout<<"Enter a num to find: ";
  106.     cin>>d1;
  107.     pt = find_adress(list, d1);
  108.     if (pt)
  109.         cout<<"Data: "<<pt->data<<endl;
  110.     cout<<"Enter a num to insert after: ";
  111.     cin>>d2;
  112.     insert_list_after(list, pt, d2);
  113.     cout<<"Enter a num to insert before: ";
  114.     cin>>d3;
  115.     insert_list_before(list, pt, d3);
  116.     show_list(list);
  117. }
Add Comment
Please, Sign In to add comment