日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言i++和++i程序_使用C ++程序从链接列表中消除重复项

發(fā)布時(shí)間:2025/3/11 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言i++和++i程序_使用C ++程序从链接列表中消除重复项 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c語言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 7

Description:

描述:

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è)問題中,我們得到一個(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->NULL

Solution Explanation:

解決方案說明:

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è)問題,我們使臨時(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語言i++和++i程序

總結(jié)

以上是生活随笔為你收集整理的c语言i++和++i程序_使用C ++程序从链接列表中消除重复项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。