双链表,指针参数问题,求指教
DbLink.h文件
- C/C++ code
#ifndef _DbLink_H#define _DbLink_H#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct DbLink_L { struct DbLink_L *prev,*next; /* 前驱和后继 */ char name[50]; int en_g; int math_g;}DbLink,*pDbLink;void InitList(pDbLink &L) /* 初始化双链表,建立头结点 */{ L=(pDbLink)malloc(sizeof(DbLink)); if(L==NULL) { printf("malloc failde.\n"); exit(1); } L->prev=L->next=NULL; }void CreateList(pDbLink &L) /* 建立结点 */{ pDbLink newNode; char order='Y'; while(order=='Y') { newNode=(pDbLink)malloc(DbLink); printf("please input name,english grade,math grade\n:"); scanf("%s%d%d",newNode->name,&(newNode->en_g),&(newNode->math_g)); newNode->prev=L; newNode->next=L->next; L->next=newNode; printf("Create new node\n(Y/N):"); scanf("%c",order); } } void ListTraverse(pDbLink L) /* 遍历打印链表 */{ pDbLink p; p=L->next int i=1; while(p->next) { printf("\n%d.name:%s,english grade:%d,math grade:%d\n", i,p->name,p->en_g,p->math_g ); p=p->next; i++; } }void DelNode(pDbLink &L,int num) /* 删除某结点 */{ pDbLink p; int i=1; p=L->next; for(;p&&i<num;i++) { p=p->next; } p->prev->next=p->next; p->next->prev=p->prev; free(p); printf("Success to del.\n"); }void alterNode(pDbLink &L,int num) /* 修改结点值 */{ pDbLink p; int i=1; p=L->next; for(;p&&i<num;i++) { p=p->next; } printf("\n%d.name:%s,english grade:%d,math grade:%d\n", num,p->name,p->en_g,p->math_g ); printf("the value to alter \n:"); scanf("%s%d%d",p->name,&(p->en_g),&(p->math_g)); printf("Success to alter.\n"); } void DestroyList(pDbLink &L) /* 销毁链表 */{ pDbLink p,q; p=L->next; while(p) { q=p->next; free(p); p=q; } free(L); printf("Success to destroy Double Link List.\n"); }void InsertList(pDbLink &L,int num){ pDbLink p,newNode; int i=1; p=L->next; for(;p&&i<num;i++) { p=p->next; } newNode=(pDbLink)malloc(sizeof(DbLink)); printf("please input name,english grade,math grade\n:"); scanf("%s%d%d",newNode->name,&(newNode->en_g),&(newNode->math_g)); newNode->prev=p; newNode->next=p->next; p->next->prev=newNode; p->next=newNode; printf("Success to insert the node.\n"); } #endiftest.c文件
- C/C++ code
#include "DbLink.h"void main(){ int order,number; pDbLink list; InitList(list) printf("============★★================\n"); printf("====== 1.Create a List ==========\n"); printf("====== 2.Insert a Node ==========\n"); printf("====== 3.Delete a Node ==========\n"); printf("====== 4.Alter a Node ==========\n"); printf("====== 5.Print the List =========\n"); printf("====== 6.Destroy the List =======\n"); printf("====== 7.exit the program =======\n"); printf("============★★================\n"); printf("please input the order\n:"); scanf("%d",&order); while(order){ switch(order) { case 1: CreateList(list); break; case 2: printf("input the number to insert\n:"); scanf("%d",&number); InsertList(list,number); break; case 3: printf("input the number to delete\n:"); scanf("%d",&number); DelNode(list,number); break; case 4: printf("input the number to alter\n:"); scanf("%d",&number); alterNode(list,number); break; case 5: ListTraverse(list); break; case 6: DestroyList(list); break; case 7: exit(0); break; default: continue; } } }
为啥,我函数里面用pDbLink &L,这样的指针参数,就报错那,用法错了吗?求高手指教,不胜感激。
[解决办法]
pDbLink &L, 你这是用C99 还是 C++的引用?
[解决办法]
参考是C++的概念,请将test.c改为test.cpp再试试?
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct DbLink_L {
struct DbLink_L *prev,*next; /* 前驱和后继 */
char name[50];
int en_g;
int math_g;
}DbLink,*pDbLink;
void InitList(pDbLink *L) /* 初始化双链表,建立头结点 */
{
*L=(pDbLink)malloc(sizeof(DbLink));
if(L==NULL)
{
printf("malloc failde.\n");
exit(1);
}
(*L)->prev=NULL;
(*L)->next=(*L)->prev;
}
void CreateList(pDbLink *L) /* 建立结点 */
{
pDbLink newNode;
char order='Y';
pDbLink ptr = *L;
while(order=='Y')
{
newNode=(pDbLink)malloc(sizeof(DbLink));
printf("please input name,english grade,math grade\n:");
scanf("%s%d%d",newNode->name,&(newNode->en_g),&(newNode->math_g));
fflush(stdin);
newNode->prev=ptr;
newNode->next=ptr->next;
ptr->next=newNode;
printf("Create new node\n(Y/N):");
scanf("%c",&order);
fflush(stdin);
}
}
void ListTraverse(pDbLink L) /* 遍历打印链表 */
{
pDbLink p = NULL;
int i=1;
if(NULL != L)
p = L->next;
while(p)
{
printf("\n%d.name:%s,english grade:%d,math grade:%d\n",
i,p->name,p->en_g,p->math_g
);
p=p->next;
i++;
}
}
void DelNode(pDbLink *L,int num) /* 删除某结点 */
{
pDbLink p;
int i=1;
p=(*L)->next;
for(;p&&i<num;i++)
{
p=p->next;
}
if(NULL == p)
{
printf("Error!Delete position error!\n");
return ;
}
p->prev->next=p->next;
if(NULL != p->next)
p->next->prev=p->prev;
free(p);
printf("Success to del.\n");
}
void alterNode(pDbLink *L,int num) /* 修改结点值 */
{
pDbLink p;
int i=1;
p=(*L)->next;
for(;p&&i<num;i++)
{
p=p->next;
}
printf("\n%d.name:%s,english grade:%d,math grade:%d\n",
num,p->name,p->en_g,p->math_g
);
printf("the value to alter \n:");
scanf("%s%d%d",p->name,&(p->en_g),&(p->math_g));
printf("Success to alter.\n");
}
void DestroyList(pDbLink *L) /* 销毁链表 */
{
pDbLink p,q;
p=(*L)->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
free(*L);
printf("Success to destroy Double Link List.\n");
}
void InsertList(pDbLink *L,int num)
{
pDbLink p,newNode;
int i=1;
p=(*L)->next;
for(;p&&i<num;i++)
{
p=p->next;
}
if(NULL == p)
{
printf("Error!Insert position error!\n");
return ;
}
newNode=(pDbLink)malloc(sizeof(DbLink));
printf("please input name,english grade,math grade\n:");
scanf("%s%d%d",newNode->name,&(newNode->en_g),&(newNode->math_g));
newNode->prev=p;
newNode->next=p->next;
if(NULL != p->next)
p->next->prev=newNode;
p->next=newNode;
printf("Success to insert the node.\n");
}
int menu(int *pOrder)
{
printf("============★★================\n");
printf("====== 1.Create a List ==========\n");
printf("====== 2.Insert a Node ==========\n");
printf("====== 3.Delete a Node ==========\n");
printf("====== 4.Alter a Node ==========\n");
printf("====== 5.Print the List =========\n");
printf("====== 6.Destroy the List =======\n");
printf("====== 0.exit the program =======\n");
printf("============★★================\n");
printf("please input the order\n:");
scanf("%d",pOrder);
return *pOrder;
}
void main()
{
int number;
pDbLink list;
int order = 0;
InitList(&list);
while(menu(&order)){
switch(order)
{
case 1:
CreateList(&list);
break;
case 2:
printf("input the number to insert\n:");
scanf("%d",&number);
InsertList(&list,number);
break;
case 3:
printf("input the number to delete\n:");
scanf("%d",&number);
DelNode(&list,number);
break;
case 4:
printf("input the number to alter\n:");
scanf("%d",&number);
alterNode(&list,number);
break;
case 5:
ListTraverse(list);
break;
case 6:
DestroyList(&list);
list = NULL;
break;
case 0:
break;
default:
continue;
}
}
}
[解决办法]
c语言中只能用*不能用&,&在c++中为引用,在ANSIc中未定义