利用数据结构和C语言所学的相关知识,实现单链表的创建、插入、删除、打印和查询功能。

二实验内容
 单链表的创建 data1,学号,姓名
 单链表的打印 显示
 单链表的插入
 单链表的删除
 单链表的查询 按字段
三实验步骤(描述+核心代码)
 程序设计规划(实现的功能、分几个模块、子函数)
 编写单链表创建子函数
 编写单链表打印子函数
 编写单链表插入子函数
 编写单链表删除子函数
 编写单链表查询子函数
 编写主函数Main(),通过功能菜单调用子函数
 编译调试程序
三、要有详细的注释
二实验内容
 单链表的创建 data1,学号,姓名
 单链表的打印 显示
 单链表的插入
 单链表的删除
 单链表的查询 按字段
三实验步骤(描述+核心代码)
 程序设计规划(实现的功能、分几个模块、子函数)
 编写单链表创建子函数
 编写单链表打印子函数
 编写单链表插入子函数
 编写单链表删除子函数
 编写单链表查询子函数
 编写主函数Main(),通过功能菜单调用子函数
 编译调试程序
三、要有详细的注释

1.整数
# include "iostream.h"
# include "stdlib.h"
# define NULL 0
typedef struct list{
int data;
struct list* next;
}list,*LIST;
void create(LIST& head){//创建链表
LIST p1,p2;
head=p1=p2=(LIST)malloc(sizeof(list));
cout<<"please input a int type num,quit by pressing 0\ndata: ";
cin>>p1->data;
for(;p1->data!=0;){
p2=p1;
p1=(LIST)malloc(sizeof(list));
cout<<"data: ";
cin>>p1->data;
p2->next=p1;
}
p2->next=NULL;
if(head->data==0)
head=NULL;
}
void insert(LIST& head){//把元素插入链表
LIST p;
p=(LIST)malloc(sizeof(list));
cout<<"please input a int type data you want to insert\ndata: ";
cin>>p->data;
p->next=head;
head=p;

}
void Delete(LIST& head){//删除链表元素
LIST p,q;
p=head;
if(head==NULL)
cout<<"list NULL,erro\n";
else{
cout<<"please input a int type num you want to delete\ndata: ";
int a;
cin>>a;
for(;p->data!=a&&p->next!=NULL;p=p->next)
q=p;
if(p->next==NULL&&p->data!=a)
cout<<a<<" is not in the list\n";
else if(p->data==a)
if(p!=head)
q->next=p->next;
else
head=head->next;
}
}
void SORT(LIST& head){ //链表排序
LIST p,q,s;
if(head==NULL)
cout<<"list is NULL,you have no need to sort it\n";
else{
for(p=head;p!=NULL;p=p->next){
s=p;
for(q=p;q!=NULL;q=q->next)
if(s->data>q->data)
s=q;
int a=p->data;
p->data=s->data;
s->data=a;
}
}
}
void print(LIST head){//输出链表
LIST p;
p=head;
if(head==NULL)
cout<<"list NULL,quit\n";
else{
cout<<"\nhere is the list\n";
for(;p!=NULL;p=p->next)
cout<<p->data<<" ";
cout<<endl;
}
}
int main(){
LIST head;
create(head);
print(head);
insert(head);
SORT(head);
print(head);
Delete(head);
print(head);

return 0;
}

2.字符
#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;

}

3.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-21
不怎么难,但是编这个很要时间的,呵呵,楼下的上