Linked List
Linked List
//compare
while (ptr2->link != NULL) {
//If duplicate then delete it
if (ptr1->data == ptr2->link->data) {
ptr2->link = ptr2->link->link;
else
ptr2 = ptr2->link;
}
ptr1 = ptr1->link;
}
}
void removeFromHead()
{
Node *p = head;
head = head->link;
delete p;
}
int countDuplicate(int key)
{
int num=0;
//compare
while (ptr != NULL) {
//If duplicate then count it
if (head->data==ptr->data) {
num++;
}
ptr = ptr->link;
}
head = head->link;
}
return num;
}
void CDLinkedList::insertAtEnd(int val){
Node *p,*q;
p=head;
p=p->prev;
p->data=val;
}
//compare
while (ptr != NULL) {
//If duplicate then delete it
if (head->data==ptr->data) {
num++;
// ptr2->link = ptr2->link->link;
ptr = ptr->link;
}
head = head->link;
}
//cout<<"Total duplicate is"<<num<<endl;
return num;
}
int main()
{
Node * p=NULL, * q= NULL, * head= NULL;
char ch;
p = new Node;
cout << "Enter the data: ";
cin >> p->data;
p->link = NULL;
head = p;
cout << "do you want to create another node: ";
ch=getch();
while (ch == 'y' || ch == 'Y') {
q = new Node;
cout << "Enter the data: " << endl;
cin >> q->data;
q->link = NULL;
p->link = q;
p = q;
cout << "Enter choice: " << endl;
ch=getch();
}
removeFromHead(head);
return 0;
}
Q#2:
//WRITE THE CODE TO INSERT NODES IN LINKLIST AT
FIRST, LAST AND RANDOM POSTION
#include<iostream>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int data;
Node *link;
};
class Linklist
{
private:
Node *p,*q,*head;
public:
Linklist()
{
head = NULL;
}
void insert_AT_first(Node* &head)
{
cout<<" \n We create a new linklist before
the first node";
q=new Node;
cout<<"\nEnter the data in the new node:";
cin>>q->data;
q->link=head;
head=q;
}
void insert_AT_last(Node *head)
{
cout<<"\n We inset the new linklist at the
end of node orn linklist:";
q=new Node;
cout<<"\nEnter the data in the new node:";
cin>>q->data;
q->link=NULL;
p=head;
while(p->link!=NULL)
{
p=p->link;
}
p->link=q;
}
void insert_AT_random(Node *head)
{
int n;
p=new Node;
cout<<"\n Enter the data : ";
cin>> p->data;
cout<<"\n Enter the node number : ";
cin>>n;
q=head;
for(int i=1;i<n;i++)
{
q=q->link;
}
p->link=q->link;
q->link=p;
}
void remove(Node *head,int key)
{
p=head;
while(p!=NULL)
{
if(p->link->data==key)
{
p->link=p->link->link;
return;
}
p=p->link;
}
}
void display(Node *head)
{
p=head;
while(p!=NULL)
{
cout<<"\n The data U entered : ";
cout<<p->data;
cout<<endl;
p=p->link;
}
}
};
int main()
{
Node *p,*q,*head;
char ch;
Linklist obj;
p = new Node ;
cout<<"\n Enter the Data : ";
cin>> p->data ;
p->link = NULL;
head=p ;
cout<<"\n Do you want to create new linked list then
enter Y or y : ";
ch=getche();
while(ch=='Y' || ch=='y')
{
q = new Node;
cout<<"\n Enter the Data : ";
cin>> q->data;
q->link = NULL;
p->link = q;
p=q;
cout<<"\n Do you want to create new linked list
then enter Y or y : ";
ch=getche();
}
while(1)
{
cout<<"\n F for insert at first.";
cout<<"\n L for insert at last.";
cout<<"\n R for insert at Random.";
cout<<"\n D for display.";
cout<<"\n A for Remove Node which is equal to
given Value.";
cout<<"\n E for exit.";
cout<<"\n Enter you choice.";
ch=getche();
switch(ch)
{
case 'F':
obj.insert_AT_first(head);
break;
case 'L':
obj.insert_AT_last(head);
break;
case 'R':
obj.insert_AT_random(head);
break;
case 'D':
obj.display(head);
break;
case 'A':
int val;
cout<<"Enter Value";
cin>>val;
obj.remove(head,val);
break;
case 'E':
exit(0);
default:
cout<<"\n U enter wrong choice plz try
again.";
}
}
return 0;
}