Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Certain operations on linked list elements - How to? (http://www.go4expert.com/forums/operations-linked-list-elements-t20267/)

pseudobluz 3Dec2009 16:31

Certain operations on linked list elements - How to?
 
Hi, all!

First, I've used search on this forum for my problem and found some very good articles about linked list, but nothing that can help me.

So, is it possible to write function in C for adding two adjoining elements of the list?
And to start that adding from second element of the list, so the first one stays the same?
Here is what I need to do:
If elements of the list are: a5, a4,...a1, I need to get the following elements:
b4=a5, b3=x*b4+a4,..., b1=x*b2+a2, b0=x*b1+a1, where x is integer value argument of the function.
Here is what I've tried:

Code:

struct niz *amnoz(struct niz *a,int m, int n)
{
    struct niz *prvi=a;
    struct niz *current=a->pok;
    struct niz *newList = NULL;
    struct niz *tail = NULL;
    while (current != NULL)
    {
       

       
        if (newList == NULL) {
        newList = (struct niz*) malloc(sizeof(struct niz));
        newList->koef = current->koef+(m*prvi->koef);
        newList->indeks=n;
        newList->pok=NULL;
        tail = newList;
       
        n--;
        }
else {
   
    tail->pok =(struct niz*) malloc(sizeof(struct niz));
    tail = tail->pok;
    tail->koef = current->koef+m*prvi->koef;
   
    tail->indeks = n;
    tail->pok = NULL;
   
}
current = current->pok;
}
return(newList);
}

This function does adding elements and multiplying them, but on a wrong way, only multiplying the first one with integer than adding it with the others.

Can this be done with single linked list, or do I have to use doble linked list?

Thanks in advance!

pseudobluz 21Dec2009 13:47

Re: Certain operations on linked list elements - How to?
 
I've solved the problem:

Code:

struct niz *bform(struct niz *koef, int n, int a)
{
        int i=0;
      struct niz *p=koef;
      struct niz *temp1=p;
      struct niz *proba=NULL;
      struct niz *temp2=p->pok;
      double b=temp1->koef;
      insertn(proba,b);
      for(i=1; i++; i=n-1)
      {
          temp1=temp1->pok;
          temp2=temp2->pok;
          b=a*temp1->koef+temp2->koef;
          insertn(proba, b);
      }
      return(proba);
}

Now I have runtime error 0xC0000005: Access Violation on line: b=a*temp1->koef+temp2->koef;. I've googled and found out it's due to program trying to access memory not reserved for it.
So, how can I fix this? Thanks.

techgeek.in 21Dec2009 21:30

Re: Certain operations on linked list elements - How to?
 
hi pseudobluz,
sorry..i din referred to ur code...but understanding the problem description i have designed the program for u...try to understand it ..if u cnt then ask me frankly...it is working perfectly..(designed and verified myself)




Code:

# include<stdio.h>
# include<conio.h>
# include<malloc.h>
void insert_item(struct list**,int);
void display_list(struct list *);
void required_function(struct list **,int);

struct list
{
int info;
struct list *link;
};

void main()
{
struct list *list1=NULL;
int i,a;
clrscr();
printf("enter the five numbers in the list:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&a);
insert_item(&list1,a);
}

// change the elements of the list
required_function(&list1,2); // the x parameter
display_list(list1);
getch();
}

void insert_item(struct list **list1,int n)
{
struct list *ptr;
struct list *node;
if((*list1)==NULL)
{
node=(struct list *)malloc(sizeof(struct list));
node->info=n;
(*list1)=node;
node->link=NULL;
}

else
{
ptr=(*list1);
while((ptr->link)!=NULL)
ptr=ptr->link;
node=(struct list *)malloc(sizeof(struct list));
node->info=n;
ptr->link=node;
node->link=NULL;
}
}


void display_list(struct list *list1)
{

while((list1->link)!=NULL)
{
printf("%d\n",list1->info);

list1=list1->link;

}
printf("%d\n",list1->info);
}

void required_function(struct list** list1,int x)
{
int i,j,temp;
struct list *ptr;

for(i=1;i<=5;i++)
{
ptr=(*list1);
for(j=1;j<=i;j++)
{
temp=ptr->info;
ptr=ptr->link;


}
ptr->info=x*temp + ptr->info;
}
}


pseudobluz 21Dec2009 22:56

Re: Certain operations on linked list elements - How to?
 
Thanks! I'll take a look at your code and try to fix mine. What I'm trying to do is write prog for Horner's rule via singly linked lists. Once I repair my code I'll post the results.

pseudobluz 21Dec2009 23:07

Re: Certain operations on linked list elements - How to?
 
UPDATE:

I've just tried to compile your code and it went smoothly but there is the same runtime error as before on line:

ptr->info=x*temp + ptr->info;

You said this code works so I guess it has something to do with compiler?
Which one are you using?

techgeek.in 21Dec2009 23:24

Re: Certain operations on linked list elements - How to?
 
i m using turbo c++...whatever the compiler u use for c..u wnt get the error as far as my code is concerned...
try this:-

(ptr->info)=(x*temp) + (ptr->info)

if still u get error then i will upload the c file in rapidshare...inform me asap becoz i m astounded by the fact that u got error..i have checked my code 10 more times but getting the code running for all input sample...nd one more thing this program is based on singly linked list...if u want the same program using doubly linked list then that also i can upload for u...i kept the basic concept while designing the code...IT WORKS FINE!!

techgeek.in 21Dec2009 23:31

Re: Certain operations on linked list elements - How to?
 
ohk..i m giving u the rapidshare link of the zip file containing MY CODE as well as EXE file.
run the exe file nd get confirmed...i think u have got something wrong with ur compiler..

http://rapidshare.com/files/324019864/linklist.rar.html

pseudobluz 22Dec2009 02:39

Re: Certain operations on linked list elements - How to?
 
I can not run your exe file. I've got the error about program incompatibillity with my x64 os.

That is probably due the program being compiled on 32-bit OS?

So I've opened your c file, tried to compile it and received the same error as before, so most definitely it's compiler related problem.
I will try Turbo C++ and some other compilers, and report the resulits once I'm done.

This problem became pretty irritating for me, since I can not solve it for days, and it's such a simple program. And I have previous experince in programming much complicated stuff in C/C++/C#. However it's been some time since my last program :)

Once more, thank you very much for you effort.
I'll update this topic once I test all the other compilers.

pseudobluz 22Dec2009 03:25

Re: Certain operations on linked list elements - How to?
 
Now I've tried Dev C++. Still the same. Can you please try to compile this code on your rig:
http://rapidshare.com/files/324105295/cprog.c.html

Ignore any comments. Those are in my native language, so you won't understand probably.

techgeek.in 22Dec2009 07:52

Re: Certain operations on linked list elements - How to?
 
your program is working file without any compilation error...it also executed well..i couldnt check the logic becoz the print comments were in ur native language...can u make it in english for me so that i can check ur logic..?? UR PROG WORKS FINE!!


All times are GMT +5.5. The time now is 01:51.