线性表(链式存储结构)c语言
这里写目录标题
这里写目录标题
前言
,待更新
先上传代码部分
代码部分
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
typedef struct MyData //定义结构体类型说明符DATA
{
char Name[20]; //姓名
int Snum; //学号
}DATA;
typedef struct Node //定义结构体类型说明符LsListType
{
DATA data; //数据域
struct Node* next; //指针域
}LsListType;
int LsListNum(LsListType* head) //查看链表节点数量
{
LsListType* h;
h = head;
int i = 0;
while (h)
{
i++;
h = h->next;
}
return i;
}
LsListType* LsListAddEnd(LsListType* head, DATA data) //添加节点到链表尾部
{
LsListType* Node, * h;
Node = (LsListType*)malloc(sizeof(LsListType)); //为节点申请动态内存
if (Node == NULL)
{
printf("动态内存申请失败!");
return NULL;
}
Node->data = data;
Node->next = NULL;
if (head == NULL) //头指针不知向下一个节点(即链表为空)
{
head = Node;
return head;
}
h = head;
while (h->next != NULL) //寻找链表中最后一个节点
{
h = h->next;
}
h->next = Node;
return head;
}
LsListType* LsListAddFirst(LsListType* head , DATA data) //添加节点到链表头部
{
LsListType* Node;
Node = (LsListType*)malloc(sizeof(LsListType)); //动态内存申请
if (Node == NULL)
{
printf("动态内存申请失败!");
return NULL;
}
Node->data = data;
Node->next = head;
head = Node;
return head;
}
void LsListPrint(LsListType* head) //遍历链表
{
LsListType* h;
h = head;
while (h)
{
printf("%s\t%d\n",h->data.Name,h->data.Snum);
h = h->next;
}
}
LsListType* LsListDelete(LsListType* head , char name[]) //删除节点
{
static LsListType* h, * m;
h = head;
int i = 0;
if (h == NULL)
{
printf("删除不成功,未找到该节点!\n");
return head;
}
while (strcmp(h->data.Name, name) != 0)
{
m = h;
h = h->next;
i++;
}
if (i == 0)
{
h = h->next;
head = h;
return head;
}
m->next = h->next;
free(h);
return head;
}
LsListType* LsListInsert(LsListType* head, DATA data, char name[]) //插入节点
{
LsListType* h, * Node;
static LsListType* m;
int i = 0;
Node = (LsListType*)malloc(sizeof(LsListType));
Node->data = data;
h = head;
if (!name)
{
printf("删除失败!\n");
return head;
}
while (strcmp(h->data.Name, name) != 0)
{
h = h->next;
m = h->next;
i++;
}
if (h->next == NULL || h == NULL)
{
printf("无法插入!\n");
free(Node);
return head;
}
if (i == 0)
{
m = h;
h = h->next;
Node->next = h;
m->next = Node;
return head;
}
Node->next = m;
h->next = Node;
return head;
}
void LsListFindName(LsListType* head , char name[]) //姓名查找
{
LsListType* h;
h = head;
int i = 0;
while (h)
{
if (strcmp(h->data.Name, name) == 0)
{
printf("%s\t%d\n", h->data.Name, h->data.Snum);
i++;
}
h = h->next;
}
if (i == 0)
{
printf("未找到该节点!\n");
}
}
void LsListFindNum(LsListType* head, int stuN) //学号查找
{
LsListType* h;
h = head;
int i = 0;
while (h)
{
if (h->data.Snum == stuN)
{
printf("%s\t%d\n", h->data.Name, h->data.Snum);
i++;
}
h = h->next;
}
if (i== 0)
{
printf("未找到该节点!\n");
}
}
int main()
{
printf("1.查看元素数量\n");
printf("2.添加\n");
printf("3.插入\n");
printf("4.删除\n");
printf("5.学号查找\n");
printf("6.姓名查找\n");
printf("7.遍历\n");
printf("8.结束\n");
static LsListType *head; //头指针
static DATA data; //储存数据
int num; //序号
char name[20]; //姓名
int stuN = 0; //学号
while (1)
{
printf("请输入需要进行的操作:");
while (1)
{
rewind(stdin); //清除缓存
scanf("%d",&num);
if (num < 1 || num >8 )
{
printf("输入有误,请重新输入:");
}
else
{
break;
}
}
switch (num)
{
case 1:
printf("链表中节点数量为%2d\n", LsListNum(head));
break;
case 2:
num = 0;
printf("1.添加至尾部\n");
printf("2.添加至头部\n");
printf("请输入序号:");
while (1)
{
rewind(stdin); //清除缓存
scanf("%d", &num);
if (num < 1 || num >2)
{
printf("输入有误,请重新输入:");
}
else
{
break;
}
}
printf("请输入姓名、学号:");
scanf("%s",data.Name);
scanf("%d",&data.Snum);
if (num == 1)
{
head = LsListAddEnd(head,data);
}
if(num == 2)
{
head = LsListAddFirst(head,data);
}
break;
case 3:
if (head == NULL)
{
printf("链表中无节点,无法插入!\n");
break;
}
if (LsListNum(head) < 2)
{
printf("无法插入!\n");
break;
}
printf("在哪位同学后面插入:");
rewind(stdin);
gets(name);
printf("请输入要插入的学生的姓名、学号:");
scanf("%s", data.Name);
scanf("%d", &data.Snum);
head = LsListInsert(head, data, name);
break;
case 4:
if (head == NULL)
{
printf("链表中无节点,无法删除!\n");
break;
}
printf("请输入要删除节点的姓名:");
rewind(stdin);
gets(name);
head = LsListDelete(head,name);
break;
case 5:
if (head == NULL)
{
printf("链表为空!\n");
break;
}
printf("请输入要查找的学号:");
rewind(stdin);
scanf("%d",&stuN);
LsListFindNum(head,stuN);
stuN = 0; //防止出错
break;
case 6:
if (head == NULL)
{
printf("链表为空!\n");
break;
}
printf("请输入要查找的姓名:");
rewind(stdin);
gets(name);
LsListFindName(head,name);
break;
case 7:
if (head == NULL)
{
printf("链表为空!\n");
break;
}
LsListPrint(head);
break;
case 8:
exit(0);
break;
}
num = 0;
}
system("pause");
return 0;
}