Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cstdlib>
- using namespace std;
- class Node {
- public:
- Node* next = nullptr;
- float data;
- };
- class CMyLinkedList {
- public:
- int length = 0;
- Node* mHead = nullptr;
- ~CMyLinkedList() {
- cout << "LIST DELETED";
- }
- void addToEnd(float data) {
- Node* nodeToInsert = new Node();
- nodeToInsert->data = data;
- if (mHead == nullptr) {
- mHead = nodeToInsert;
- length++;
- }
- else {
- Node *temp = mHead;
- while (temp->next != nullptr) {
- temp = temp->next;
- }
- temp->next = nodeToInsert;
- length++;
- }
- }
- float get(int pos) {
- Node *temp = mHead;
- int count = 0;
- while (count <= pos) {
- if (count == pos) {
- if (temp) {
- cout << "your Node is : " << temp->data << endl;
- return temp->data;
- }
- else {
- cout << "you are loser: " << endl;
- return 0;
- }
- }
- if (temp == nullptr) {
- cout << "you are loser: " << endl;
- return 0;
- }
- temp = temp->next;
- count++;
- }
- }
- void removeHead() {
- if (mHead == nullptr) {
- cout<< "Head is absent" << endl;
- }
- else {
- Node *temp = mHead;
- mHead = mHead->next;
- delete temp;
- length--;
- cout << "Head is removed" << endl;
- }
- }
- void removeTail() {
- cout << mHead << endl;
- if (mHead == nullptr) {
- cout << "there is no list here" << endl;
- }
- else {
- if (mHead->next) {
- removeHead();
- }
- else {
- if (mHead->next->next) {
- delete mHead->next;
- }
- }
- Node *temp = mHead;
- while (temp->next) {
- temp = temp->next;
- }
- delete temp;
- length--;
- }
- cout << "tail is deleted" << endl;
- }
- void deletePosition(int pos) {
- if (pos == 0) {
- cout << "there is no list here" << endl;
- }
- else {
- Node * temp = mHead;
- Node *temp1 = nullptr;
- for (int i = 0; i < pos - 2; i++) {
- temp = temp->next;
- }
- temp1 = temp->next;
- temp->next = temp1->next;
- length--;
- }
- }
- void print() const {
- int i = 1;
- Node * temp = mHead;
- while (temp != nullptr) {
- std::cout << i << ": " << temp->data << std::endl;
- temp = temp->next;
- i++;
- }
- }
- };
- int main(float argc, char const *argv[])
- {
- CMyLinkedList* list = new CMyLinkedList();
- float af = 11;
- float ah = 22;
- float an = 33;
- list->addToEnd(af);
- list->addToEnd(ah);
- list->addToEnd(an);
- //list->print();
- list->removeTail();
- //list->get(10);
- //list->deletePosition(2);
- list->print();
- //list->removeHead();
- //list->print();
- cout << "List Length: " << list->length << std::endl;
- delete list;
- int aaa;
- cin >> aaa;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement