Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

Insert at tail Link List


//Program to insert at tail in Link List using Classes.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <Windows.h>
using namespace std;

class linkList{
public:
void getData();
void insert(linkList **head, linkList **tail); // insert at tail
void printAll(linkList *head); // Display all records.
private:
char name[100];
int rollNo;
linkList *next;
};
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::getData(){
cout << "Enter Name: "; cin >> name;
cout << "Enter Roll Number: "; cin >> rollNo;
next = NULL;
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::insert(linkList **head, linkList **tail){ // insert at tail
if (*head == NULL && *tail == NULL){
*head = (this);
*tail = (this);
}
else{
(*tail)->next = this;
*tail = this;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::printAll(linkList *head){
for (linkList *temp = head; temp!=NULL; temp=temp->next){
cout << "Name: " << temp->name << endl
<<  "Roll Number: " << temp->rollNo << endl << endl;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
/////////////////////////////////////////////////// Main() //////////////////////////////////////
int main(){
linkList *pointerOfLinkList  = 0, *head = 0, *tail = 0;


while (true){
cout << "1. Insert Node\n"
<< "2. Print All\n"
<< "0. Exit\t";
int opt; cin >> opt;

switch(opt){
case 1:
pointerOfLinkList = new linkList;
pointerOfLinkList->getData();
pointerOfLinkList->insert(&head,&tail);
cout << "Inserted" << endl;
break;
case 2:
pointerOfLinkList->printAll(head);
break;
case 8:
break;
case 9:
break;
case 0:
return 0;
default:
cout << "Wrong Key pressed" << endl;
}
_getch();
system("cls");
}
return 0;
}

Basic Classes C++


#include <iostream>
using namespace std;

class a{
int x; //private
public:
int getX(){return x;}
void setX(int a){
x = a;
}
};

int main(){
a obj;
obj.setX(10);//Functions are called over objects.
cout << "x = " << obj.getX() << endl;
return 0;
}

Another way

//-----------------------------Seperating in 3 files-----------------------------------------------------------------

//test.h
class a{
int x; //private
public:
int getX(){return x;}
void setX(int a){
x = a;
}

void copyConstructor(a &obj);
};
//-----------------------------------

//test.cpp
#include "test.h"

void a::copyConstructor(a &obj){
x  =  obj.getX();
}
//----------------------------------
//Main.cpp
#include <iostream>
#include "test.h"
using namespace std;

int main(){
a obj;
obj.setX(10);//Functions are called over objects.
cout << "x = " << obj.getX() << endl;
a obj1;
obj1.copyConstructor(obj);
cout << "obj1 = " << obj1.getX() << endl;
return 0;
}



Basic program Classes C++

Basic program Classes C++.

#include <iostream>
using namespace std;

class a{
public:
void set(int x){num = x;}
int get(){return num;}
private:
int num;
};

int main(){
a b;
b.set(45);
cout << b.get();
return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."