Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 797803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:52:00+00:00 2026-05-14T22:52:00+00:00

My question is about removing duplicates from a linked list. But i want to

  • 0

My question is about removing duplicates from a linked list. But i want to do it before adding to linked list.

struct myStr{int number; mystr *next;}
void append(mystr **q,int item)
{
myStr *temp;
temp = *q;
myStr *newone;
if(*q==NULL)// There should be control of previous elements. Call of keysearch function.
     {   temp = (myStr *)malloc(sizeof(myStr));

          temp->number =size;
          temp->next=NULL;
          *q=temp;
     }
     else //And also here
     {  temp = *q;
         while(temp->next !=NULL)
         {  temp=temp->next;
         }
         newone = (myStr *)malloc(sizeof(myStr));
         newone->count = size;
         newone->next=NULL;
         temp->next=newone;

     }
}
int keysearch (myStr *p)
{
struct myStr *temp = p;
int found = 0;
int key= p->number;
while (temp->next != NULL) 
 {
 if(temp->number == key)
    {
   return 1;
//break;
        }
     temp = temp->next;   
     }
    return 0;
    }

My problem is in keySearch. I don’t know what is wrong? Or is there another way for doing this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:52:01+00:00Added an answer on May 14, 2026 at 10:52 pm

    In your code, you have 2 comments where you expect to be invoking keySearch. In fact, you only need it in 1 place – the second comment. This is because the first place is where you are creating a brand new list, so of course nothing will be in it that you need to worry about.

    In the second case, you want to call this keySearch method. There are 3 types of keySearch methods I can think of that would be useful:

    1. Invoke a method int keySearch(mystr *p, int value) that looks through p for value and, if found, returns true (non-zero number)
    2. Invoke a method int keySearch(mystr *p) that looks through p for any duplicates and removes them. This would not be called on every append, though, and the implementation above suggests this is not what you are trying to do. It’s a lot more work to do this, in any event.
    3. Invoke a method int keySearch(mystr *p) that looks through p to see if the first value in q is duplicated, returning true if it is. It seems this is what your method is trying to do.

    Based on the signature of the method, you are trying to do #2 or #3. But both of those are wrong-headed – they assume you’ve already added the duplicate to the list. What you should be doing instead is trying to prevent a duplicate from being added in the first place. The method as it is does not have enough information to do that, though. It needs the value of the element that has not yet been added.

    I would suggest changing the method to the one in #1, and passing in the value you want to add. If it is found, return 1. In the append method, if this function evaluates to 1, don’t append anything. Otherwise append the new element.

    int keysearch(struct myStr *p, int value)
    {
       if(p == NULL) return 0;
       // reusing p is fine - it's a local variable
       while(p != NULL)
       {
          if(p->number == value) return 1;  // return if value exists already
          p = p->next;                      // go to next element
       }
       return 0;
    }
    

    Now, when you are about to add an element, first run it by this method. If the method returns 1, leave your append method immediately – there is nothing to be done. If it returns 0, then malloc your new node and set it to the end of the list.


    EDIT: An enterprising codesmith may want to optimize slightly so that on every append we don’t do 2 loops through the whole list (one for keySearch, and then one to find the last element for actual appending). You can do this by modifying keySearch slightly…

    // returns NULL if p is empty / value exists; otherwise returns the last element
    struct myStr *keysearch(struct myStr *p, int value)
    {
       // same logic, different return values; integration into append changes too!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about removing non-alphanumeric characters from a list in R. I
I've seen a number of questions about removing HTML tags from strings, but I'm
This question is about removing sequences from an array, not duplicates in the strict
Several days ago I had a question about removing index.php from the address bar,
I have question about removing element from QList. myclass.h: class node2D : public QObject
A question was asked recently about removing SourceSafe integration from Visual Studio 6 .
A question about inheritance in java... class Base { private int val = 10;
I have a question regarding removing duplicates after sorting within a tuple in R.
I have a rather specific question about how to exclude items of one list
Quick question: What's the best way to go about removing <br> and <br />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.