动态链表中如何实现查找功能啊?

usr_src 2009-07-16 10:26:18
例如在一个动态链表中有一个关于学生信息的结构体,里面有姓名,学号,年龄等!!怎么去实现用姓名查找或者是学号查找??多谢高人指点!!
...全文
435 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
usr_src 2009-07-17
  • 打赏
  • 举报
回复
多谢楼上的各路高手~~散分咯!!!
lpyzl 2009-07-17
  • 打赏
  • 举报
回复
#include "stdafx.h" 
#include "stdlib.h"
#include "string.h"

struct student//設置結構體
{
char* name;//姓名
unsigned int ID;//學號
unsigned int age;//年齡
student* pnext;//指向下一個的指針
};


int Append_Student(student** pphead,char* name,unsigned int ID,unsigned int age);
int Search_Student(student** pphead,char* name,unsigned int* pID,unsigned int* page);

int _tmain(int argc, _TCHAR* argv[])
{
student* phead=NULL;//設置一個指針,指向鏈錶的頭
if(!Append_Student(&phead,"ab",0113324,10))//將指針的地址放入函數,和學生信息
printf("Succeed to add student!\n");
else
printf("Fail to add student!\n");
if(!Append_Student(&phead,"cd",0113325,11))//將指針的地址放入函數,和學生信息
printf("Succeed to add student!\n");
else
printf("Fail to add student!\n");
if(!Append_Student(&phead,"ef",0113326,10))//將指針的地址放入函數,和學生信息
printf("Succeed to add student!\n");
else
printf("Fail to add student!\n");

unsigned int ID=0;
unsigned int age=0;
if(Search_Student(&phead,"cd",&ID,&age)==0)//把ID和age的地址放入函數內,讓函數往地址內賦值
printf("Found specified student, ID:%d\t Age:%d\n",ID,age);
else
printf("Cannot found specified student!\n");

return 0;
}

int Append_Student(student** pphead,char* name,unsigned int ID,unsigned int age)
{
student* pnew_st=(student*)malloc(sizeof(student));//在heap中開闢內存空間,尺寸為一個結構體的大小
if(pnew_st==NULL)//內存開闢失敗,返回-1
return -1;
(*pnew_st).name=name;//初始化結構體
(*pnew_st).ID=ID;
(*pnew_st).age=age;
(*pnew_st).pnext=NULL;//注意,指向下一個指針必須初始化,設置為NULL
if(*pphead==NULL)//如果鏈錶頭的指針地址為空,說明第一次調用該函數,這個結構體的地址賦值到鏈錶頭指針
{
*pphead=pnew_st;
return 0;//添加成功
}
student* plast_st=*pphead;//否則,遍歷鏈錶,一直到最後一個節點
while((*plast_st).pnext!=NULL)
{
plast_st=(*plast_st).pnext;
}
(*plast_st).pnext=pnew_st;//把新的節點的地址放入最後一個節點的指針內
return 0;//添加成功
}

int Search_Student(student** pphead,char* name,unsigned int* pID,unsigned int* page)
{
student* pcurrent_st=*pphead;//獲取鏈錶頭的地址
if(strcmp((*pcurrent_st).name,name)==0)//判斷該學生的名字是不是和指定名字相同
{
*pID=(*pcurrent_st).ID;//如果相同,將該學生的學好,年齡賦值到地址的值內
*page=(*pcurrent_st).age;
return 0;//成功
}

while((*pcurrent_st).pnext!=NULL)//如果第一個節點不是,遍歷鏈錶
{
pcurrent_st=(*pcurrent_st).pnext;//指針指向下一個節點
if(strcmp((*pcurrent_st).name,name)==0)//判斷
{
*pID=(*pcurrent_st).ID;
*page=(*pcurrent_st).age;
return 0;
}
}
return -1;//遍歷結束依然找不到,返回-1
}

顶。学习了。。。
ernst20020530 2009-07-17
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"

struct student//設置結構體
{
char* name;//姓名
unsigned int ID;//學號
unsigned int age;//年齡
student* pnext;//指向下一個的指針
};


int Append_Student(student** pphead,char* name,unsigned int ID,unsigned int age);
int Search_Student(student** pphead,char* name,unsigned int* pID,unsigned int* page);

