Professional Documents
Culture Documents
Today Codes-1
Today Codes-1
Today Codes-1
h////////////////
#pragma once
class Node
{
public:
Node *next;
int key;
Node();
~Node();
};
///////////////////node.cpp////////////////
#include "pch.h"
#include "Node.h"
Node::Node()
{
}
Node::~Node()
{
}
///////////////////Linkedlist.h////////////////
#pragma once
#include "Node.h"
#include<iostream>
using namespace std;
class LinkedList
{
private:
Node* head;
Node* tail;
int size;
public:
LinkedList();
~LinkedList();
void addToFront(int val);
void addToEnd(int val);
void printList();
};
///////////////////Linkedlist.cpp////////////////
#include "pch.h"
#include "LinkedList.h"
LinkedList::LinkedList()
{
head = NULL;
tail = NULL;
size = 0;
}
LinkedList::~LinkedList()
{
}
if (head == NULL) {
head = temp;
tail = temp;
}
else {
temp->next = head;
head = temp;
}
size++;
}
void LinkedList::addToEnd(int val) {
Node* temp = new Node();
temp->key = val;
temp->next = NULL;
if (tail == NULL) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
tail=temp;
}
size++;
}
void LinkedList::printList() {
Node* current = head;
while (current != NULL) {
cout << current->key<<endl;
current = current->next;
}
///////////////////main////////////////
// ConsoleApplication3.cpp : This file contains the 'main' function. Program
execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include "LinkedList.h"
int main()
{
LinkedList list;
list.addToFront(10);
list.addToFront(23);
list.addToFront(45);
list.addToFront(85);
list.addToFront(95);
list.printList();
cout << "----------"<<endl;
list.addToEnd(1005);
list.printList();
}