数据结构 单链表 创建 打印 删除 插入 查询 实验报告

《数据结构》设计型实验报告格式
一实验目的
利用数据结构和C语言所学的相关知识,实现单链表的创建、插入、删除、打印和查询功能。功能上要以菜单选项形式体现出来,可以与用户良好的交互。通过本次设计型实验对前面所学的知识做一总结与回顾。
二实验内容
 单链表的创建(结点结构要求至少两个数据域:学号、姓名)
 单链表的打印
 单链表的插入
 单链表的删除
 单链表的查询
三实验步骤
 程序设计规划(实现的功能、分几个模块、子函数)
 编写单链表创建子函数
 编写单链表打印子函数
 编写单链表插入子函数
 编写单链表删除子函数
 编写单链表查询子函数
 编写主函数Main(),通过功能菜单调用子函数
 编译调试程序
四重点难点
在系统开发中遇到的问题,如何解决的,越具体越好。也可以分析一些重要的源代码。
五系统评价
系统自我评价,实现了哪些功能,是否能正常运行,哪些是自己做的。自己给自己打多少分。
基本功能实现:60分;查询功能实现:80分;良好的用户操作性:90分;自己设计的创新功能:100分。根据不同的代码风格、简洁程度、变量命名规范酌情增减分数。
六心得体会
在完成这次2个学时的设计型实验的过程中,有什么收获、心得和体会,用自己的话说。

1.c++编的
#include <iostream>
using namespace std;

typedef struct node
{
char data;
struct node *next;
}link;

link * get(link *l, int i)
{
link *p;int j=0;
p=l;
while((j<i) && (p->next!=NULL))
{p=p->next;j++;}
if(j==i)
return p;
else
return NULL;
}

link * ins (link *l, char ch,int i)
{ link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=(link *)malloc(sizeof(link));
s->data=ch;
s->next=p->next;
p->next=s;
}
return l;
}

link * find(link *l, char ch)
{
link *p; int i=0; int j=0;
p=l;

while(p!=NULL)
{ i++;
if(p->data!=ch)
p=p->next;
else {cout<<"您查找的数据在第"<<i-1<<"个位置."<<endl;
j=1;p=p->next;
}

}
if(j!=1)
cout<<"您查找的数据不在线性表中."<<endl;
return l;
}

link * del(link *l, int i)
{
link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<<endl;
else
{
s=p->next;
p->next=s->next;
free(s);
}
return l;
}

link * add(link *l )
{
link *p,*s;
cout<<"请输入一串单字符数据,以*结束!"<<endl;
char ch;
link *HEAD;
link *R,*P,*L;
HEAD=(link *)malloc(sizeof(link));
HEAD->next=NULL;
R=HEAD;
getchar();
ch=getchar();
while(ch!='*')
{
P=(link *)malloc(sizeof(link));
P->data=ch;P->next=NULL;
R->next=P;R=R->next;
getchar();
ch=getchar();

}

L=HEAD;
cout<<"当前输入的线性表为:"<<endl;
P=L;P=P->next;
if(L!=NULL)
do
{cout<<P->data<<" ";
P=P->next;
}while(P!=NULL);
cout<<endl;
p=l;
while(p->next!=NULL)
p=p->next;
s=L;
p->next=s->next;
p=l;
return l;
}

link * print(link *l)
{ int i,k;
char ch;
link *p,*q;
cout<<"当前线性表为:"<<endl;
p=l;p=p->next;
if(l!=NULL)
do
{cout<<p->data<<" ";
p=p->next;
}while(p!=NULL);
cout<<endl;
cout<<"请选择您要的操作:";
cout<<" 1、插入";
cout<<" 2、查找";
cout<<" 3、删除";
cout<<" 4、合并";
cout<<" 0、退出";
cout<<endl;
cin>>k;
if(k==1)
{
cout<<"请输入您要插入的数据值:";
cin>>ch;
cout<<"请输入您要插入的位置:";
cin>>i;
p=ins(l,ch,i);
q=print(l);
}
else if(k==2)
{
cout<<"请输入您要查找的数据值:";
cin>>ch;
p=find(l,ch);
q=print(l);
}
else if(k==3)
{
cout<<"请输入您要删除的数据的位置:";
cin>>i;
p=del(l,i);
q=print(l);
}
else if(k==4)
{ p=add(l);
q=print(l);
}
else if(k==0)
;
else
{cout<<"输入错误!"<<endl;
q=print(l);}
return l;
}

int main()
{
cout<<"请输入一串单字符数据,以*结束!"<<endl;
char ch;
//link *head;
link *r,*p,*q,*l;
l=(link *)malloc(sizeof(link));
l->next=NULL;
r=l;
ch=getchar();
// getchar();
while(ch!='*')
{
p=(link *)malloc(sizeof(link));
p->data=ch;p->next=NULL;
r->next=p;r=r->next;
ch=getchar();
// getchar();
}
//l=head;
q=print(l);
return 0;

}

2.c语言编的

#include <stdio.h>
#include <malloc.h>
#define N 8
typedef struct node
{int data;
struct node *next;
}node;

node * createsl()
{
node *p,*s,*h;
int j=1,x;
p=s=h=(node*)malloc(sizeof(node));
h->next=NULL;
printf("please input the data to create the list,end with -1 or %d nupmbers\n",N);
while(x!=-1&&j<=N)
{
printf("number %d:",j);
scanf("%d",&x);
s=(node*)malloc(sizeof(node));
s->data=x;
if(h->next==NULL)
h->next=s;
else
p->next=s;
p=s;
j++;
}
p->next=NULL;
return h;
}

