#include
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((jnext!=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<<"输入有误"<
{
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<<"您查找的数据在第"<
}
}
if(j!=1)
cout<<"您查找的数据不在线性表中."<
}
link * del(link *l, int i)
{
link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"输入有误"<
{
s=p->next;
p->next=s->next;
free(s);
}
return l;
}
link * add(link *l )
{
link *p,*s;
cout<<"请输入一串单字符数据,以*结束!"<
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<<"当前输入的线性表为:"<
if(L!=NULL)
do
{cout<
P=P->next;
}while(P!=NULL);
cout<
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<<"当前线性表为:"<
if(l!=NULL)
do
{cout<
p=p->next;
}while(p!=NULL);
cout<
cout<<" 1、插入";
cout<<" 2、查找";
cout<<" 3、删除";
cout<<" 4、合并";
cout<<" 0、退出";
cout<
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<<"输入错误!"<
return l;
}
int main()
{
cout<<"请输入一串单字符数据,以*结束!"<
//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
#include
#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
#include
#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
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()--------*/
/*---------------------------------------