Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- using namespace std;
- template <typename T>
- class list{
- struct element{
- T inf;
- element* next;
- element(T x) :inf(x), next(0) {}
- };
- int count = 0;
- element* head;
- int size;
- element* findx(T x)
- {
- element* cur = head;
- for (int i = 1; i < size; i++)
- {
- if (cur->inf == x)
- {
- return cur;
- }
- cur = cur->next;
- }
- }
- element* find(int index)
- {
- if ((index < 0) || (index > size))
- {
- return nullptr;
- }
- else
- {
- element* cur = head;
- for (int i = 1; i < index; i++)
- {
- cur = cur->next;
- }
- return cur;
- }
- }
- public:
- list() :head(0), size(0) {}
- bool empty()
- {
- return head == 0;
- }
- int getlength()
- {
- return size;
- }
- T getx(T x)
- {
- element* r = findx(x);
- T i = r->inf;
- return i;
- }
- T get(int index)
- {
- element* r = find(index);
- T i = r->inf;
- return i;
- }
- void insert(int index, T data)
- {
- element* newptr = new element(data);
- size = getlength() + 1;
- if (index == 1)
- {
- newptr->next = head;
- head = newptr;
- }
- else
- {
- element* prev = find(index - 1);
- newptr->next = prev->next;
- prev->next = newptr;
- }
- }
- void remove(int index)
- {
- element* cur;
- --size;
- if (index == 1)
- {
- cur = head;
- head = head->next;
- }
- else
- {
- element* prev = find(index - 1);
- cur = prev->next;
- prev->next = cur->next;
- }
- cur->next = nullptr;
- delete cur;
- }
- void print()
- {
- for (element* cur = head; cur != nullptr; cur = cur->next)
- {
- cout << cur->inf << ' ';
- }
- cout << endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement