Problem D: 链表的基本运算(线性表)
2016-03-26 23:38
357 查看
128 MB
Submit: 19 Solved: 11
[Submit][Status][Web
Board]
Description
编写一个程序,实现链表的各种基本运算(假设顺序表的元素类型为char),主函数已给出,请补充每一种方法。1、初始化单链表L;
2、采用尾插法依次插入元素a,b,c,d,e;
3、输出单链表L;
4、输出单链表L的长度;
5、判断单链表L是否为空;
6、输出单链表L的第三个元素;
7、输出元素a的位置;
8、在第四个元素位置插入元素f;
9、输出单链表L;
10、删除L的第三个元素;
11、输出单链表L;
12、释放单链表L;
数据元素类型定义为
typedef char ElemType;
顺序表的定义为
typedef struct Node
{
ElemType data;
struct Node *next;
} SqList;
主函数:
int main()
{
SqList *L;
InitList(L); //初始化单链表
ElemType a,b,c,d,e;
scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);
Insert(L,a);
Insert(L,b);
Insert(L,c);
Insert(L,d);
Insert(L,e); //使用尾插法插入元素a,b,c,d,e
Print(L); //输出单链表
PrintLength(L); //输出单链表长度
if(SqNull(L))
printf("单链表不为空\n");
else printf("单链表为空\n"); //判断单链表是否为空
PrintData(L,3); //输出第三个元素
printf("元素a的位置:%d\n",Find(L,a)); //输出元素a的位置
ElemType f;
scanf("%c",&f);
Insertinto(L,4,f); //将f插入到第四个位置
Print(L); //输出单链表
Delete(L,3); //删除第三个元素
Print(L); //输出单链表
free(L); //释放内存
return 0;
}
Input
第一行输入五个元素a,b,c,d,e;接下来输入元素f;请根据题目编写算法。Output
Sample Input
1 2 3 4 5 6
Sample Output
1 2 3 4 5 5 单链表不为空 3 元素a的位置:1 1 2 3 6 4 5 1 2 6 4 5
HINT
请使用C++编译并提交#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
} SqList;
void InitList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList));
L->next=NULL;
}
void Insert(SqList *&L,char x)
{
SqList *p=L,*s;
while(p->next!=NULL)
p=p->next;
s=p;
p=(SqList *)malloc(sizeof(SqList));
p->data=x;
s->next=p;
p->next=NULL;
}
void Print(SqList *&L)
{
SqList *p=L->next;
while(p!=NULL)
{
if(p->next!=NULL)
printf("%c ",p->data);
else
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
bool PrintLength(SqList *&L)
{
int j=0;SqList *p=L;
while(p->next!=NULL)
{
j++;
p=p->next;
}
printf("%d\n",j);
}
bool SqNull(SqList *&L)
{
return(L->next!=NULL);
}
void PrintData(SqList *&L,int n)
{
SqList *p=L;
int j=0;
while(j<n&&p->next!=NULL)
{
p=p->next;
j++;
}
printf("%c\n",p->data);
}
bool Find(SqList *&L,char a)
{
int j=1;
SqList *p=L->next;
while(p->data!=a&&p->next!=NULL)
{
p=p->next;
j++;
}
return j;
}
void Insertinto(SqList *&L,int n,char f)
{
SqList *p=L,*s;
int j=0;
while(j<n-1&&p->next!=NULL)
{
j++;
p=p->next;
}
s=(SqList *)malloc(sizeof(SqList));
s->data=f;
s->next=p->next;
p->next=s;
}
void Delete(SqList *&L,int n)
{
int j=1;
SqList *p=L->next,*q;
while(j<n-1&&p!=NULL)
{
j++;p=p->next;
}
q=p->next;
p->next=q->next;
free(q);
}
int main()
{
SqList *L;
InitList(L); //初始化单链表
ElemType a,b,c,d,e;
scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);
Insert(L,a);
Insert(L,b);
Insert(L,c);
Insert(L,d);
Insert(L,e); //使用尾插法插入元素a,b,c,d,e
Print(L); //输出单链表
PrintLength(L); //输出单链表长度
if(SqNull(L))
printf("单链表不为空\n");
else printf("单链表为空\n"); //判断单链表是否为空
PrintData(L,3); //输出第三个元素
printf("元素a的位置:%d\n",Find(L,a)); //输出元素a的位置
ElemType f;
scanf("%c",&f);
Insertinto(L,4,f); //将f插入到第四个位置
Print(L); //输出单链表
Delete(L,3); //删除第三个元素
Print(L); //输出单链表
free(L); //释放内存
return 0;
}
相关文章推荐
- 二代旅游网站管理程序V1使用手册(四):旅游出发地的管理
- 欢迎Adobe Animate CC 2015, Flash Professional的新时代!
- hdu-5650 so easy(水题)
- 左式堆 斜堆
- java中的BigInteger(很好很强大)(转)
- HDU 5653 (DP)
- How to Flash an SD Card for Raspberry Pi
- 结对编程 一
- linux内核分析 第五周
- Stanford 机器学习笔记 Week9 Recommender Systems
- Python 程序设计语言 笔记(二)
- 单链表的创建算法
- JDK源码分析-ArrayList分析
- 网站被黑检测-网页挂马及暗链检测
- Android中菜单的使用
- JavaScript中数组的操作
- Oracle数据库中的几个名字及监听的配置问题
- 码农小汪-struts2学习10-上传下载文件
- HDU 5653 Bomber Man wants to bomb an Array. dp
- 设计模式--中介者模式C++实现