#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;
}
我在这里所做的是-我创建了一个带有个人个人信息结构的链表。之后我想到了根据结构的名字对链表进行排序(升序)。
当我试图运行这个程序时,我遇到了一个错误
错误:一元“&”中的非左值
同时将参数传递给冒泡排序函数中的交换函数。
有人可以帮助我,请解释左值的含义。谢谢..