Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. // aids1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. struct node
  12. {
  13.     int val;
  14.     node *next = NULL;
  15.     node *prev;
  16.  
  17. };
  18.  
  19. node *head;
  20.  
  21. void add(node *&head, int val)
  22. {
  23.     node *tmp = new node;
  24.     tmp->val = val;
  25.     if (head == NULL)
  26.         head = tmp;
  27.     else
  28.     {
  29.         tmp->next = head;
  30.         head = tmp;
  31.         tmp->next->prev = tmp;
  32.     }
  33. }
  34.  
  35. void show(node* head)
  36. {
  37.     node* tmp = head;
  38.     while (tmp != NULL)
  39.     {
  40.         cout << tmp->val << endl;
  41.         tmp = tmp->next;
  42.     }
  43. }
  44.  
  45. void readfromfile(node*&head)
  46. {
  47.     int n;
  48.     ifstream f;
  49.     f.open("plik.txt");
  50.     if (f.good())
  51.     {
  52.         while (!f.eof())
  53.         {
  54.             f >> n;
  55.             add(head, n);
  56.         }
  57.     }
  58.     f.close();
  59.  
  60. }
  61.  
  62. void del (node *head, int val)
  63. {
  64.     node *tmp = head;
  65.     while (tmp != NULL)
  66.     {
  67.         if (tmp->val == val)break;
  68.         tmp = tmp->next;
  69.     }
  70.  
  71.     if (head->next == NULL)
  72.     {
  73.         delete tmp;
  74.         head = NULL;
  75.     }
  76.     else if (tmp == NULL)
  77.     {
  78.         cout << "W liscie nie ma podanej wartosci" << endl;
  79.         return;
  80.     }
  81.     else if (tmp->next==NULL)
  82.     {
  83.         tmp->prev->next = NULL;
  84.         delete tmp;
  85.     }
  86.     else
  87.     {
  88.         tmp->prev->next = tmp->next;
  89.         tmp->next->prev = tmp->prev;
  90.         delete tmp;
  91.     }
  92. }
  93.  
  94. void addindex(node *&head, int val,int ind )
  95. {
  96.     node *tmp = head;
  97.     for (int i = 0; i < ind-1; i++)
  98.     {
  99.         tmp = tmp->next;
  100.  
  101.         if (tmp == NULL)break;
  102.     }
  103.  
  104.     if (tmp == NULL)cout << "nie ma w liscie takiego indeksu" << endl;
  105.     else if (tmp->next == NULL)
  106.     {
  107.         add(tmp, val);
  108.         tmp->next = NULL;
  109.     }
  110.     if (head == tmp)
  111.     {
  112.         add(tmp, val);
  113.         head = tmp;
  114.     }
  115.     else
  116.     {
  117.         node*a = tmp->prev;
  118.         add(tmp, val);
  119.         tmp->prev = a;
  120.         tmp->prev->next = tmp;
  121.     }
  122.  
  123. }
  124.  
  125. int main()
  126. {
  127.  
  128.     int a;
  129.     cout << "Wpisz 4 liczby: " << endl;
  130.     for (int i = 0; i < 4; i++)
  131.     {
  132.         cin >> a;
  133.         add(head, a);
  134.     }
  135.  
  136.     readfromfile(head);
  137.  
  138.     cout << "Wypisz liste: " << endl;
  139.     show(head);
  140.  
  141.     del(head, 10);
  142.  
  143.     cout << "Wypisz liste bez 10" << endl;
  144.     show(head);
  145.  
  146.     addindex(head, 99, 3);
  147.     cout << "Na 3 elemencie dodalismy 99" << endl;
  148.     show(head);
  149.  
  150.     addindex(head, 11, 1);
  151.     cout << "Na 1 elemencie dodalismy 11" << endl;
  152.     show(head);
  153.  
  154.     system("pause");
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement