c语言i++和++i程序_使用C ++程序从链接列表中消除重复项
c語(yǔ)言i++和++i程序
Given a sorted linked list (elements are sorted in ascending order). Eliminate duplicates from the given LL, such that output LL contains only unique elements.
給定一個(gè)排序的鏈表(元素按升序排序)。 從給定的LL中消除重復(fù)項(xiàng),以便輸出LL僅包含唯一元素。
Input format: Linked list elements (separated by space and terminated by 1)
輸入格式:鏈接列表元素(以空格分隔并以1終止)
Sample Input 1 :1 2 3 3 3 4 4 5 5 5 7 -1Sample Output 1 :1 2 3 4 5 7Description:
描述:
In this question, we are given a sorted linked list with duplicate elements in it. Our task is to remove the duplicate nodes of the linked list. Since the list is sorted, the duplicate elements will be present in consecutive orders, that makes the work a lot easier.
在這個(gè)問(wèn)題中,我們得到一個(gè)包含重復(fù)元素的排序鏈表。 我們的任務(wù)是刪除鏈表的重復(fù)節(jié)點(diǎn)。 由于列表已排序,因此重復(fù)的元素將以連續(xù)的順序顯示,這使工作變得更加容易。
Example:
例:
Lets the list be:1->2->3->3->4->4->4->NULLThe modified list will be:1->2->3->4->NULLSolution Explanation:
解決方案說(shuō)明:
To solve this problem, we keep the temp pointer pointing to node. While(temp -> next != NULL), we check if the data of temp is equal to data of temp->next. If they are equal, we will point the temp-> next to temp -> next ->next.
Original LL: 3-> 3-> 4-> ...
After Change: 3-> 4-> ... Leaving out the middle 3->
為了解決這個(gè)問(wèn)題,我們使臨時(shí)指針指向節(jié)點(diǎn)。 while(temp-> next!= NULL)時(shí),我們檢查temp的數(shù)據(jù)是否等于temp-> next的數(shù)據(jù)。 如果它們相等,則將temp->指向temp-> next-> next。
原始LL:3-> 3-> 4-> ...
更改后:3-> 4-> ...省略中間3->
This will keep on until we reached the end of the list and return head at the end.
這將一直持續(xù)到我們到達(dá)列表的末尾并返回末尾。
Steps
腳步
1. 1->2->3->3->4->4->4->NULL, temp = 12. 1->2->3->3->4->4->4->NULL, temp = 23. 1->2->3->3->4->4->4->NULL, temp = 34. 1->2->3->4->4->4->NULL, temp = 45. 1->2->3->4->4->NULL, temp = 4Finally,1->2->3->4->NULL .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Function:
功能:
Node *removeDuplicate(Node* head){//temp pointing to headNode *temp = head; while(temp->next != NULL && temp != NULL){//Duplicate Foundif(temp->data == temp->next->data){ //DUplicate Removedtemp -> next = temp ->next ->next; }else{//No Duplicate Presenttemp = temp->next; }}//Return Headreturn head; }C++ Code:
C ++代碼:
#include<bits/stdc++.h> using namespace std;struct Node{// linked list Nodeint data;Node * next; };Node *newNode(int k){ //defining new nodeNode *temp = (Node*)malloc(sizeof(Node)); temp->data = k; temp->next = NULL; return temp; }//Used to add new node at the end of the list Node *addNode(Node* head, int k){if(head == NULL){head = newNode(k);}else{Node * temp = head;Node * node = newNode(k);while(temp->next!= NULL){temp = temp->next;}temp-> next = node;}return head; }// Used to create new linked list and return head Node *createNewLL(){int cont = 1;int data;Node* head = NULL;while(cont){cout<<"Enter the data of the Node"<<endl;cin>>data;head = addNode(head,data);cout<<"Do you want to continue?(0/1)"<<endl;cin>>cont;}return head; }//To print the Linked List void *printLL(Node * head){while(head!= NULL){cout<<head->data<<"->";head = head-> next;}cout<<"NULL"<<endl; }//Function Node *removeDuplicate(Node* head){//temp pointing to headNode *temp = head;while(temp->next != NULL && temp != NULL){//Duplicate Foundif(temp->data == temp->next->data){//DUplicate Removedtemp -> next = temp ->next ->next;}else{//No Duplicate Presenttemp = temp->next;}}//Return Headreturn head; }//Driver Main int main(){Node * head = createNewLL();cout<<"The linked list is"<<endl;printLL(head);head = removeDuplicate(head);cout<<"The new Linked List is" <<endl;printLL(head);return 0; } .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Output
輸出量
Enter the data of the Node 1 Do you want to continue?(0/1) 1 Enter the data of the Node 1 Do you want to continue?(0/1) 1 Enter the data of the Node 2 Do you want to continue?(0/1) 1 Enter the data of the Node 2 Do you want to continue?(0/1) 1 Enter the data of the Node 3 Do you want to continue?(0/1) 1 Enter the data of the Node 4 Do you want to continue?(0/1) 1 Enter the data of the Node 5 Do you want to continue?(0/1) 1 Enter the data of the Node 5 Do you want to continue?(0/1) 1 Enter the data of the Node 5 Do you want to continue?(0/1) 0 The linked list is 1->1->2->2->3->4->5->5->5->NULL The new Linked List is 1->2->3->4->5->NULL翻譯自: https://www.includehelp.com/cpp-programs/eliminate-duplicates-from-linked-list.aspx
c語(yǔ)言i++和++i程序
總結(jié)
以上是生活随笔為你收集整理的c语言i++和++i程序_使用C ++程序从链接列表中消除重复项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在Java中使用Collator和S
- 下一篇: JAVA Opencv在图片上添加中文