int _tmain(int argc, _TCHAR* argv[])
{
student* phead=NULL;//設置一個指針,指向鏈錶的頭
if(!Append_Student(&phead,"ab",0113324,10))//將指針的地址放入函數,和學生信息
printf("Succeed to add student!\n");
else
printf("Fail to add student!\n");
if(!Append_Student(&phead,"cd",0113325,11))//將指針的地址放入函數,和學生信息
printf("Succeed to add student!\n");
else
printf("Fail to add student!\n");
if(!Append_Student(&phead,"ef",0113326,10))//將指針的地址放入函數,和學生信息
printf("Succeed to add student!\n");
else
printf("Fail to add student!\n");

unsigned int ID=0;
unsigned int age=0;
if(Search_Student(&phead,"cd",&ID,&age)==0)//把ID和age的地址放入函數內,讓函數往地址內賦值
printf("Found specified student, ID:%d\t Age:%d\n",ID,age);
else
printf("Cannot found specified student!\n");

return 0;
}

int Append_Student(student** pphead,char* name,unsigned int ID,unsigned int age)
{
student* pnew_st=(student*)malloc(sizeof(student));//在heap中開闢內存空間,尺寸為一個結構體的大小
if(pnew_st==NULL)//內存開闢失敗,返回-1
return -1;
(*pnew_st).name=name;//初始化結構體
(*pnew_st).ID=ID;
(*pnew_st).age=age;
(*pnew_st).pnext=NULL;//注意,指向下一個指針必須初始化,設置為NULL
if(*pphead==NULL)//如果鏈錶頭的指針地址為空,說明第一次調用該函數,這個結構體的地址賦值到鏈錶頭指針
{
*pphead=pnew_st;
return 0;//添加成功
}
student* plast_st=*pphead;//否則,遍歷鏈錶,一直到最後一個節點
while((*plast_st).pnext!=NULL)
{
plast_st=(*plast_st).pnext;
}
(*plast_st).pnext=pnew_st;//把新的節點的地址放入最後一個節點的指針內
return 0;//添加成功
}

int Search_Student(student** pphead,char* name,unsigned int* pID,unsigned int* page)
{
student* pcurrent_st=*pphead;//獲取鏈錶頭的地址
if(strcmp((*pcurrent_st).name,name)==0)//判斷該學生的名字是不是和指定名字相同
{
*pID=(*pcurrent_st).ID;//如果相同,將該學生的學好,年齡賦值到地址的值內
*page=(*pcurrent_st).age;
return 0;//成功
}

while((*pcurrent_st).pnext!=NULL)//如果第一個節點不是,遍歷鏈錶
{
pcurrent_st=(*pcurrent_st).pnext;//指針指向下一個節點
if(strcmp((*pcurrent_st).name,name)==0)//判斷
{
*pID=(*pcurrent_st).ID;
*page=(*pcurrent_st).age;
return 0;
}
}
return -1;//遍歷結束依然找不到,返回-1
}
raymondguo008 2009-07-16
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h>
#include <string.h>/*包含一些字符串处理函数的头文件*/
#define N 10

typedef struct node
{
char name[20];
struct node *link;
}stud;

stud * creat(int n) /*建立链表的函数*/
{
stud *p,*h,*s;
int i;

if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}

h->name[0]='\0';
h->link=NULL;
p=h;

for(i=0;i<N;I++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}
p->link=s;
printf("请输入第%d个人的姓名",i+1);
scanf("%s",s->name);
s->link=NULL;
p=s;
}

return(h);
}

stud * search(stud *h,char *x) /*查找链表的函数,其中h指针是链表的表头指针,x指针是要查找的人的姓名*/
{
stud *p; /*当前指针,指向要与所查找的姓名比较的结点*/
char *y; /*保存结点数据域内姓名的指针*/
p=h->link;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0) /*把数据域里的姓名与所要查找的姓名比较,若相同则返回0,即条件成立*/
return(p); /*返回与所要查找结点的地址*/
else p=p->link;
}

if(p==NULL)
printf("没有查找到该数据!");
}

main()
{
int number;
char fullname[20];
stud *head,*searchpoint; /*head是表头指针,searchpoint是保存符合条件的结点地址的指针*/
number=N;
head=creat(number);
printf("请输入你要查找的人的姓名:");
scanf("%s",fullname);
searchpoint=search(head,fullname); /*调用查找函数,并把结果赋给searchpoint指针*/
}
Nio96 2009-07-16
  • 打赏
  • 举报
回复
不客氣。。。
usr_src 2009-07-16
  • 打赏
  • 举报
回复
多谢ls好友指点@@@我动手试一下!!!
Nio96 2009-07-16
  • 打赏
  • 举报
回复
定义一个学生的结构体,包含学生的姓名,学号,年龄,指向下一个节点的指针等。一个学生一个节点。

查找的时候如果按学号的话,链表的话一般只能一个一个地比较,比较学号或者姓名。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