Today Codes-1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

///////////////////node.

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()
{
}

void LinkedList::addToFront(int val) {


Node* temp = new Node();
temp->key = val;
temp->next = NULL;

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();
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu


// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:


// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add
Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and
select the .sln file

You might also like