Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // aids1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- struct node
- {
- int val;
- node *next = NULL;
- node *prev;
- };
- node *head;
- void add(node *&head, int val)
- {
- node *tmp = new node;
- tmp->val = val;
- if (head == NULL)
- head = tmp;
- else
- {
- tmp->next = head;
- head = tmp;
- tmp->next->prev = tmp;
- }
- }
- void show(node* head)
- {
- node* tmp = head;
- while (tmp != NULL)
- {
- cout << tmp->val << endl;
- tmp = tmp->next;
- }
- }
- void readfromfile(node*&head)
- {
- int n;
- ifstream f;
- f.open("plik.txt");
- if (f.good())
- {
- while (!f.eof())
- {
- f >> n;
- add(head, n);
- }
- }
- f.close();
- }
- void del (node *head, int val)
- {
- node *tmp = head;
- while (tmp != NULL)
- {
- if (tmp->val == val)break;
- tmp = tmp->next;
- }
- if (head->next == NULL)
- {
- delete tmp;
- head = NULL;
- }
- else if (tmp == NULL)
- {
- cout << "W liscie nie ma podanej wartosci" << endl;
- return;
- }
- else if (tmp->next==NULL)
- {
- tmp->prev->next = NULL;
- delete tmp;
- }
- else
- {
- tmp->prev->next = tmp->next;
- tmp->next->prev = tmp->prev;
- delete tmp;
- }
- }
- void addindex(node *&head, int val,int ind )
- {
- node *tmp = head;
- for (int i = 0; i < ind-1; i++)
- {
- tmp = tmp->next;
- if (tmp == NULL)break;
- }
- if (tmp == NULL)cout << "nie ma w liscie takiego indeksu" << endl;
- else if (tmp->next == NULL)
- {
- add(tmp, val);
- tmp->next = NULL;
- }
- if (head == tmp)
- {
- add(tmp, val);
- head = tmp;
- }
- else
- {
- node*a = tmp->prev;
- add(tmp, val);
- tmp->prev = a;
- tmp->prev->next = tmp;
- }
- }
- int main()
- {
- int a;
- cout << "Wpisz 4 liczby: " << endl;
- for (int i = 0; i < 4; i++)
- {
- cin >> a;
- add(head, a);
- }
- readfromfile(head);
- cout << "Wypisz liste: " << endl;
- show(head);
- del(head, 10);
- cout << "Wypisz liste bez 10" << endl;
- show(head);
- addindex(head, 99, 3);
- cout << "Na 3 elemencie dodalismy 99" << endl;
- show(head);
- addindex(head, 11, 1);
- cout << "Na 1 elemencie dodalismy 11" << endl;
- show(head);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement