Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node
- {
- int Value;
- Node *Next;
- Node *Prev;
- };
- class DLL
- {
- private:
- Node *Head;
- Node *Tail;
- int Size;
- public:
- DLL()
- {
- Head = Tail = NULL;
- Size = 0;
- }
- ~DLL()
- {
- DeleteAll();
- }
- void AddTail(int Number)
- {
- Node* tmp = new Node();
- tmp->Value = Number;
- tmp->Next = NULL;
- if (Head == NULL)
- {
- tmp->Prev = NULL;
- Head = Tail = tmp;
- }
- else
- {
- Tail->Next = tmp;
- tmp->Prev = Tail;
- Tail = tmp;
- }
- Size++;
- }
- void AddHead(int Number)
- {
- Node* tmp = new Node();
- tmp->Value = Number;
- tmp->Prev = NULL;
- if (Head == NULL)
- {
- tmp->Next = NULL;
- Head = Tail = tmp;
- }
- else
- {
- Head->Prev = tmp;
- tmp->Next = Head;
- Head = tmp;
- }
- Size++;
- }
- int GetSize()
- {
- return Size;
- }
- void DeleteHead()
- {
- Head = Head->Next;
- }
- void DeleteTail()
- {
- Tail = Tail->Prev;
- }
- void DeleteAll()
- {
- while (Head != NULL)
- {
- DeleteHead();
- }
- }
- void Print()
- {
- Node *tmp = new Node();
- tmp = Head;
- while (tmp->Next != NULL)
- {
- cout << tmp->Value << " ";
- tmp = tmp->Next;
- }
- cout << endl;
- }
- /*void Insert(int Index)
- {
- // Need to complete = Ex
- }
- void DeleteMiddle(int Index)
- {
- // Need to complete = Ex
- }*/
- };
- int main()
- {
- DLL List;
- for (int i = 0; i < 10; i++)
- List.AddTail(i);
- cout << endl;
- for (int i = 0; i < 10; i++)
- List.AddHead(i);
- List.Print();
- List.DeleteHead();
- List.Print();
- List.DeleteHead();
- cin.get();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement