0
#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;
}

ここでやったことは、個人の個人情報の構造をまとめたリンクリストを作成した後、構造の名でリンクリストを並べ替え(昇順)することを考えました。

このプログラムを実行しようとすると、エラーが発生しました

エラー:単項 `&'の非左辺値

バブルソート関数のスワップ関数にパラメータを渡している間。

誰かが私を助けてくれますか、そして左辺値が何を意味するのか説明してください。ありがとう。

4

2 に答える 2

1

KingsIndian が提供した変更に加えて、swap関数を変更する必要があります。

void swap(harsha *pnode1,harsha *pnode2)
{
    harsha temp;
    temp=*pnode1;
    *pnode1=*pnode2;
    *pnode2=temp;
}

また、次のように呼び出す方が理にかなっている場合があります。

swap(&pnode[j], &pnode[j-1]);
于 2012-10-13T08:36:23.800 に答える
0

エラーは次の行にあります。

 swap(&(pnode+j),&(pnode+j-1));

pnodeはポインタであり、 で追加するとj右辺値が生成されます。右辺値のアドレスを取得する&ことはできないため、コンパイラでエラーが発生します。

于 2012-10-13T08:24:11.233 に答える