c数据结构 实现单链表的创建、插入、删除、打印和查询
单链表的创建(3个域,一个指针域两个数据域)单链表的打印单链表的插入单链表的删除单链表的查...
单链表的创建(3个域,一个指针域两个数据域)
单链表的打印
单链表的插入
单链表的删除
单链表的查询
三实验步骤
程序设计规划(实现的功能、分几个模块、子函数)
编写单链表创建子函数
编写单链表打印子函数
编写单链表插入子函数
编写单链表删除子函数
编写单链表查询子函数
编写主函数Main(),通过功能菜单调用子函数
编译调试程序
四重点难点
在系统开发中遇到的问题,如何解决的,越具体越好。也可以分析一些重要的源代码。
查询要实现 展开
单链表的打印
单链表的插入
单链表的删除
单链表的查询
三实验步骤
程序设计规划(实现的功能、分几个模块、子函数)
编写单链表创建子函数
编写单链表打印子函数
编写单链表插入子函数
编写单链表删除子函数
编写单链表查询子函数
编写主函数Main(),通过功能菜单调用子函数
编译调试程序
四重点难点
在系统开发中遇到的问题,如何解决的,越具体越好。也可以分析一些重要的源代码。
查询要实现 展开
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;
}
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;
}
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;
}
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;
}
展开全部
/*-----------------------------------------------------*/
/*--------------单链表的创建、插入、删除、倒置操作-----------*/
/*-----------------------------------------------------*/
#include<malloc.h>
#include<stdio.h>
#define null 0
struct student
{
long Number;
char Name[20];
long Score;
struct student *Next;
};
int n=0;/*n为全局变量,用来计算链表的结点个数*/
/*-----------------------------------------*/
/*--------------创建结点函数Creat()--------*/
/*-----------------------------------------*/
struct student *Creat()
{
struct student *p1;
struct student *p2;
struct student *head=null;
p1=p2=(struct student *)malloc(sizeof(struct student));/*开辟一段可用内存单元*/
printf("please input the student''s Number Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
while(p2->Number!=0)
{
n++;
if(n==1) /*是否开辟的是第一个结点*/
head=p2;
else
p1->Next=p2;
p1=p2;
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
}
p1->Next=null;
return(head);
}
/*------------------------------------------*/
/*--------------查看链表内容函数View()------*/
/*------------------------------------------*/
View(struct student *head)
{
struct student *p;
p=head;
while(p->Next!=null)
{
printf("%ld %s %ld\n",p->Number,p->Name,p->Score);
p=p->Next;
}
printf("%ld %s %ld\n",p->Number,p->Name,p->Score);
}
/*-------------------------------------------------*/
/*--------------插入结点函数(前插)Insert()-------*/
/*-------------------------------------------------*/
Insert(struct student *head,int Num) /*head为链表头指针,Num插入链表位置*/
{
int t=1;
struct student *p1,*p2;
p1=head;
if (Num>n||Num<0)
{
printf("input error!!!\n");
return 0;
}
while(t<Num-1) /*找到要插入结点的前一个结点*/
{
p1=p1->Next;
t++;
}
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
p2->Next=p1->Next;
p1->Next=p2;
n++;
}
/*------------------------------------------*/
/*------------ 删除结点函数Delnode()--------*/
/*---------------------------------------
/*--------------单链表的创建、插入、删除、倒置操作-----------*/
/*-----------------------------------------------------*/
#include<malloc.h>
#include<stdio.h>
#define null 0
struct student
{
long Number;
char Name[20];
long Score;
struct student *Next;
};
int n=0;/*n为全局变量,用来计算链表的结点个数*/
/*-----------------------------------------*/
/*--------------创建结点函数Creat()--------*/
/*-----------------------------------------*/
struct student *Creat()
{
struct student *p1;
struct student *p2;
struct student *head=null;
p1=p2=(struct student *)malloc(sizeof(struct student));/*开辟一段可用内存单元*/
printf("please input the student''s Number Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
while(p2->Number!=0)
{
n++;
if(n==1) /*是否开辟的是第一个结点*/
head=p2;
else
p1->Next=p2;
p1=p2;
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
}
p1->Next=null;
return(head);
}
/*------------------------------------------*/
/*--------------查看链表内容函数View()------*/
/*------------------------------------------*/
View(struct student *head)
{
struct student *p;
p=head;
while(p->Next!=null)
{
printf("%ld %s %ld\n",p->Number,p->Name,p->Score);
p=p->Next;
}
printf("%ld %s %ld\n",p->Number,p->Name,p->Score);
}
/*-------------------------------------------------*/
/*--------------插入结点函数(前插)Insert()-------*/
/*-------------------------------------------------*/
Insert(struct student *head,int Num) /*head为链表头指针,Num插入链表位置*/
{
int t=1;
struct student *p1,*p2;
p1=head;
if (Num>n||Num<0)
{
printf("input error!!!\n");
return 0;
}
while(t<Num-1) /*找到要插入结点的前一个结点*/
{
p1=p1->Next;
t++;
}
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:\n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
p2->Next=p1->Next;
p1->Next=p2;
n++;
}
/*------------------------------------------*/
/*------------ 删除结点函数Delnode()--------*/
/*---------------------------------------
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询