Need a project done?

C++ Programming Developer

Search This Blog

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

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."