Pages

Saturday, 5 September 2015

C++ Program to insert a new nod and Value in Doubly Link List


#include <iostream>
using namespace std;

struct nod
{
int data;
nod *next;
nod *prev;
};

class myClass
{
private:
nod *head;
nod *temp;

public:
myClass()
{
head = NULL;
temp = NULL;
}

void insert();
void print();
};



void myClass :: insert()
{

if (head == NULL)
{
head = new nod;
cout << " Value: ";
cin >> head->data;
head->next = NULL;
head->prev = NULL;
}
else
{
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}

temp->next = new nod;
cout << " Value: ";
cin >> temp->next->data;
temp->next->next = NULL;
temp->next->prev = temp;
temp = temp->next;
}

}

void myClass :: print()
{
cout << endl;
temp = head;
while (temp != NULL)
{
cout << temp->data << endl;
temp= temp->next;
}

system("pause");

}


int main ()
{
myClass m;
int choose;

A:
system("cls");
cout << endl;
cout << " <1> Insert new value" << endl;
cout << endl;
cout << " <2> Print" << endl;
cout << endl;

cout << " Choose: ";
cin >> choose;

if (choose == 1)
{
system("cls");
m.insert();
goto A;
}

if (choose == 2)
{  
system("cls");
m.print();
goto A;
}


}

For any query email us : mohsin.mahmood@hotmail.co.uk

No comments: