Reputation: 5924
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
struct name * create_node(char *,char *,int );
struct name *add_node(struct name *,char *,char *,int);
void output(struct name *,char *);
void free_variables(struct name *);
struct name *bubble_sort(struct name *,int);
void swap(struct name **,struct name **);
typedef struct name
{
char firstname[100];
char secondname[100];
int pno;
struct name *next;
}harsha;
harsha *create_node(char *first,char *second, int phone)
{
harsha *pnode=(struct name *)malloc(sizeof(struct name));
strcpy(pnode->firstname,first);
strcpy(pnode->secondname,second);
pnode->pno=phone;
pnode->next=NULL;
return pnode;
}
harsha *bubble_sort(harsha *pnode,int count)
{
int i,j,n;
n=count;
for(i=n;i>1;i--)
{
for(j=1;j<n;j++)
{
if(strcmp(pnode[j].firstname,pnode[j-1].firstname)<0)
{
swap(&(pnode+j),&(pnode+j-1));
}
}
}
return pnode;
}
void swap(harsha **pnode1,harsha **pnode2)
{
harsha *temp;
temp=*pnode1;
*pnode1=*pnode2;
*pnode2=temp;
}
harsha *add_node(harsha *pnode,char *first,char *second,int phone)
{
if(pnode==NULL)
{
pnode=create_node(first,second,phone);
return pnode;
}
else
{
while(pnode->next!=NULL)
{
pnode=pnode->next;
}
pnode->next=create_node(first,second,phone);
}
return pnode;
}
void output(harsha *pnode,char *str)
{
while(pnode->next!=NULL)
{
if(strcmp(pnode->secondname,str)==0)
{
printf("%s",pnode->firstname);
printf("%d\n",pnode->pno);
}
pnode=pnode->next;
}
if(strcmp(pnode->secondname,str)==0)
{
printf("%s",pnode->firstname);
printf("%d\n",pnode->pno);
}
}
void free_variables(harsha *pnode)
{
if(pnode->next!=NULL)
free_variables(pnode->next);
free(pnode);
}
int main()
{
harsha *head=NULL;
//int i;
char first[50];
char second[50];
char sname[50];
int phone;
char option='y';
int count=0;
while(tolower(option)=='y')
{
count++;
printf("enter the first name:");
scanf("%s",first);
printf("enter the second name:");
scanf("%s",second);
printf("enter the phone number:");
scanf("%d",&phone);
fflush(stdin);
if (head==NULL)
head=create_node(first,second,phone);
else
add_node(head,first,second,phone);
printf("enter the option:");
scanf("%c",&option);
}
fflush(stdin);
head=bubble_sort(head,count);
printf("enter the second name:");
scanf("%s",sname);
output(head,sname);
free_variables(head);
return 0;
}
What I have done here is-I have created a linked list with the structures of personal information of an individual.After that I thought of sorting(ascending order) the linked list based on the first name of the structures.
When I tried to run this program.I have got an error
error:non-lvalue in unary `&'
while passing the parameters to the swap function in the bubble sort function.
Can someone help me out and please explain what lvalue means.Thanks..
Upvotes: 0
Views: 8202
Reputation: 121397
The error is in this line:
swap(&(pnode+j),&(pnode+j-1));
pnode
is a pointer and when you add it with j
, it produces a rvalue. Taking address-of &
an rvalue is not possible, hence compiler gives error.
Upvotes: 0
Reputation: 781493
In addition to the change KingsIndian gave, you need to change the swap
function:
void swap(harsha *pnode1,harsha *pnode2)
{
harsha temp;
temp=*pnode1;
*pnode1=*pnode2;
*pnode2=temp;
}
Also, it may make more sense to you to call it this way:
swap(&pnode[j], &pnode[j-1]);
Upvotes: 1