主函数main
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
①主函数main()
②保存单链表函数save()
③重载操作菜单函数load()
④显示单链表内容函数display ()
⑤插入元素函数insert ()
⑥删除元素函数decelt ()
⑦修改元素函数modify()
各函数间关系如下:
#include "stdafx.h"
#include
#include
typedef char ElemType;
struct LNode
{
ElemType data;
struct LNode *next;
};
//***********************************************************置空表setnull()
void setnull(struct LNode **p)
{
*p=NULL;
}
//************************************************************求长度length()
int length(struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL)
{
n++;
q=q->next;
}
return(n);
}
//*************************************************************取结点get()
ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (j
{
q=q->next;j++;
}
if (q!=NULL) /**//*找到了第i个结点*/
return(q->data);
else
{
printf("位置参数不正确!\n");
return NULL;
}
}
//************************************************************按值查找locate()
int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL && q->data!=x) /**//*查找data域为x的第一个结点*/
{
q=q->next;
n++;
}
if (q==NULL) /**//*未找到data域等于x的结点*/
return(-1);
else /**//*找到data域等于x的结点*/
return(n+1);
}
//**********************************************************插入结点insert()
void insert(struct LNode **p,ElemType x,int i)
{
int j=1;
struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode)); /**//*建立要插入的结点s*/
s->data=x;
q=*p;
if (i==1) /**//*插入的结点作为头结点*/
{
s->next=q;
*p=s;
}
else
{
while (j
{
q=q->next;j++;
}
if (j==i-1) /**//*找到了第i-1个结点,由q指向它*/
{
s->next=q->next; /**//*将结点s插入到q结点之后*/
q->next=s;
}
else
printf("位置参数不正确!\n");
}
}
//*********************************************************删除结点del()
void del(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if (i==1) /**//*删除链表的头结点*/
t=q;
*p=q->next;
}
else
{
while (j
{
q=q->next;j++;
}
if (q->next!=NULL && j==i-1) /**//*找到第i-1个结点,由q指向它*/ {
t=q->next; /**//*t指向要删除的结点*/
q->next=t->next; /**//*将q之后的结点删除*/
}
else printf("位置参数不正确!\n");
}
if (t!=NULL) /**//*在t不为空时释放该结点*/
free(t);
}
//********************************************************显示链表display()
void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf("单链表显示:");
if (q==NULL) /**//*链表为空时*/
printf("链表为空!");
else if (q->next==NULL) /**//*链表只有一个结点时*/
printf("%c\n",q->data);
else { /**//*链表存在一个以上的结点时*/