Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <fstream>
- using namespace std;
- struct node
- {
- int val;
- node* next;
- node* prev;
- };
- node* SetE(node* &head, int k)
- {
- node* e=head;
- for(int i =0; i<k-1; i++)
- {
- e=e->next;
- }
- return e;
- }
- void AddFirst(node* &head, int value)
- {
- node* tmp = new node;
- tmp->val = value;
- tmp->next = head;
- if (head != NULL)
- {
- head = tmp;
- tmp->prev = NULL;
- tmp->next->prev = tmp;
- }
- else head = tmp;
- }
- void AddWhereYouWant(node *&head, node* e, int value)
- {
- node *tmp = new node;
- tmp->val = value;
- if (head == e)
- {
- AddFirst(head, value);
- }
- else
- {
- e= e->prev;
- tmp->next = e->next;
- e->next = tmp;
- }
- }
- void DeleteFirst(node* &head)
- {
- node* tmp;
- tmp = head;
- if(tmp)
- {
- head = tmp->next;
- delete tmp;
- }
- }
- void DeleteWhereYouWant(node* &head, node* e)
- {
- node* tmp;
- if(head == e)
- {
- DeleteFirst(head);
- }
- else
- {
- tmp = head;
- while(tmp->next != e)
- {
- tmp = tmp->next;
- }
- tmp->next = e->next;
- delete e;
- }
- }
- void DisconnectAllSameValues(node* &head, int value)
- {
- node* tmp;
- tmp = head;
- node* e;
- if(head!=NULL)
- {
- if(head->val == value)
- {
- DeleteFirst(head);
- }
- while(tmp->next->next != NULL)
- {
- if(tmp->next->val==value)
- {
- e=tmp->next->next;
- tmp->next=e;
- if(tmp->next->next==NULL && tmp->next->val==value)
- {
- tmp->next=NULL;
- break;
- }
- }
- tmp = tmp->next;
- }
- }
- else cout << "Lista jest pusta lub nie ma danej wartości";
- }
- void ShowList(node* head)
- {
- node* tmp = head;
- while (tmp != NULL)
- {
- cout << tmp->val << " ";
- tmp = tmp->next;
- }
- }
- void ReadFromFile(node* &head, char* Filename)
- {
- ifstream plik;
- plik.open(Filename);
- int a;
- while (!plik.eof())
- {
- plik >> a;
- AddFirst(head, a);
- }
- }
- int main()
- {
- node* head = NULL;
- ReadFromFile(head, "plik1.txt");
- ShowList(head);
- AddWhereYouWant(head, SetE(head, 4), 55);
- cout << endl;
- ShowList(head);
- DisconnectAllSameValues(head, 3);
- cout << endl;
- ShowList(head);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement