Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Copy Linked List Function

linked list

  • This topic is locked This topic is locked
17 replies to this topic

#1 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 22 May 2012 - 12:22 AM

Hi, i have a following C problem:

I need to make a function (with a returning value) that will copy one linked list to another.
L1 is already loaded and fully usable list, and the L2 list is a empty list that needs to be fulled with content from L1.

Afret copying, the lists should be completly independend.

I have a following bit of code that i wrote, but it doesn't seem to work:
Elem *copy(Elem *L1, Elem *L2, int n){
    temp1=L1;

for(i=0;i<n;i++){
    Elem *new = malloc (sizeof(Elem));
    if(new==NULL) printf("Failed allocation!"), exit(1);
    
        new->data=temp1->new;
        new->next=NULL
        
    if(!L2) return new;
    else {
        Elem *temp2 = L2;
        while (temp2->next) temp2 = temp2->next;
        temp2->next2=new;
        return L2;
    }
    temp1->next=temp1->next;
    }/* kraj for-a*/
}

So, can anyone help or give some guidelines?
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 22 May 2012 - 12:43 AM

There are few problems in your code -- i) if you want to pass the copied linked list as a argument (in this case L2), you have to pass double pointer (**) for it, ii) you are looping to the second linked-list (in this case L2) to make it point the newly created item. This is an extra looping you are doing each time creating a destination node.

Following is the code that copy the given linked-list to a new one and return it.

struct __Elem__
{
       int data;
       struct __Elem__ *next;
};

typedef struct __Elem__ Elem;

Elem *copy(Elem *L1)
{
     Elem*  L2 = 0;
     Elem* preElem = 0;

     // Move through all the nodes in source list.
     while(L1 != 0) {

          // Create the memory for the destination list node.
          Elem *elem = (Elem*)malloc (sizeof(Elem));

          if(elem == 0) {
               printf("Failed allocation!"), exit(1);
          }

          // Copy the data from the source to dest node
          elem->data = L1->data;
          elem->next = 0;

          if (L2 == 0) {
                // keep the root for the destination list.
                L2 = elem;
                preElem = elem;
          }
          else {
                // point the newly created node to the previous created node.
                preElem->next = elem;
                // keep the new node to use it as previous node in next node creation.
                preElem = elem;
          }

          L1 = L1->next;
    }

    return L2;
}

  • 0

#3 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 22 May 2012 - 01:01 AM

Ok, so if i call this function inside another function, the returning value would be the pointer to the L2, and i can use L2 from the main program standardly?

I tried this code, called it inside the program as copy(L1), and then via function that prints the function on the screen i called print(L2), and it didn't print anything? (print is a function that i wrote for printing out linked lists).
  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 22 May 2012 - 01:08 AM

Yes, the 'copy' method will work as you said. You can post your code (for creating source list & printing the destination list) so that we can catch the problems in your code.

Following is the usage of the above code.
void print(Elem* L2)
{
     // Loop through the L2 and print value for each node of it
     while(L2 != 0) {
          printf("%d ", L2->data);
          L2 = L2->next;
     }
}

void main()
{
     Elem * L1, *L2;
     // create two nodes in L1
     L1 = (Elem*)malloc (sizeof(Elem)); // First node
     L1->data = 10;
     L1->next =  (Elem*)malloc (sizeof(Elem)); // Second node
     L1->next->data = 20;
     L1->next->next = 0;

     // make a copy of L1
     L2 = copy(L1);
     print(L2);
}

If the code/solution works for you, please leave a reply so that we know that and mark the thread as 'SOLVED'.
  • 0

#5 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 22 May 2012 - 02:58 AM

Thank you for your help. I found an error in my print function and it works now.

Anyways, i have another problem related to these lists - lists contans data about meetings and their priority.
I need to edit the list so that there are no overlapings of the meetings, in a following way - if two meetings overlap, the one with a higher priority stays, and if the priorities are the same - the one that starts earlier stays,

I thought about doing it in the following way - i compare the first meeting with all others, and when i find an overlaping meeting, i delete one of them based on the previous criteria.
Then i compare the second meeting with all others except the first (if the first is still in the list after comparison), and do the same thing (delete based on the criteria).
Then with the third, foruth and so on until the n-1 meeting (where n is the number of overall meetings).

Would it work this way? Or did i overlook something?
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 22 May 2012 - 03:00 AM

I think your logic for the 'overlapping' feature is ok.
  • 0

#7 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 22 May 2012 - 03:02 AM

Ok, thanks!

If i have any problems, i'll post here ;)!
  • 0

#8 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 22 May 2012 - 07:45 AM

Wouldn't it be better for Print to be a method inside the object?

Then
void print(Elem* L2)
{
	 // Loop through the L2 and print value for each node of it
	 while(L2 != 0) {
		  printf("%d ", L2->data);
		  L2 = L2->next;
	 }
}

Would go inside the class

and:
print(L2);
Would become
l2.print();

just my 2 cents
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#9 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 22 May 2012 - 07:59 AM

Wouldn't it be better for Print to be a method inside the object?

Then

void print(Elem* L2)
{
	 // Loop through the L2 and print value for each node of it
	 while(L2 != 0) {
		  printf("%d ", L2->data);
		  L2 = L2->next;
	 }
}

Would go inside the class

and:
print(L2);
Would become
l2.print();


Well, we are working with C.
  • 0

#10 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 23 May 2012 - 08:53 AM

Hi guys, i wrote the code, with that overlaping thing, and it goes like this

while(i<duz){
tmp1 = lst;
tmp2 = lst;
//printf("%.2f - %.2f", tmp2->start, tmp2->end);
j=i+1;
  for(k=1;k<i;k++)tmp1=tmp1->next;
  for(k=1;k<j;k++)tmp2=tmp2->next;
 
   while(j<=duz)
   {
    if((tmp1->start < tmp2->start && tmp1->end > tmp2->start && tmp1->end < tmp2->end) ||
    (tmp1->start > tmp2->start && tmp1->start < tmp2->end && tmp1->end > tmp2->end) ||
    (tmp1->start > tmp2->start && tmp1->end < tmp2->end) ||
    (tmp1->start < tmp2->start && tmp1->end > tmp2->end) ||
    (tmp1->start == tmp2->start && tmp1->end == tmp2->end))
    {
    printf("Preklapaju se termini: %.2fh - %.2fh i %.2fh - %.2fh\n", tmp1->start, tmp1->end, tmp2->start, tmp2->end);
    if(tmp1->prority == tmp2->prority) {
	 if(tmp1->start > tmp2->start){izbacivanje_el_liste(&drugi,i);i=i-1;duz=duz-1;break;}
	 else {tmp2=tmp2->next;izbacivanje_el_liste(&drugi,j);duz=duz-1;j++;continue;}
    } /*  */
    if(tmp1->prority != tmp2->prority) {
	 if(tmp1->prority < tmp2->prority){izbacivanje_el_liste(&drugi,i);i=i-1;duz=duz-1;break;}
	 else {tmp2=tmp2->next;izbacivanje_el_liste(&drugi,j);duz=duz-1;j++;continue;}
    } /*  */
    }/*  */
    else {
    j++;
    tmp2=tmp2->next;}
   }
  
i++;
}/**/

It seems to work, most of the time...
But, there are occasions when it doesn't work...

For example, if i enter 5 same intervals, with same priorities, it should leave only one, but it doesnt.

And, when i enter, for example (priority, start and end value)
1 12 13
1 11.30 13.30
2 12.30 13.30

it should leave only the thirtd item, becase all ov them interlap, and the 3rd has the highest priority, but it leaves 1st and 3rd in the list?

So, can anyone help?
  • 0

#11 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 23 May 2012 - 08:57 AM

Please post your complete code here so that we can look into that.
  • 0

#12 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 23 May 2012 - 09:00 AM

Well, basically that's the whole code.

It's a function, and i pass a list to it.

void interlap(Elem *prvi, Elem *drugi, int n){
	Elem *tmp1,*tmp2;
	int duz,i=1,j,k; /* duz represents the lenght of a list*/
	lst = copy(orig_list);

	tmp1 = lst;
	tmp2 = lst;
	duz=n;

That stands just before the while loop, and basically that's that of my code;


Edit: i found out that my condition was wrong, and that's why the program didn't detect the overlaping of 11.30 13.30 and 12.30 and 13.30
So, the edited contdition is this
if((tmp1->start < tmp2->start && tmp1->end > tmp2->start && tmp1->end < tmp2->end) ||
                (tmp1->start > tmp2->start && tmp1->start < tmp2->end && tmp1->end > tmp2->end) ||
                (tmp1->start > tmp2->start && tmp1->end < tmp2->end) ||
                (tmp1->start < tmp2->start && tmp1->end > tmp2->end) ||
                (tmp1->start == tmp2->start && tmp1->end == tmp2->end))

But, still, it seems that it doesnt delete elements when i enter a list of, like 5 elements with same parameters
(the lists loads ok - it contains 5 identical elements, but, after deleting it only has to have one).
  • 0





Also tagged with one or more of these keywords: linked list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download