# Certain operations on linked list elements - How to?

Discussion in 'C' started by pseudobluz, Dec 3, 2009.

1. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
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!

2. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
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.

3. ### techgeek.inNew Member

Joined:
Dec 20, 2009
Messages:
572
Likes Received:
17
Trophy Points:
0
Occupation:
EOC (exploitation of computers)..i m a Terminator.
Location:
Not an alien!! for sure
Home Page:
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;
}
}```

Last edited by a moderator: Dec 22, 2009
4. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
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.

5. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
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?

6. ### techgeek.inNew Member

Joined:
Dec 20, 2009
Messages:
572
Likes Received:
17
Trophy Points:
0
Occupation:
EOC (exploitation of computers)..i m a Terminator.
Location:
Not an alien!! for sure
Home Page:
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!!

7. ### techgeek.inNew Member

Joined:
Dec 20, 2009
Messages:
572
Likes Received:
17
Trophy Points:
0
Occupation:
EOC (exploitation of computers)..i m a Terminator.
Location:
Not an alien!! for sure
Home Page:
8. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
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.

9. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
10. ### techgeek.inNew Member

Joined:
Dec 20, 2009
Messages:
572
Likes Received:
17
Trophy Points:
0
Occupation:
EOC (exploitation of computers)..i m a Terminator.
Location:
Not an alien!! for sure
Home Page:
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!!

11. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
Here is the english version:
http://rapidshare.com/files/324297385/cprog-eng.c.html

If you want to understand logic, you have to know Horner's scheme:
http://en.wikipedia.org/wiki/Horner_scheme

It is pretty simple but effective algorithm for evaluation of polynomials.

Note: There are some more functions in code, than it's really needed. I have added those in process of debugging to check out certain segments of code.

Can you please send me exe file of my code? I want to check out that.
I suspect now the source of the problem is maybe 32-bit compiler on 64-bit machine.

12. ### techgeek.inNew Member

Joined:
Dec 20, 2009
Messages:
572
Likes Received:
17
Trophy Points:
0
Occupation:
EOC (exploitation of computers)..i m a Terminator.
Location:
Not an alien!! for sure
Home Page:
13. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
Thanks, mate! I'll try that on my friend's 32bit pc and see what happens.

14. ### pseudobluzNew Member

Joined:
Dec 3, 2009
Messages:
10
Likes Received:
0
Trophy Points:
0
Home Page:
I made it

The problem was with one pointer referring to NULL value.

Here is the corrected version:

Code:
```struct pnode *bform(struct pnode *coeff, int a)
{
struct pnode *p=coeff;
struct pnode *temp1=p;
struct pnode *proba=NULL;
double b=temp1->coeff;
proba=insert(proba,1,b);
while(temp1->link!=NULL)
{
temp1->link->coeff=a*(temp1->coeff)+(temp1->link->coeff);
proba=insert(proba,1,temp1->link->coeff);
temp1=temp1->link;

}
return(proba);
}
```
So, if anyone needs this prog for computing Horne's scheme, I ll translate it and upload.
It's not a big deal, but I couldn't find anyone did this using linked lists.
It's way easier with arrays.