我試圖只列印一次鏈接串列中所有重復項,但我無法列印最后一個。
我還嘗試將最后一個重復的字串存盤ptr1->name在一個臨時變數中并在while回圈結束后列印它,但它不適用于不同的值對,只有最后一個,我也找不到列印最后一個重復位置的方法細繩。
如果沒有更好的方法來列印最后一個重復的字串,我怎么能只忽略第一次出現?
例子:
串列 :
1 7001 Sanjana
2 7014 Aishwarya
3 7025 Ranjit
4 7017 Gurudas
5 7101 Deeksha
6 7023 Indu
7 7017 Gurudas
8 7001 Sanjana
9 7017 Gurudas
10 7016 Kiran
11 7020 Rajni
12 7020 Rajni
重復功能輸出(當前)
1 7001 Sanjana
4 7017 古魯達
7 7017 古魯達
11 7020 拉吉尼
所需輸出:
1 7001 Sanjana
4 7017 Gurudas
7 7017 Gurudas
8 7001 Sanjana
9 7017 Gurudas
11 7020 Rajni
12 7020 Rajni
要么
7 7017 古魯達
8 7001 Sanjana
9 7017 古魯達
12 7020 拉吉尼
這是我寫的函式:
int data;
char name[20];
struct node* link;
};
// function
void duplicate(void){
int count = 0;
int loc = 0; // Location in the list.
struct node * ptr1, * ptr2;
ptr1 = root; // root is global pointer variable.
while (ptr1->link!=NULL){
ptr2 = ptr1->link;
loc ;
while(ptr2!=NULL){
if ((ptr1->num == ptr2->num) && !(strcmpi(ptr1->name, ptr2->name))){
count ;
printf(" %d--> d %s\n", loc, ptr2->num, ptr2->name);
break;
}
ptr2 = ptr2->link;
}
ptr1 = ptr1->link;
}
if (!count) {
printf("No duplicates found in the list!\n");
}else{
printf("\nTotal (%d) duplicates.\n", count);
}
}
----------
Edit:
Found Solution with help of @peal-mazumder
void duplicate(void){
int count = 0;
int loc1 = 0;
int loc2 = 0;
struct node * ptr1, * ptr2;
bool vis[5000] = {false};
ptr1 = root;
while (ptr1!=NULL){
loc1 ;
if (vis[loc1]) {
ptr1 = ptr1->link;
continue;
}
ptr2 = ptr1->link;
loc2 = loc1;
while(ptr2!=NULL){
loc2 ;
if ((ptr1->num == ptr2->num) && !(strcmpi(ptr1->name, ptr2->name))){
count ;
printf(" %d--> d %s\n", loc2, ptr2->num, ptr2->name);
vis[loc2] = true;
}
ptr2 = ptr2->link;
}
ptr1 = ptr1->link;
}
if (!count) {
printf("No duplicates found in the list!\n");
}else{
printf("\nTotal (%d) duplicates.\n", count);
}
}
uj5u.com熱心網友回復:
8 --> 7001 Sanjana
9 --> 7017 Gurudas
12 --> 7020 Rajni
Total (3) duplicates.
如果您嘗試列印類似的內容,請檢查下面的代碼,否則在您的描述中為給定的示例案例添加所需的輸出。
struct node {
int num;
char name[20];
struct node* link;
} * root;
// function
void duplicate(void){
int count = 0;
int loc = 0; // Location in the list.
struct node * ptr1, * ptr2;
bool vis[200000] = {false};
ptr1 = root; // root is global pointer variable.
while (ptr1->link!=NULL) {
loc ;
if(vis[ptr1->num]) { // if i already processed for same num value then we don't need to check duplicate for this again.
ptr1 = ptr1->link;
continue;
}
vis[ptr1->num] = true;
ptr2 = ptr1->link;
int Ptr2Location = loc 1, lastDupLocation = 0;
struct node *temp; // to store last duplicate node
while(ptr2 != NULL) {
if ((ptr1->num == ptr2->num) && !(strcmpi(ptr1->name, ptr2->name))){
temp = ptr2;
lastDupLocation = Ptr2Location;
}
Ptr2Location ;
ptr2 = ptr2->link;
}
if(lastDupLocation)
printf(" %d --> d %s\n", lastDupLocation, temp->num, temp->name), count ;
ptr1 = ptr1->link;
}
if (!count) {
printf("No duplicates found in the list!\n");
}else {
printf("\nTotal (%d) duplicates.\n", count);
}
}
void insert(int num, char str[20], int len)
{
if(root == NULL) { //If the list is empty
root = new node();
root->num = num;
strcpy(root->name, str);
root->link = NULL;
}
else{
node* current_node = root; //make a copy of root node
while(current_node->link!=NULL) //Find the last node
{
current_node=current_node->link; //go to next address
}
node *newnode = new node(); //create a new node
newnode->num = num;
strcpy(newnode->name, str);
newnode->link = NULL;
current_node->link=newnode; //link the last node with new node
}
}
int main()
{
int num, n, i;
cin>>n;
char name[20];
for (i = 0; i<n; i )
{
scanf("%d %s", &num, name);
insert(num, name, strlen(name));
}
duplicate();
return 0;
}
您可以通過將這些值存盤在資料結構或結構中來根據它們的位置對其進行排序。
1 --> 7001 Sanjana
8 --> 7001 Sanjana
4 --> 7017 Gurudas
7 --> 7017 Gurudas
9 --> 7017 Gurudas
11 --> 7020 Rajni
12 --> 7020 Rajni
代碼
void duplicate(void){
int count = 0;
int loc = 0; // Location in the list.
struct node * ptr1, * ptr2;
bool vis[200000] = {false};
ptr1 = root; // root is global pointer variable.
while (ptr1->link!=NULL) {
loc ;
if(vis[ptr1->num]) { // if i already processed for same num value then we don't need to check duplicate for this again.
ptr1 = ptr1->link;
continue;
}
ptr2 = ptr1->link;
int Ptr2Location = loc 1;
while(ptr2 != NULL) {
if ((ptr1->num == ptr2->num) && !(strcmpi(ptr1->name, ptr2->name))){
if(!vis[ptr1->num])
printf(" %d --> d %s\n", loc, ptr1->num, ptr1->name), count ;
printf(" %d --> d %s\n", Ptr2Location, ptr2->num, ptr2->name);
vis[ptr1->num] = true;
}
Ptr2Location ;
ptr2 = ptr2->link;
}
ptr1 = ptr1->link;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/442977.html
