7-1 jmu-ds-单链表的基本运算 (15 分)
实现单链表的基本运算:初始化、插入、删除、求表的长度、判空、释放。
(1)初始化单链表L,输出L->next的值;
(2)依次采用尾插法插入元素:输入分两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。
(3)输出单链表L;
(4)输出单链表L的长度;
(5)判断单链表L是否为空;
(6)输出单链表L的第3个元素;
(7)输出元素a的位置;
(8)在第4个元素位置上插入‘x’元素;
(9)输出单链表L;
(10)删除L的第3个元素;
(11)输出单链表L;
(12)释放单链表L。
输入格式:
两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。
输出格式:
按照题目要求输出
输入样例:
5
a b c d e
输出样例:
0
a b c d e
5 no
c
1
a b c x d e
a b x d e
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
Status CreatList_L(LinkList &L);
Status LinkListInsert(LinkList &L,int pos,ElemType e);
Status Print_L(LinkList &L);
Status Length_L(LinkList &L);
Status JudgeEmpty_L(LinkList &L);
Status GetVal_L(LinkList &L,int pos);
Status ListDelete(LinkList &L,int pos);
Status Locate_L(LinkList &L,ElemType e);
void ListFree(LinkList &L);
int main()
{
int n;
ElemType e;
LinkList L;
if(CreatList_L(L)==OK)//1、创建单链表
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf(" %c",&e);
LinkListInsert(L,i,e);//2、尾插法
}
Print_L(L);//3、输出单链表
Length_L(L);//4、输出单链表的长度
if(JudgeEmpty_L(L))//5、判断是否为空
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
GetVal_L(L,3);//6、输出单链表中的第三个元素
e='a';//7、输出元素a的位置
Locate_L(L,e);
e='x';//8、在第四个元素位置上插入x元素
LinkListInsert(L,4,e);
Print_L(L);//9、输出单链表
ListDelete(L,3);//10、删除第三个元素
Print_L(L);//11、输出单链表
ListFree(L);//12、释放单链表
}
else
return ERROR;
}
Status CreatList_L(LinkList &L)
{
LNode *curP;
curP=(LNode*)malloc(sizeof(LNode));
if(!curP) return OVERFLOW;
L=curP;
curP->next=NULL;
cout<<0<<endl;
return OK;
}
Status LinkListInsert(LinkList &L,int pos,ElemType e)
{
if(pos<=0) return FALSE;
LNode *curP, *reaP, *tmp;
reaP=L;
curP=L->next;
int num=1;
while(num<pos && curP) //指针移动到插入点
{
curP=curP->next; num++; reaP=reaP->next;
}
if(num<pos) return FALSE;
tmp=(LNode*)malloc(sizeof(LNode));//申请一个新节点暂存插入元素
if(!tmp) return OVERFLOW;
tmp->data=e;
tmp->next=NULL;
tmp->next=curP;
reaP->next=tmp;
return OK;
}
Status Print_L(LinkList &L)
{
LNode *curP;
curP=L->next;
int k=0;
while(curP)
{
if(k==0)
cout<<curP->data;
else
cout<<" "<<curP->data;
curP=curP->next; //因为地址不一定连续 不能用curp++
k++;
}
cout<<endl;
}
Status Length_L(LinkList &L)
{
LNode *curP;
curP=L->next;
int len=0;
while(curP)
{
len++;
curP=curP->next;
}
cout<<len<<endl;
return OK;
}
Status JudgeEmpty_L(LinkList &L)
{
// L=L->next;
// if(L) return TRUE;
// else return FALSE;
return L->next ? FALSE : TRUE;
}
Status GetVal_L(LinkList &L,int pos)
{
if(pos<=0) return FALSE;
LNode *curP;
curP=L->next;
int num=1;
while(curP && num<pos)
{
num++; curP=curP->next;
}
if(num<pos) return FALSE;
cout<<curP->data<<endl;
}
Status ListDelete(LinkList &L,int pos)
{
if(pos<0) return FALSE;
LNode *curP, *reaP;
curP=L->next;
reaP=L;
int num=1;
while(num<pos && curP)
{
curP=curP->next;
reaP=reaP->next;
num++;
}
if(num<pos) return FALSE;
reaP->next=curP->next;
return OK;
}
void ListFree(LinkList &L)
{
LNode *curP, *reaP;
curP=L->next;
reaP=L;
while(curP)
{
free(reaP);
reaP=curP;
curP=curP->next;
}
free(curP);
}
Status Locate_L(LinkList &L,ElemType e)
{
LNode *curP;
curP=L->next;
int num=1;
while(curP && curP->data!=e)
{
curP=curP->next;
num++;
}
if(!curP)
return FALSE;
cout<<num<<endl;
return OK;
}