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.
  • S sadas232341s

    and what about the other main problem with the duplicates?

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

    i'm going to guess that that's probably a side effect of: a) not setting prev to anything but NULL and b) not initializing 'start' the first time around. and, maybe i don't understand the task, but why are you doing this:

    if(p->prev != p->next)

    why compare prev and next pointers ? and why compare prev and next and not p itself:

    if(p->key != p->next->key)

    image processing toolkits | batch image processing

    S 1 Reply Last reply
    0
    • C Chris Losinger

      i'm going to guess that that's probably a side effect of: a) not setting prev to anything but NULL and b) not initializing 'start' the first time around. and, maybe i don't understand the task, but why are you doing this:

      if(p->prev != p->next)

      why compare prev and next pointers ? and why compare prev and next and not p itself:

      if(p->key != p->next->key)

      image processing toolkits | batch image processing

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

      I fixed the prev problem. "if(p->prev != p->next)" is a shot in the dark. Any ideas how to show only uniques :)

      C 1 Reply Last reply
      0
      • S sadas232341s

        I fixed the prev problem. "if(p->prev != p->next)" is a shot in the dark. Any ideas how to show only uniques :)

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

        you're on the right track. but the key thing that's missing is that the list needs to be sorted before you can be guaranteed that duplicates will show up in p and p->next. duplicates, but p, p->next will never be the same: 1,4,3,5,3 this will work: 1,3,3,4,5

        image processing toolkits | batch image processing

        S 2 Replies Last reply
        0
        • C Chris Losinger

          you're on the right track. but the key thing that's missing is that the list needs to be sorted before you can be guaranteed that duplicates will show up in p and p->next. duplicates, but p, p->next will never be the same: 1,4,3,5,3 this will work: 1,3,3,4,5

          image processing toolkits | batch image processing

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

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

          L C D 3 Replies Last reply
          0
          • S sadas232341s

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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #8

            You do not need to actually sort the list, since that would mean you first compare a lot of them and move around some nodes, then remove some (the duplicates). That is too much effort for what is needed. What you need to do is compare a lot and remove, but never move. In pseudo-code (always solve an algorithmic problem in pseudo-code first):

            foreach(Node n1) {
            foreach (Node n2 before n1) {
            if (n2.value==n1.value) {
            remove(n1);
            break;
            }
            }
            }

            :) PS: and that could be done on a single-linked list as well, as both loops are enumerating from the start and never need to go backward.

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            S 1 Reply Last reply
            0
            • C Chris Losinger

              you're on the right track. but the key thing that's missing is that the list needs to be sorted before you can be guaranteed that duplicates will show up in p and p->next. duplicates, but p, p->next will never be the same: 1,4,3,5,3 this will work: 1,3,3,4,5

              image processing toolkits | batch image processing

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

              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 1 Reply Last reply
              0
              • L Luc Pattyn

                You do not need to actually sort the list, since that would mean you first compare a lot of them and move around some nodes, then remove some (the duplicates). That is too much effort for what is needed. What you need to do is compare a lot and remove, but never move. In pseudo-code (always solve an algorithmic problem in pseudo-code first):

                foreach(Node n1) {
                foreach (Node n2 before n1) {
                if (n2.value==n1.value) {
                remove(n1);
                break;
                }
                }
                }

                :) PS: and that could be done on a single-linked list as well, as both loops are enumerating from the start and never need to go backward.

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

                i did not understand anything you said. And whats that code C++?

                1 Reply Last reply
                0
                • S sadas232341s

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

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

                  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 1 Reply Last reply
                  0
                  • 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