0% found this document useful (0 votes)
12 views

Linked List

The document discusses linked lists and various operations that can be performed on them including: 1. Removing duplicate nodes by traversing the list and skipping nodes with duplicate data. 2. Removing the head node by updating the head pointer to point to the next node. 3. Counting duplicate nodes by traversing the list and incrementing a counter when duplicate data is found. 4. Inserting a node at the end of a linked list by traversing to the tail and updating the next pointer.

Uploaded by

HuZaM Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Linked List

The document discusses linked lists and various operations that can be performed on them including: 1. Removing duplicate nodes by traversing the list and skipping nodes with duplicate data. 2. Removing the head node by updating the head pointer to point to the next node. 3. Counting duplicate nodes by traversing the list and incrementing a counter when duplicate data is found. 4. Inserting a node at the end of a linked list by traversing to the tail and updating the next pointer.

Uploaded by

HuZaM Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Linked List

void removeDuplicates1(struct Node* start)


{
struct Node *ptr1, *ptr2, *dup, *next;
ptr1 = start;
//Agr list empty ha to control return kr do
if (ptr1 == NULL){
return;
}
//nested loop for all linklist like sorting method
while (ptr1 != NULL) {
ptr2 = ptr1;

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

//nested loop for all linklist like sorting method


while (head->link!=NULL) {
Node *ptr= head->link;

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

Extra complete Question just for understanding


// C++ Program to count duplicates in linked list and
delete head node
#include<iostream>
#include<conio.h>
using namespace std;
class Node
{
public:
int data;
Node* link;
};

int countDuplicate(Node* head)


{
//struct Node *ptr1, *ptr2, *dup, *next;
//ptr1 = start;
int num=0;

//nested loop for all linklist like sorting method


while (head->link!=NULL) {
Node *ptr= head->link;

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

/*Remove head node from linked list*/


void removeFromHead(Node* &head)
{
Node *p = head;
head = head->link;
delete p;
}

/* Function to print nodes in a given linked list */


void printList(Node *node)
{
while (node!=NULL)
{
cout<<" "<<node->data;
node = node->link;
}
cout<<endl;
}

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

cout<<"Linked list Before Remove Head Node : ";


printList(head);

/* Remove duplicates from linked list */


cout<<"Total duplicate in Linked List :
"<<countDuplicate(head);

removeFromHead(head);

cout<<"\nLinked list After Remove Head Node : ";


printList(head);

//cout<<"\nLinked list after duplicate removal ";


//printList(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>

using namespace std;

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

You might also like