int access(node *h,int i)
{
node *p;int j=1;
p=h->next;
while(p!=NULL)
{
if(p->data==i)
break;
p=p->next;
j++;
}
if(p!=NULL)
{
printf("find the number in position:%d\n",j);
return(p->data);
}
else
{
printf("can't find the number in the list!\n");
return -1;
}
}

void insertsl(node *h,int i)
{
node *p,*t;
int j=1;
p=h->next;;
while(p->next!=NULL)
{
p=p->next;
j++;
}

t=(node*)malloc(sizeof(node));
t->data=i;
t->next=p->next;
p->next=t;
printf("insert success in position %d\n",j+1);
}

void deletesl(node *h,int i)
{
node *p,*s,*q;
int j=1;
p=h;
while(p->next!=NULL)
{
q=p->next;
if(q->data==i)
break;
p=p->next;
j++;
}

if(p->next==NULL)
{
printf("Can't find the number you want to delete.\n");
return;
}
else
{
s=p->next;
p->next=s->next;
free(s);
printf("delete success in position %d\n",j+1);
}
}

void print(node *h)
{
printf("\nprint all the data in the list:") ;
node *s;
s=h->next;
if(s!=NULL)
{
while(s!=NULL)
{
printf(" %d ",s->data) ;
s=s->next;
}
}
else
printf("the list is empty!%d");
printf("\n");
}

int main()
{
node *p;
int a;
p=createsl() ;
printf("\nyou need find the number:\n");
scanf("%d",&a);
access(p,a);

printf("\nplease input the number you want to insert:\n");
scanf("%d",&a);
insertsl(p,a);

printf("\nplease input the number you want to delete:\n");
scanf("%d",&a);
deletesl(p,a);

print(p);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-22
希望以下程序对你有用:
/*链表的基本操作*/
# define NULL 0
# define ERROR 0
# define LEN sizeof(struct linklist)
struct linklist { /*链表的存储结构的表示*/
int data;
struct linklist *next;
};
int n; /*定义n为全局变量*/
struct linklist *head;
struct linklist *create( ) /*创建一个空链表*/
{
struct linklist *p,*q; /* p、q 为指向struct linklist 类型数据的指针变量*/
p=q=(struct linklist*)malloc(LEN); /*开辟一个新单元*/
scanf ("%d",&p->data);
head=NULL;
while (p->data!=0) /*当所输入数据不为0时,则执行循环体*/
{
n=n+1;
if (n==1) head=p;
else q->next=p;
q=p;
p=(struct linklist*)malloc(LEN); /*注要开辟了一个新单元后,才能输入数据
scanf("%d",&p->data);
}
q->next=NULL;
return(head); /*返回链表的头地址*/
}

struct linklist *insert(struct linklist *head,struct linklist *input) /*插入操作*/
{
struct linklist *s,*p,*q;
p=head; /*使p指向第一个结点*/
s=input; /*s指向要插入的结点*/
if (head==NULL) /*原来的链表是空表*/
{ head=s; s->next=NULL; } /*使s指向的结点作为头结点*/
else
while ((s->data>p->data)&&(p->next!=NULL))
{
q=p; /*使q指向刚才p指向的结点*/
p=p->next; /*p后移一个结点*/
}
if (s->data<=p->data)
{
if (head==p) head=s; /*插到原来第一个结点之前*/
else q->next=s; /*插到q指向的结点之后*/
s->next=p;
}
else
{p->next=s; s->next=NULL;}/*插到最后的结点之后*/
n=n+1;/*结点数加1
return (head);
}

struct linklist *del(struct linklist *head,int num) /*删除操作*/
{
struct linklist *p,*q;
if (head==NULL)
{
printf ("\n list null!\n");
}
p=head;
while (num!=p->data && p->next!=NULL) /*p指向的不是所要长的结点,并
{ 且后面还有结点*/
q=p;p=p->next; /*p后移一个结点*/
}
if (num=p->data) /*结点找到了*/
{
if (p=head) /*若p指向的是表结点,把第二个结点地址赋予head*/
head=p->next;
else /*否则将下一结点地址赋给前一结点地址*/
q->next=p->next;
n=n-1; /*删除一个结点后,结点数减1*/
}
else printf ("%d not been found !\n",num); /*找不到该结点*/
return (head); /*返回头指针地址*/
}

void print(struct linklist *head) /*输出链表函数*/
{
struct linklist *p;
printf ("\n now there are %d record :",n);
p=head;
if (head!=NULL)
do
{
printf ("%d ",p->data);
p=p->next; /*p指向下一个要输出的结点*/
}while (p!=NULL);
}

main()
{
struct linklist *in;
int del_num;
printf ("\ninput the records,exit with 0 :\n");
head=create(); /*返回头指针*/
print(head); /*输出全部结点*/
printf ("\n input the inserted record,end with 0:");
in=(struct linklist *)malloc(LEN);
scanf ("%d", &in->data);
while (in->data!=0)
{
head=insert(head,in);
print (head);
printf ("\n input the inserted number,end with 0 :");
in=(struct linklist *)malloc(LEN); /*每次插入,都要开辟一个新结点*/
scanf ("%d", &in->data);
}
printf ("\ninput the deleted number,exit with 0:");
scanf ("%d",&del_num);
while (del_num!=0)
{
head=del(head,del_num);
print (head);
printf ("\ninput the deleted number,exit with 0:");
scanf ("%d",&del_num);
}
}


第2个回答  2012-03-16
数据结构图的遍历操作
第3个回答  2009-05-24
百度搜啊
相似回答