Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Duplicates in list

Duplicates in list

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
21 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Chris Losinger

    if you don't want to sort:

    for each node, Cur
    for each node, Temp
    if Temp!=Cur
    if Temp.value==Cur.value, duplicate
    Temp=Temp.next
    Cur=Cur.next

    basically, for each node, you compare its value to every other node. it's inefficient, but it will work. you can also sort your list when you add: 1. walk the list, find the first node with a value greater than the new value. this is 'Cur' 2. change the next pointer of Cur->prev to point to your new node. 3. set your new node's prev to Cur->prev. now you're all linked in the prev direction. 4. change the prev pointer of Cur to point to your new node. 5. set your new node's cur to point to Cur. now you're all linked in the next direction. that way, the list is always sorted, and you can just walk it, looking for duplicates.

    image processing toolkits | batch image processing

    S Offline
    S Offline
    sadas232341s
    wrote on last edited by
    #12

    OK, I decided to sort the list, but can you tell me why is not working well?

    1 Reply Last reply
    0
    • S sadas232341s

      for(int i = 1; i < N; i++) for(int j = N - 1; j >= i; j--) if(p->key > p->next->key) { t = p->key; p->key = p->next->key; p->next->key = t; } Here is the sort but it doesn' t work

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #13

      first problem: you're never moving p

      image processing toolkits | batch image processing

      S 1 Reply Last reply
      0
      • C Chris Losinger

        first problem: you're never moving p

        image processing toolkits | batch image processing

        S Offline
        S Offline
        sadas232341s
        wrote on last edited by
        #14

        how to move it? p++

        C 1 Reply Last reply
        0
        • S sadas232341s

          how to move it? p++

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #15

          p = p->next; p = p->prev; (forgot you're inserting at the beginning, not at the end of list) if (p==NULL) bail.

          image processing toolkits | batch image processing

          S 1 Reply Last reply
          0
          • C Chris Losinger

            p = p->next; p = p->prev; (forgot you're inserting at the beginning, not at the end of list) if (p==NULL) bail.

            image processing toolkits | batch image processing

            S Offline
            S Offline
            sadas232341s
            wrote on last edited by
            #16

            OK, here is the full code that I' m working on. Can you tell me my mistakes exactly because I can' t understand where to put what. The UniqueList function is important, which now only shows the list and nothing more. The sort does not work...

            #include "iostream"

            using namespace std;

            void Add(int n);
            void UniqueList(int N);

            struct Elem
            {
            int key;
            Elem *prev;
            Elem *next;
            } *start;

            int main()
            {
            int num;
            int N = 0;

            start = NULL;
            
            while(cin >> num)
            {
            	Add(num);
            
            	N++;
            }
            
            cout << endl;
            
            UniqueList(N);
            
            return 0;
            

            }

            void Add(int n)
            {
            Elem *p = start;
            start = new Elem;

            start->key = n;
            start->next = p;
            start->prev = NULL;
            
            if(NULL != p) p->prev = start;
            

            }

            void UniqueList(int N)
            {
            Elem *p = start;
            int t;

            if(NULL != start) cout << "List:" << endl;
            else cout << "Empty list!";
            
            for(int i = 1; i < N; i++)
            	for(int j = N - 1; j >= i; j--)
            	{
            		if(p->key > p->next->key)
            		{
            			t = p->key;
            			p->key = p->next->key;
            			p->next->key = t;
            		}
            	}
            
            while(NULL != p)
            {
            	cout << p->key << endl;
            
            	p = p->next;
            }
            

            }

            S 1 Reply Last reply
            0
            • S sadas232341s

              OK, here is the full code that I' m working on. Can you tell me my mistakes exactly because I can' t understand where to put what. The UniqueList function is important, which now only shows the list and nothing more. The sort does not work...

              #include "iostream"

              using namespace std;

              void Add(int n);
              void UniqueList(int N);

              struct Elem
              {
              int key;
              Elem *prev;
              Elem *next;
              } *start;

              int main()
              {
              int num;
              int N = 0;

              start = NULL;
              
              while(cin >> num)
              {
              	Add(num);
              
              	N++;
              }
              
              cout << endl;
              
              UniqueList(N);
              
              return 0;
              

              }

              void Add(int n)
              {
              Elem *p = start;
              start = new Elem;

              start->key = n;
              start->next = p;
              start->prev = NULL;
              
              if(NULL != p) p->prev = start;
              

              }

              void UniqueList(int N)
              {
              Elem *p = start;
              int t;

              if(NULL != start) cout << "List:" << endl;
              else cout << "Empty list!";
              
              for(int i = 1; i < N; i++)
              	for(int j = N - 1; j >= i; j--)
              	{
              		if(p->key > p->next->key)
              		{
              			t = p->key;
              			p->key = p->next->key;
              			p->next->key = t;
              		}
              	}
              
              while(NULL != p)
              {
              	cout << p->key << endl;
              
              	p = p->next;
              }
              

              }

              S Offline
              S Offline
              sadas232341s
              wrote on last edited by
              #17

              Just the sort?

              C 1 Reply Last reply
              0
              • S sadas232341s

                Just the sort?

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #18

                maybe it's best to rethink this from the beginning. if your goal is to have a list with no duplicates, the easiest thing to do is to simply not add duplicates. so, every time, you go to add a new Elem, walk through your list and see if you already have an Elem with that key. if you do, don't add anything. to do that, you need a function that can walk through the list and look for an Elem with a given key:

                bool haveKey(Elem *p, int k)
                {
                while (p!=NULL)
                {
                if (p->key=k) return true;
                p = p->next;
                }
                return false;
                }

                just call that at the top of your Add. if it returns false, don't add anything.

                image processing toolkits | batch image processing

                S 1 Reply Last reply
                0
                • C Chris Losinger

                  maybe it's best to rethink this from the beginning. if your goal is to have a list with no duplicates, the easiest thing to do is to simply not add duplicates. so, every time, you go to add a new Elem, walk through your list and see if you already have an Elem with that key. if you do, don't add anything. to do that, you need a function that can walk through the list and look for an Elem with a given key:

                  bool haveKey(Elem *p, int k)
                  {
                  while (p!=NULL)
                  {
                  if (p->key=k) return true;
                  p = p->next;
                  }
                  return false;
                  }

                  just call that at the top of your Add. if it returns false, don't add anything.

                  image processing toolkits | batch image processing

                  S Offline
                  S Offline
                  sadas232341s
                  wrote on last edited by
                  #19

                  No, the assignment is to add duplicates, but when show the list to show it without them. I think the sort thing will do the job but can' t do it properly.

                  modified on Monday, April 18, 2011 4:57 PM

                  C 1 Reply Last reply
                  0
                  • S sadas232341s

                    No, the assignment is to add duplicates, but when show the list to show it without them. I think the sort thing will do the job but can' t do it properly.

                    modified on Monday, April 18, 2011 4:57 PM

                    C Offline
                    C Offline
                    Chris Losinger
                    wrote on last edited by
                    #20

                    ok, then i would recommend sorting the nodes on input. it is far more efficient than sorting the whole list, and it's actually fairly straightforward. (i outlined it above) unfortunately, i have to leave for the day. so.. good luck!

                    image processing toolkits | batch image processing

                    1 Reply Last reply
                    0
                    • S sadas232341s

                      it's getting messy. How to sort a list??? (Doubly linked)

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #21

                      TCPMem wrote:

                      How to sort a list???

                      Insert the items in their "sorted" position within the list.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups