今天课上我们老师为我们讲述了c语言的指针、链表的原理以及各类操作。
一、指针
1、指针
指针是一个存储计算机内存地址的变量。从指针指向的内存读取数据称作指针的取值。指针可以指向某些具体类型的变量地址,例如int、long和double。指针也可以是void类型、NULL指针和未初始化指针。指针是一个存储计算机内存地址的变量。从指针指向的内存读取数据称作指针的取值。指针可以指向某些具体类型的变量地址,例如int、long和double。指针也可以是void类型、NULL指针和未初始化指针。
2、数组
C语言的数组表示一段连续的内存空间,用来存储多个特定类型的对象。数组和指针不是同一种结构因此不可以互相转换。而数组变量指向了数组的第一个元素的内存地址。
3、指针与结构体
就像数组一样,指向结构体的指针存储了结构体第一个元素的内存地址。与数组指针一样,结构体的指针必须声明和结构体类型保持一致,或者声明为void类型。
我们在使用c语言进行编写程序的时候,就一定会使用上指针,指针的操作是编写程序的一个点。如何更好的使用指针,将是我们进行更好的书写程序的关键。我认为弄清楚下面几个要点,可以更好的理解指针:
1:指针是一个地址,指向的是个类型
2:指针指向的是地址,地址指向的是内容
3:指针的指针,是面对于指针的变量
二、链表
链表我的理解要包含以下特征:(1).由n个节点离散分配;(2).每个节点通过指针连接(3)每一个节点由一个前驱节点和一个后驱节点(4).首节点没有前驱节点,尾节点没有后驱节点;
满足上面的4条,我们就称为链表;链表既然由很多个节点,那节点又由什么组成?节点由两个部分组成,一是数据域,用来存放有效数据;二是指针域,用来指向下一个节点;下面用C语言来构建链表数据结构,首先应该构造出节点,然后再把所有的节点连起来,就构成了链表;
我认为学生宿舍的管理系统中,指针的作用就是使其保存到下一个地址
下面是单链表的学生信息管理系统
#include "string.h"
#include "sys/malloc.h"
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
#include "sys/types.h"
#define MAXSIZE 100
typedef struct Student
{
char sname[9];
char sno[5];
int score;
}DataType;
typedef struct
{
DataType data;
struct Node *next;
}LinkList;
LinkList * inputdata();
void display(LinkList * p);
//遍历链表
void displayAll(LinkList * L);
//插入
LinkList * createTailList();//尾插
LinkList * createHeadList();//头插
//查询
void getElem(LinkList * L, int i);//按序号
void locateElemBysno(LinkList * L, char ch[]);//按值
void locateElemBysname(LinkList * L, char ch[]);
int lengthList(LinkList * L);
//插入
void insertElem(LinkList * L, int i);//在第i个结点前进行插入
void insertElemBysno(LinkList * L, char ch[5]);//按学号
void deleteElem(LinkList * L, char ch[]);
//排序
void insertSort(LinkList * L);
int menue();
int main(int argc,char *argv[])
{
LinkList *L;
char sno[5] = {‘\0‘};
char sname[9] = {‘\0‘};
int b = 1;
int i = 1;
while (b) {
switch(menue())
{
case 1:
L = createTailList();
//L = createHeadList();
break;
case 2:
//displayAll(L);
printf("\t◎输入插入哪个位置前:");
scanf("%d", &i);
insertElem(L, i);
//scanf("%s", sno);
//insertElemBysno(L, sno);
break;
case 3:
printf("\t◎输入待删除学生的学号:");
scanf("%s", sno);
deleteElem(L, sno);
break;
case 4:
printf("╭═══════════════════════════════╮\n");
printf("║\t学生总数为:%d\t\t║\n", lengthList(L));
printf("╰═══════════════════════════════╯\n\n");
break;
case 5:
printf("\t◎输入待查找学生的学号:");
scanf("%s", sno);
locateElemBysno(L, sno);
break;
case 6:
printf("\t◎输入待查学生的位置:");
scanf("%d", &i);
getElem(L, i);
break;
case 7:
displayAll(L);
break;
case 8:
insertSort(L);
break;
case 9:
i = 2;
while (i)
{
system("clear");
printf("\033[5m");
printf("\033[?25l"); //隐藏光标
printf("\n\n\n\n\n\n");
printf("╭═══════════════════════════════╮\n");
printf("║\t正在退出(%d秒)\t\t║\n", i--);
printf("╰═══════════════════════════════╯\n");
sleep(1);
}
printf("\033[0m");
system("clear");
return 0;
break;
default:
i = 2;
while (i)
{
system("clear");
printf("╭═══════════════════════════════╮\n");
printf("║操作数无效,正在返回主菜单(%d秒)║\n", i--);
printf("╰═══════════════════════════════╯\n");
sleep(1);
}
continue;
}
getchar();
printf("PRESS ENTER TO CONTINUE!");
while (1) {
if (‘\n‘ == getchar())
{
break;
}
}
}
return 0;
}
int menue()
{
system("clear");
//警告音
printf("\033[0m"); //关闭所有属性
printf("\033[44;37m"); //47是字背景颜色,33是字体的颜色
printf ("\033[5m"); //闪烁
printf("\n^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^\n\n");
printf("\033[0m"); //关闭所有属性
printf("\033[44;37m");
printf("╭═══════════════════════════════╮\n");
printf("║\t学生成绩管理程序\t║\n");
printf("║\t\t\t\t║\n");
printf("║\t<1>创建\t\t\t║\n");
printf("║\t<2>指定位置后插入\t║\n");
printf("║\t<3>按位置删除\t\t║\n");
printf("║\t<4>求学生总数\t\t║\n");
printf("║\t<5>按学号查找\t\t║\n");
printf("║\t<6>按位置查找\t\t║\n");
printf("║\t<7>显示所有学生\t\t║\n");
printf("║\t<8>成绩排序\t\t║\n");
printf("║\t<9>退出\t\t\t║\n");
printf("╰═══════════════════════════════╯\n\n");
printf ("\033[5m");
printf("^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^\n\n");
printf("\033[0m"); //关闭所有属性
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
//printf ( "\t\007%s", asctime (timeinfo) );
printf ("\033[;34m");
printf ("\t[%4d-%02d-%02d %02d:%02d:%02d]\n",
1900+timeinfo->tm_year,
1+timeinfo->tm_mon,
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec);
char colorname[3][20] = {{"RED"}, {"BLUE"}, {"YELLLOW"}};
srand(time(&rawtime)); //时间触发
//textcolor(colorname[rand()%3]);
//printf("\t[textcolor is %s]\n", colorname[rand()%3]);
//颜色码和控制码 我的参考链接 http://wenku.baidu.com/view/a38f77ff910ef12d2af9e70b.html
printf("\t◎输入功能项:");
int a = 0;
scanf("%d",&a);
printf("\033[0m");
system("clear");
return a;
}
LinkList * inputdata()
{
LinkList *s = NULL;
char sno[5] = {‘\0‘};
char sname[9] = {‘\0‘};
int score = 0;
printf("\tsno\t->");
scanf("%s", sno);
if (sno[0] == ‘#‘) {
return s;
}
s = (LinkList *)malloc(sizeof(LinkList));
strcpy(s -> data.sno, sno);
printf("\tsname\t->");
scanf("%s", sname);
strcpy(s -> data.sname, sname);
printf("\tscore\t->");
scanf("%d", &score);
s -> data.score = score;
printf("\n");
return s;
}
void display(LinkList * p)
{
printf("╭═══════════════════════════════════════════════╮\n");
printf("║\tsno\t\tsname\t\tscore\t║\n");
printf("║\t%s\t\t%s\t\t%-4d\t║\n", p->data.sno, p->data.sname, p->data.score);
printf("╰═══════════════════════════════════════════════╯\n\n");}
void displayAll(LinkList * L)
{
printf("╭═══════════════════════════════════════════════╮\n");
printf("║\tsno\t\tsname\t\tscore\t║\n");
LinkList * p = L -> next;
while(p)
{
printf("║\t%s\t\t%s\t\t%-4d\t║\n", p->data.sno, p->data.sname, p->data.score);
p = p -> next;
}
printf("╰═══════════════════════════════════════════════╯\n\n");
}
LinkList * createTailList()
{
//链表头结点
LinkList * L = (LinkList *)malloc(sizeof(LinkList));
//结点
LinkList * s = NULL;
//尾结点
LinkList * r = L;
printf("╭═══════════════════════════════════════════════╮\n");
printf("║\t\t尾插法建立\t\t\t║\n");
printf("║\t请输入学生信息(当学号为\"#\"时结束)\t║\n");
printf("║\t\t\t\t\t\t║\n");
printf("║\t学号sno (不超过4位)\t\t║\n");
printf("║\t姓名sname (不超过4个汉字)\t\t║\n");
printf("║\t成绩score (int型)\t\t\t║\n");
printf("╰═══════════════════════════════════════════════╯\n\n");
while (1) {
s = inputdata();
if (!s) {
break;
}
r->next = s;
r = s;
}
r->next = NULL;
return L;
}
LinkList * createHeadList()
{
//链表头结点
LinkList * L = (LinkList *)malloc(sizeof(LinkList));
//结点
LinkList * s = NULL;
printf("╭═══════════════════════════════════════════════╮\n");
printf("║\t\t头插法建立\t\t║\n");
printf("║\t请输入学生信息(当学号为\"#\"时结束)\t║\n");
printf("║\t\t\t\t\t\t║\n");
printf("║\t学号sno (不超过4位)\t\t║\n");
printf("║\t姓名sname (不超过4个汉字)\t\t║\n");
printf("║\t成绩score (int型)\t\t\t║\n");
printf("╰═══════════════════════════════════════════════╯\n\n");
while (1) {
s = inputdata();
if (!s) {
break;
}
s->next = L->next;
L->next = s;
}
return L;
}
void getElem(LinkList * L, int i)
{
LinkList * p = L;
int j = 0;
while (p && j<i) {
p = p->next;
j++;
}
if (!p) {
printf("╭═══════════════════════════════╮\n");
printf("║\tDon‘t find the student!\t║\n");
printf("╰═══════════════════════════════╯\n\n");
}
else
{
display(p);
}
}
void locateElemBysno(LinkList * L, char ch[5])
{
LinkList * p = L->next;
while (p && (0 != strcmp(p->data.sno, ch)))
{
p = p -> next;
}
if (!p) {
printf("╭═══════════════════════════════╮\n");
printf("║\tDon‘t find the student!\t║\n");
printf("╰═══════════════════════════════╯\n\n");
}
else
{
display(p);
}
}
void locateElemBysname(LinkList * L, char ch[9])
{
LinkList * p = L->next;
while (p && (0 != strcmp(p->data.sname, ch)))
{
p = p -> next;
}
if (!p) {
printf("╭═══════════════════════════════╮\n");
printf("║\tDon‘t find the student!\t║\n");
printf("╰═══════════════════════════════╯\n\n");
}
else
{
display(p);
}
}
int lengthList(LinkList * L)
{
LinkList * p = L->next;
int j = 0;
while (p) {
p = p->next;
j ++;
}
return j;
}
void insertElem(LinkList * L, int i)
{
LinkList * s = inputdata();
LinkList * p = L;
int j = 0;
while (p && j<i-1)
{
p = p->next;
j++;
}
if (p && p->next)
{
s->next = p->next;
p->next = s;
}
else
{
printf("╭═══════════════════════════════╮\n");
printf("║\tDon‘t find the student!\t║\n");
printf("╰═══════════════════════════════╯\n\n");
}
displayAll(L);
}
void insertElemBysno(LinkList * L, char ch[5])
{
LinkList * p = L;
LinkList * s = NULL;
while (p && (0 != strcmp(p->data.sno, ch)))
{
p = p->next;
}
if (!p) {
printf("╭═══════════════════════════════╮\n");
printf("║\tDon‘t find the student!\t║\n");
printf("╰═══════════════════════════════╯\n\n");
}
else
{
s = inputdata();
s->next = p->next;
p->next = s;
}
}
void deleteElem(LinkList * L, char ch[5])
{
LinkList *p, *q;
p = L->next;
q=L;
while (p && (strcmp(p->data.sno, ch) != 0)) {
q = p;
p = p->next;
}
if (!p) {
printf("╭═══════════════════════════════╮\n");
printf("║\tDon‘t find the student!\t║\n");
printf("╰═══════════════════════════════╯\n\n");
}
else
{
display(p);
q->next = p->next;
free(p);
}
}
void insertSort(LinkList * L)
{
LinkList * L1;
LinkList * p;
LinkList * q;
LinkList * s;
int len;
len = lengthList(L);
L1 = (LinkList *)malloc(sizeof(LinkList));
if (L->next) {
s = (LinkList *)malloc(sizeof(LinkList));
strcpy(s->data.sno, L->data.sno);
strcpy(s->data.sname, L->data.sname);
s->data.score = L->data.score;
s->next = NULL;
L1->next = s;
q = L->next;
}
else
{
printf("╭═══════════════════════════════════╮\n");
printf("║\tThe student link list is empty!\n║\n");
printf("╰═══════════════════════════════════╯\n\n");
return;
}
while (q) {
p = L1->next;
while (p &&(p->data.score >= q->data.score))
{
p = p->next;
}
s = (LinkList *)malloc(sizeof(LinkList));
strcpy(s->data.sno, q->data.sno);
strcpy(s->data.sname, q->data.sname);
s->data.score = q->data.score;
if (!p) {
s->next = NULL;
p->next = s;
}
else
{
s->next = p->next;
p->next = s;
}
q = q->next;
}
displayAll(L1);
}