链表实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《数据结构》实验报告二
系别:嵌入式系统工程系班级:嵌入式11003班
学号:11160400314 姓名:孙立阔
日期:2012年4月9日指导教师:申华
一、上机实验的问题和要求:
单链表的查找、插入与删除。设计算法,实现线性结构上的单链表的产生以及元素的查找、插入与删除。具体实现要求:
1.从键盘输入10个字符,产生不带表头的单链表,并输入结点值。
2.从键盘输入1个字符,在单链表中查找该结点的位置。若找到,则显示“找到了”;否
则,则显示“找不到”。
3.从键盘输入2个整数,一个表示欲插入的位置i,另一个表示欲插入的数值x,将x插
入在对应位置上,输出单链表所有结点值,观察输出结果。
4.从键盘输入1个整数,表示欲删除结点的位置,输出单链表所有结点值,观察输出结果。
5.将单链表中值重复的结点删除,使所得的结果表中个结点值均不相同,输出单链表所有
结点值,观察输出结果。
6.删除其中所有数据值为偶数的结点,输出单链表所有结点值,观察输出结果。
7.(★)将单链表分解成两个单链表A和B,使A链表中含有原链表中序号为奇数的元
素,而B链表中含有原链表中序号为偶数的元素,且保持原来的相对顺序,分别输出单链表A和单链表B的所有结点值,观察输出结果。
二、程序设计的基本思想,原理和算法描述:
(包括程序的结构,数据结构,输入/输出设计,符号名说明等)
创建一个空的单链表,实现对单链表的查找,插入,删除的功能。
三、源程序及注释:
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define List_Init_Size 10
#define ListIncrement 2
typedef char ET;
typedef ET * Ep;
typedef int Status;
typedef struct LNode{
ET data;
struct LNode *next;
}LNode, *LinkList;
/*LinkList La,Lb,Lc;*/
#include "stdio.h"
#include "alloc.h"
/*Display the linklist's elements. */
void printlk(LinkList L) {
LinkList p;
p=L->next;
while (p) {
printf("%c -> ",p->data);
p = p->next;
}
printf("NULL\n");
}
/*Creat linklist from head node. */
void CreatList( LinkList *L,int n){
int i;
LinkList p,q;
ET str[20],c;
p=(LinkList)malloc(sizeof(LNode)); p->next=NULL;
*L = q = p;
printf("Please input the data : ");
for (i=n;i>0;i--) {
p=(LinkList)malloc(sizeof(LNode));
c = getche(); /*scanf("%c",&c);*/
printf("\n\n");
p->data = c;
p->next = q->next;
q->next = p;
}
}
/*Init the linklist. */
void Init(LinkList *L) {
int n;
printf("Please input the number of the node : "); scanf("%d",&n);
CreatList(L,n);
}
/* Get the value of element I; */
int GetElem(LinkList L,int i,ET *e) {
int j=1;
LinkList p;
p=L->next;
while(p&&j
p=p->next;
++j;
}
if(!p||j>i) return TRUE;
*e=p->data;
return FALSE;
}
/*Insert a element after I */
int ListInsert(LinkList *L,int i,ET e) {
/* Add your own codes. */
}
/*Delete the element I */
int ListDelete(LinkList *L,int i,ET *e)
{
/* Add your own codes. */
}
int Insert(LinkList *L) {
int i,flag;
ET data;
printf("Please input the position : ");
scanf("%d",&i);
printf("Please input the data : ");
data = getche(); /*scanf("%c",&data);*/