数据结构单链表应用
来源:互联网 发布:linux rpm安装命令 编辑:程序博客网 时间:2024/05/24 22:44
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct {
char sno[4];
char sname[10];
int sage;
char sdept[3];
}StuType;
typedef StuType ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode, *List;
Status InitList(List &L){
L = (List)malloc(sizeof(LNode));
if (!L) exit(OVERFLOW);
L->next = NULL;
return OK;
}
Status ListInsert(List &L, int i, ElemType e) {
LNode *p = L; int j = 0;
while (p && j < i-1)
{ p = p->next; ++j; }
if (!p || j > i-1)
return ERROR;
LNode *s = (List) malloc ( sizeof (LNode));
s->data = e;
s->next = p->next; p->next = s;
return OK;
}
Status ListDelete(List &L, int i, ElemType &e){
LNode *p = L; int j = 0;
while (p->next && j < i-1)
{ p = p->next; ++j; }
if (!(p->next) || j > i-1)
return ERROR;
LNode *q = p->next; p->next = q->next;
e = q->data; free(q);
return OK;
}
int LocateElem(List L, ElemType e, Status (*compare)(ElemType, ElemType)){
LNode *p=L->next;
int i=1;
while (p && !(*compare)(p->data, e)) {
p=p->next;
++i;
}
if (p)
return i;
else
return 0;
}
Status PutElem( List&L, int i,ElemType &e )
{
ElemType tmp;
LNode *p = L->next; int j = 1;
while (p && j<i) { p = p->next; ++j; }
if ( !p || j>i )
return ERROR;
tmp=p->data;
p->data =e;
e=tmp;
return OK;
}
Status GetElem(List L, int i, ElemType &e){
LNode *p;
p = L->next; int j = 1
while (p && j<i) { p = p->next; ++j; }
if ( !p || j>i )
return ERROR;
e = p->data;
return OK;
}
bool ListEmpty(List L )
{
if (!L->next)
return true;
else
return false;
}
Status ListTraverse(List L, Status (* visit)(ElemType)){
LNode *p;
p = L->next;
while (p) {
if (!visit(p->data)) return ERROR;
p=p->next;
}
return OK;
}
int ListLength(List L )
{
LNode *p=L;
int i=0;
while (p->next)
{
p=p->next;
++i;
}
return i;
}
Status equal (ElemType a,ElemType b){
int i;
for(i=0;(a.sno[i]!='\0')&&(b.sno[i]!='\0');i++){
if(a.sno[i]!=b.sno[i]){
return ERROR;
}
}
return OK;
}
void visit(ElemType e){
printf("%s\t%s\t%d\t%s\n",e.sno,e.sname,e.sage,e.sdept);
}
void main()
{
List L;
ElemType stu,stu_tmp;
int func,pos;
if (!InitList(L))
{
printf("overflow!\n");
exit(-1);
}
for (int j=0;;j++)
{
printf("Please select the function:\n0--Exit\n1--insert student\n2--delete student\n3--update student name\n4--search student with sno\n5--show all student\n6--count the student\n Please input:");
scanf("%d",&func);
switch (func)
{
case 0:
exit(1);
break;
case 1: //插入学生信息
printf("sno: ");
scanf("%s", &stu.sno);
printf("sname: ");
scanf("%s", &stu.sname);
printf("sage: ");
scanf("%d", &stu.sage);
printf("sdept: ");
scanf("%s", &stu.sdept);
ListInsert(L,ListLength(L)+1,stu);
break;
case 2://删除指定学号的学生信息
printf("input the sno:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
ListDelete(L,pos,stu);
else
printf("The student is not exist!\n");
break;
case 3://更新指定学号的学生姓名
printf("input the sno of the student to be updated:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
{
printf("input the new name:");
scanf("%s",&stu.sname);
GetElem(L,pos,stu_tmp);
stu.sage=stu_tmp.sage;
strcpy(stu.sdept,stu_tmp.sdept);
PutElem(L,pos,stu);
}
else
printf("The student is not exist!\n");
break;
case 4://查找指定学号的学生信息
printf("input the sno:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
{
GetElem(L,pos,stu_tmp);
printf("%s\t%s\t%d\t%s\n",stu_tmp.sno,stu_tmp.sname,stu_tmp.sage,stu_tmp.sdept);
}
else
printf("The student is not exist!\n");
break;
case 5: //输出所有学生信息
ListTraverse(L,visit);
break;
case 6: //输出学生数量
printf("The student count is %d\n",ListLength(L));
break;
}
}
}
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct {
char sno[4];
char sname[10];
int sage;
char sdept[3];
}StuType;
typedef StuType ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode, *List;
Status InitList(List &L){
L = (List)malloc(sizeof(LNode));
if (!L) exit(OVERFLOW);
L->next = NULL;
return OK;
}
Status ListInsert(List &L, int i, ElemType e) {
LNode *p = L; int j = 0;
while (p && j < i-1)
{ p = p->next; ++j; }
if (!p || j > i-1)
return ERROR;
LNode *s = (List) malloc ( sizeof (LNode));
s->data = e;
s->next = p->next; p->next = s;
return OK;
}
Status ListDelete(List &L, int i, ElemType &e){
LNode *p = L; int j = 0;
while (p->next && j < i-1)
{ p = p->next; ++j; }
if (!(p->next) || j > i-1)
return ERROR;
LNode *q = p->next; p->next = q->next;
e = q->data; free(q);
return OK;
}
int LocateElem(List L, ElemType e, Status (*compare)(ElemType, ElemType)){
LNode *p=L->next;
int i=1;
while (p && !(*compare)(p->data, e)) {
p=p->next;
++i;
}
if (p)
return i;
else
return 0;
}
Status PutElem( List&L, int i,ElemType &e )
{
ElemType tmp;
LNode *p = L->next; int j = 1;
while (p && j<i) { p = p->next; ++j; }
if ( !p || j>i )
return ERROR;
tmp=p->data;
p->data =e;
e=tmp;
return OK;
}
Status GetElem(List L, int i, ElemType &e){
LNode *p;
p = L->next; int j = 1
while (p && j<i) { p = p->next; ++j; }
if ( !p || j>i )
return ERROR;
e = p->data;
return OK;
}
bool ListEmpty(List L )
{
if (!L->next)
return true;
else
return false;
}
Status ListTraverse(List L, Status (* visit)(ElemType)){
LNode *p;
p = L->next;
while (p) {
if (!visit(p->data)) return ERROR;
p=p->next;
}
return OK;
}
int ListLength(List L )
{
LNode *p=L;
int i=0;
while (p->next)
{
p=p->next;
++i;
}
return i;
}
Status equal (ElemType a,ElemType b){
int i;
for(i=0;(a.sno[i]!='\0')&&(b.sno[i]!='\0');i++){
if(a.sno[i]!=b.sno[i]){
return ERROR;
}
}
return OK;
}
void visit(ElemType e){
printf("%s\t%s\t%d\t%s\n",e.sno,e.sname,e.sage,e.sdept);
}
void main()
{
List L;
ElemType stu,stu_tmp;
int func,pos;
if (!InitList(L))
{
printf("overflow!\n");
exit(-1);
}
for (int j=0;;j++)
{
printf("Please select the function:\n0--Exit\n1--insert student\n2--delete student\n3--update student name\n4--search student with sno\n5--show all student\n6--count the student\n Please input:");
scanf("%d",&func);
switch (func)
{
case 0:
exit(1);
break;
case 1: //插入学生信息
printf("sno: ");
scanf("%s", &stu.sno);
printf("sname: ");
scanf("%s", &stu.sname);
printf("sage: ");
scanf("%d", &stu.sage);
printf("sdept: ");
scanf("%s", &stu.sdept);
ListInsert(L,ListLength(L)+1,stu);
break;
case 2://删除指定学号的学生信息
printf("input the sno:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
ListDelete(L,pos,stu);
else
printf("The student is not exist!\n");
break;
case 3://更新指定学号的学生姓名
printf("input the sno of the student to be updated:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
{
printf("input the new name:");
scanf("%s",&stu.sname);
GetElem(L,pos,stu_tmp);
stu.sage=stu_tmp.sage;
strcpy(stu.sdept,stu_tmp.sdept);
PutElem(L,pos,stu);
}
else
printf("The student is not exist!\n");
break;
case 4://查找指定学号的学生信息
printf("input the sno:");
scanf("%s",&stu.sno);
pos=LocateElem(L,stu,equal);
if (pos>0)
{
GetElem(L,pos,stu_tmp);
printf("%s\t%s\t%d\t%s\n",stu_tmp.sno,stu_tmp.sname,stu_tmp.sage,stu_tmp.sdept);
}
else
printf("The student is not exist!\n");
break;
case 5: //输出所有学生信息
ListTraverse(L,visit);
break;
case 6: //输出学生数量
printf("The student count is %d\n",ListLength(L));
break;
}
}
}
阅读全文
0 0
- 数据结构单链表应用
- 数据结构单链表的简单应用
- 数据结构 单链表应用:回溯法求幂集
- 数据结构实习题-单链表的应用
- 数据结构 单链表的应用:多项式及其运算
- 数据结构的基本概念 单链表的应用
- 数据结构例程——单链表应用举例
- 数据结构算法——单链表的应用
- 数据结构基础:单链表的应用(1)
- 数据结构第四周项目3--单链表应用
- 数据结构的应用
- 数组应用[数据结构课程设计]
- 数据结构的泡妞应用
- 数据结构的泡妞应用
- 数据结构的简单应用
- 【数据结构】堆的应用
- 游戏中的数据结构应用
- 数据结构栈的应用
- HDU 5718 Oracle(大数)
- 12cR2 下手工建库 CDB
- 数论[模板]
- 二叉搜索树的操作集(30 分)
- 线程池
- 数据结构单链表应用
- Git简单入门
- 纵向拆分和横向拆分
- HTTP简介
- LeetCode-667:Beautiful Arrangement II (数组的完美安排) -- medium
- JavaScript中的try...catch语句和es5的严格模式
- 【云星数据---mesos实战系列002】:mesos全分布式部署实战001--机器准备
- oracle常用函数小结(一)
- 笨办法学python17 更多文件操作