📅  最后修改于: 2020-10-15 06:22:31             🧑  作者: Mango
在此程序中,我们将创建一个循环链接列表,并从列表中删除重复的节点。我们将把一个节点与列表的其余部分进行比较,并检查是否重复。如果找到重复节点,请从列表中删除重复节点。
1-> 2-> 2-> 4-> 3
在上面的列表中,我们可以看到,节点2在列表中存在两次。因此,我们将有一个节点电流将遍历列表。索引将指向当前的下一个节点。 Temp将指向索引之前的节点。找到重复项后,我们将temp.next指向index.next来删除它。删除重复项后的上述列表:
1-> 2-> 4-> 3
#Represents the node of list.
#Represents the node of list.
class Node:
def __init__(self,data):
self.data = data;
self.next = None;
class CreateList:
#Declaring head and tail pointer as null.
def __init__(self):
self.head = Node(None);
self.tail = Node(None);
self.head.next = self.tail;
self.tail.next = self.head;
#This function will add the new node at the end of the list.
def add(self,data):
newNode = Node(data);
#Checks if the list is empty.
if self.head.data is None:
#If list is empty, both head and tail would point to new node.
self.head = newNode;
self.tail = newNode;
newNode.next = self.head;
else:
#tail will point to new node.
self.tail.next = newNode;
#New node will become new tail.
self.tail = newNode;
#Since, it is circular linked list tail will point to head.
self.tail.next = self.head;
#Removes duplicate from the list
def removeDuplicate(self):
#Current will point to head
current = self.head;
if(self.head == None):
print("List is empty");
else:
while(True):
#Temp will point to previous node of index.
temp = current;
#Index will point to node next to current
index = current.next;
while(index != self.head):
#If current node is equal to index data
if(current.data == index.data):
#Here, index node is pointing to the node which is duplicate of current node
#Skips the duplicate node by pointing to next node
temp.next = index.next;
else:
#Temp will point to previous node of index.
temp = index;
index= index.next;
current =current.next;
if(current.next == self.head):
break;
#Displays all the nodes in the list
def display(self):
current = self.head;
if self.head is None:
print("List is empty");
return;
else:
#Prints each node by incrementing pointer.
print(current.data);
while(current.next != self.head):
current = current.next;
print(current.data);
print("\n");
class CircularLinkedList:
cl = CreateList();
#Adds data to the list
cl.add(1);
cl.add(2);
cl.add(3);
cl.add(2);
cl.add(2);
cl.add(4);
print("Originals list: ");
cl.display();
#Removes duplicate nodes
cl.removeDuplicate();
print("List after removing duplicates: ");
cl.display();
输出:
Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
#include
#include
#include
//Represents the node of list.
struct node{
int data;
struct node *next;
};
//Declaring head and tail pointer as null.
struct node *head = NULL;
struct node *tail = NULL;
//This function will add the new node at the end of the list.
void add(int data){
//Create new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
//Checks if the list is empty.
if(head == NULL){
//If list is empty, both head and tail would point to new node.
head = newNode;
tail = newNode;
newNode->next = head;
}else {
//tail will point to new node.
tail->next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will point to head.
tail->next = head;
}
}
//Removes duplicate from the list
void removeDuplicate() {
//Current will point to head
struct node *current = head, *index = NULL, *temp = NULL;
if(head == NULL) {
printf("List is empty");
}
else {
do{
//Temp will point to previous node of index.
temp = current;
//Index will point to node next to current
index = current->next;
while(index != head) {
//If current node is equal to index data
if(current->data == index->data) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
temp->next = index->next;
}
else{
//Temp will point to previous node of index.
temp = index;
}
index= index->next;
}
current =current->next;
}while(current->next != head);
}
}
//This function will display the nodes of circular linked list
void display(){
struct node *current = head;
if(head == NULL){
printf("List is empty");
}
else{
do{
//Prints each node by incrementing pointer.
printf("%d ", current->data);
current = current->next;
}while(current != head);
printf("\n");
}
}
int main()
{
//Adds data to the list
add(1);
add(2);
add(3);
add(2);
add(2);
add(4);
printf("Originals list: \n");
display();
//Removes duplicate nodes
removeDuplicate();
printf("List after removing duplicates: \n");
display();
return 0;
}
输出:
Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
public class RemoveDuplicate {
//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
//Declaring head and tail pointer as null.
public Node head = null;
public Node tail = null;
//This function will add the new node at the end of the list.
public void add(int data){
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
if(head == null) {
//If list is empty, both head and tail would point to new node.
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will points to head.
tail.next = head;
}
}
//Removes duplicate from the list
public void removeDuplicate() {
//Current will point to head
Node current = head, index = null, temp = null;
if(head == null) {
System.out.println("List is empty");
}
else {
do{
//Temp will point to previous node of index.
temp = current;
//Index will point to node next to current
index = current.next;
while(index != head) {
//If current node is equal to index data
if(current.data == index.data) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
temp.next = index.next;
}
else {
//Temp will point to previous node of index.
temp = index;
}
index= index.next;
}
current =current.next;
}while(current.next != head);
}
}
//Displays all the nodes in the list
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
}
else {
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.data);
current = current.next;
}while(current != head);
System.out.println();
}
}
public static void main(String[] args) {
RemoveDuplicate cl = new RemoveDuplicate();
//Adds data to the list
cl.add(1);
cl.add(2);
cl.add(3);
cl.add(2);
cl.add(2);
cl.add(4);
System.out.println("Originals list: ");
cl.display();
//Removes duplicate nodes
cl.removeDuplicate();
System.out.println("List after removing duplicates: ");
cl.display();
}
}
输出:
Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
using System;
namespace CircularLinkedList
{
public class Program
{
//Represents the node of list.
public class Node{
public T data;
public Node next;
public Node(T value) {
data = value;
next = null;
}
}
public class CreateList{
protected Node head = null;
protected Node tail = null;
//This function will add the new node at the end of the list.
public void add(T data){
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
if(head == null){
head = newNode;
tail = newNode;
newNode.next = head;
}else{
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will point to head.
tail.next = head;
}
}
//Removes duplicate from the list
public void removeDuplicate() {
//Current will point to head
Node current = head, index = null, temp = null;
if(head == null) {
Console.WriteLine("List is empty");
}
else {
do{
//Temp will point to previous node of index.
temp = current;
//Index will point to node next to current
index = current.next;
while(index != head) {
//If current node is equal to index data
if(current.data.Equals(index.data)) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
temp.next = index.next;
}
else{
//Temp will point to previous node of index.
temp = index;
}
index= index.next;
}
current =current.next;
}while(current.next != head);
}
}
//Displays all the nodes in the list
public void display(){
Node current = head;
if(head == null){
Console.WriteLine("List is empty");
}
else{
do{
//Prints each node by incrementing pointer.
Console.Write(" "+ current.data);
current = current.next;
}while(current != head);
Console.WriteLine();
}
}
}
public static void Main()
{
CreateList cl = new CreateList();
//Adds data to the list
cl.add(1);
cl.add(2);
cl.add(3);
cl.add(2);
cl.add(2);
cl.add(4);
Console.WriteLine("Originals list: ");
cl.display();
//Removes duplicate nodes
cl.removeDuplicate();
Console.WriteLine("List after removing duplicates: ");
cl.display();
}
}
}
输出:
Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4
data = $data;
$this->next = NULL;
}
}
class CreateList{
//Declaring head and tail pointer as null.
private $head;
private $tail;
function __construct(){
$this->head = NULL;
$this->tail = NULL;
}
//This function will add the new node at the end of the list.
function add($data){
//Create new node
$newNode = new Node($data);
//Checks if the list is empty.
if($this->head == NULL){
//If list is empty, both head and tail would point to new node.
$this->head = $newNode;
$this->tail = $newNode;
$newNode->next = $this->head;
}
else{
//tail will point to new node.
$this->tail->next = $newNode;
//New node will become new tail.
$this->tail = $newNode;
//Since, it is circular linked list tail will point to head.
$this->tail->next = $this->head;
}
}
//Removes duplicate from the list
function removeDuplicate() {
//Current will point to head
$current = $this->head;
if($this->head == NULL) {
echo "List is empty";
}
else {
do{
//Temp will point to previous node of index.
$temp = $current;
//Index will point to node next to current
$index = $current->next;
while($index != $this->head) {
//If current node is equal to index data
if($current->data == $index->data) {
//Here, index node is pointing to the node which is duplicate of current node
//Skips the duplicate node by pointing to next node
$temp->next = $index->next;
}
else {
//Temp will point to previous node of index.
$temp = $index;
}
$index= $index->next;
}
$current = $current->next;
}while($current->next != $this->head);
}
}
//Displays all the nodes in the list
function display() {
$current = $this->head;
if($this->head == NULL) {
echo "List is empty";
}
else {
do{
//Prints each node by incrementing pointer.
echo(" $current->data");
$current = $current->next;
}while($current != $this->head);
echo "
";
}
}
}
$cl = new CreateList();
//Adds data to the list
$cl->add(1);
$cl->add(2);
$cl->add(3);
$cl->add(2);
$cl->add(2);
$cl->add(4);
echo "Originals list:
";
$cl->display();
//Removes duplicate nodes
$cl->removeDuplicate();
echo "List after removing duplicates:
";
$cl->display();
?>
输出:
Originals list:
1 2 3 2 2 4
List after removing duplicates:
1 2 3 4