c语言链表和指针的运用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言链表和指针的运用相关的知识,希望对你有一定的参考价值。
在学习指针之前,首先要认识指针。指针是一个存储计算机内存地址的变量。从指针指向的内存读取数据称作指针的取值。指针可以指向某些具体类型的变量地址,例如int、long和double。指针也可以是void类型、NULL指针和未初始化指针。
根据出现的位置不同,操作符 * 既可以用来声明一个指针变量,也可以用作指针的取值。当用在声明一个变量时,*表示这里声明了一个指针。其它情况用到*表示指针的取值。&是地址操作符,用来引用一个内存地址。通过在变量名字前使用&操作符,我们可以得到该变量的内存地址。
说到指针就要说到数组和结构体,C语言的数组表示一段连续的内存空间,用来存储多个特定类型的对象。与之相反,指针用来存储单个内存地址。数组和指针不是同一种结构因此不可以互相转换。而数组变量指向了数组的第一个元素的内存地址。
一个数组变量是一个常量。即使指针变量指向同样的地址或者一个不同的数组,也不能把指针赋值给数组变量。也不可以将一个数组变量赋值给另一个数组。然而,可以把一个数组变量赋值给指针,这一点似乎让人感到费解。把数组变量赋值给指针时,实际上是把指向数组第一个元素的地址赋给指针。就像数组一样,指向结构体的指针存储了结构体第一个元素的内存地址。与数组指针一样,结构体的指针必须声明和结构体类型保持一致,或者声明为void类型。
除了指针还有一个重要的内容是链表。在学习链表前,静态内存分配和动态内存分配。很多的情况下,你并不能确定要使用多大的数组,那么你就要把数组定义得足够大。这样,你的程序在运行时就申请了固定大小的你认为足够大的内存空间。即使你知道数量大小,但是如果因为某种特殊原因数量有增加或者减少,你又必须重新去修改程序,扩大数组的存储范围。这种分配固定大小的内存分配方法称之为静态内存分配。但是这种内存分配的方法存在比较严重的缺陷,特别是处理某些问题时:在大多数情况下会浪费大量的内存空间,在少数情况下,当你定义的数组不够大时,可能引起下标越界错误,甚至导致严重后果。那么有没有其它的方法来解决这样的外呢体呢?有,那就是动态内存分配。
所谓动态内存分配就是指在程序执行的过程中动态地分配或者回收存储空间的分配内存的方法。动态内存分配不象数组等静态内存分配方法那样需要预先分配存储空间,而是由系统根据程序的需要即时分配,且分配的大小就是程序要求的大小。从以上动、静态内存分配比较可以知道动态内存分配相对于静态内存分配的特点:
1、不需要预先分配存储空间;
2、分配的空间可以根据程序的需要扩大或缩小。
接下来就是链表了,链表由一系列不必在内存中相连的结构组成。每一个结构均含有表元素和指向包含该元素后继元的结构指针。我们称之为next指针。最后一个单元的next指针指向NULL;该值由C定义并且不能与其它指针混淆。ANSI C规定NULL为零。
链表的基本运算
查找
对单链表进行查找的思路为:对单链表的结点依次扫描,检测其数据域是否是我们所要查好的值,若是返回该结点的指针,否则返回NULL。
以下为源代码:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N 3
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指针*/
printf("查找的姓名是:%s \n",searchpoint->name);
}
插入
插入命令需要使用一次malloc调用从系统得到一个新单元并在此后执行两次指针调整。想法通过图3给出,其中虚线表示原来的指针。
以下是源代码:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N 3
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) /*查找函数*/
{
stud *p;
char *y;
p=h->link;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
return(p);
else p=p->link;
}
if(p==NULL)
printf("没有查找到该数据!");
}
stud * list(stud *h) /*查找函数*/
{
stud *p;
char *y;
p=h->link;
printf("所有的姓名列表 \n");
while(p!=NULL)
{
printf("%s \n",p->name);
p=p->link;
}
}
void insert(stud *p) /*插入函数,在指针p后插入*/
{
char stuname[20];
stud *s; /*指针s是保存新结点地址的*/
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}
printf("请输入你要插入的人的姓名:");
scanf("%s",stuname);
strcpy(s->name,stuname); /*把指针stuname所指向的数组元素拷贝给新结点的数据域*/
s->link=p->link; /*把新结点的链域指向原来p结点的后继结点*/
p->link=s; /*p结点的链域指向新结点*/
}
main()
{
int number;
char fullname[20]; /*保存输入的要查找的人的姓名*/
stud *head,*searchpoint;
number=N;
head=creat(number); /*建立新链表并返回表头指针*/
printf("请输入你要查找的人的姓名:");
scanf("%s",fullname);
searchpoint=search(head,fullname); /*查找并返回查找到的结点指针*/
insert(searchpoint); /*调用插入函数*/
list(head);
}
还有一些其它方法,有待我去学习。
以下是建立的一个学生管理系统,但是我写的这个系统还是有些错误我无法解决,证明在这方面我还有许多东西要去学习。
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N 3
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) /*查找函数*/
{
stud *p;
char *y;
p=h->link;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
return(p);
else p=p->link;
}
if(p==NULL)
printf("没有查找到该数据!");
}
stud * search2(stud *h,char *x)
/*另一个查找函数,返回的是上一个查找函数的直接前驱结点的指针,
h为表头指针,x为指向要查找的姓名的指针
其实此函数的算法与上面的查找算法是一样的,只是多了一个指针s,并且s总是指向指针p所指向的结点的直接前驱,
结果返回s即是要查找的结点的前一个结点*/
{
stud *p,*s;
char *y;
p=h->link;
s=h;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
return(s);
else
{
p=p->link;
s=s->link;
}
}
if(p==NULL)
printf("没有查找到该数据!");
}
void insert(stud *p) /*插入函数,在指针p后插入*/
{
char stuname[20];
stud *s; /*指针s是保存新结点地址的*/
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}
printf("请输入你要插入的人的姓名:");
scanf("%s",stuname);
strcpy(s->name,stuname); /*把指针stuname所指向的数组元素拷贝给新结点的数据域*/
s->link=p->link; /*把新结点的链域指向原来p结点的后继结点*/
p->link=s; /*p结点的链域指向新结点*/
}
void del(stud *x,stud *y) /*删除函数,其中y为要删除的结点的指针,x为要删除的结点的前一个结点的指针*/
{
stud *s;
s=y;
x->link=y->link;
free(s);
}
void print(stud *h)
{
stud *p;
p=h->link;
printf("数据信息为:\n");
while(p!=NULL)
{
printf("%s \n",&*(p->name));
p=p->link;
}
}
void quit()
{
exit(0);
}
void menu(void)
{
system("cls");
printf("\t\t\t单链表C语言实现实例\n");
printf("\t\t|----------------|\n");
printf("\t\t| |\n");
printf("\t\t| [1] 建 立 新 表 |\n");
printf("\t\t| [2] 查 找 数 据 |\n");
printf("\t\t| [3] 插 入 数 据 |\n");
printf("\t\t| [4] 删 除 数 据 |\n");
printf("\t\t| [5] 打 印 数 据 |\n");
printf("\t\t| [6] 退 出 |\n");
printf("\t\t| |\n");
printf("\t\t| 如未建立新表,请先建立! |\n");
printf("\t\t| |\n");
printf("\t\t|----------------|\n");
printf("\t\t 请输入你的选项(1-6):");
}
main()
{
int choose;
stud *head,*searchpoint,*forepoint;
char fullname[20];
while(1)
{
menu();
scanf("%d",&choose);
switch(choose)
{
case 1:
head=creat(N);
break;
case 2:
printf("输入你所要查找的人的姓名:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
printf("你所查找的人的姓名为:%s",*&searchpoint->name);
printf("\n按回车键回到主菜单。");
getchar();getchar();
break;
case 3: printf("输入你要在哪个人后面插入:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
printf("你所查找的人的姓名为:%s",*&searchpoint->name);
insert(searchpoint);
print(head);
printf("\n按回车键回到主菜单。");
getchar();getchar();
break;
case 4:
print(head);
printf("\n输入你所要删除的人的姓名:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
forepoint=search2(head,fullname);
del(forepoint,searchpoint);
break;
case 5:
print(head);
printf("\n按回车键回到主菜单。");
getchar();getchar();
break;
case 6:quit();
break;
default:
printf("你输入了非法字符!按回车键回到主菜单。");
system("cls");
menu();
getchar();
}
}
}
*管理学生的个人信息及各科成绩;
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node Node;
//定义成绩信息节点
//分别为语文、数学、英语和总成绩;
struct Score
{
int chinese,math,english,sum;
};
//定义学生信息节点
//分别为姓名、班级、学号、成绩和指向下一个节点的指针
//定义了4个全局变量,头节点,和临时节点变量;
struct Node
{
char name[20],classs[20],number[20];
struct Score score;
struct Node* next;
}*head,*u,*p,*q;
//定义多个学生的学生个数及各科平均成绩优秀率及格率;
int n,C,M,E,Cj,Cy,Mj,My,Ej,Ey;
char num[20];
//进入菜单函数
void Welcome()
{
printf("\t\t # # # # # # # # # # # # # # # # #\n");
printf("\t\t # 欢迎您使用学生成绩管理系统 #\n");
printf("\t\t # #\n");
printf("\t\t # 1.读取文件 #\n");
printf("\t\t # #\n");
printf("\t\t # 2.保存文件 #\n");
printf("\t\t # #\n");
printf("\t\t #   以上是关于c语言链表和指针的运用的主要内容,如果未能解决你的问题,请参考以下文章