1.第一个较简单,建立两个结点(一个是空结点)
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
node* next;
}a;
node* creat()//或者struct* creat
{
node* p,*s;
int i;
p=(node*)malloc(sizeof(node));
s=(node*)malloc(sizeof(node));
p->next=NULL;
printf("input a number\n");
scanf("%d",&i);
s->data=i;
s->next=p->next;
p->next=s;
return p;
}
node* put(node* p)
{
node* q=p->next;
if(p->next==NULL)
{
return 0;
}
else
{printf("*********\n");
printf("%d\n",q->data);
}
}
int main()
{
int i,j;
node* p=creat();
put(p);
return 0;
}
2.第二个比第一个更进一步
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct node
{
int data;
node* next;
};
node* creat()
{
node* p,*s;
char a[30],i;
printf("please enter a string:\n");
scanf("%s",a);
int len=strlen(a);
p=(node*)malloc(sizeof(node));//node 和struct的字节为8;
//printf("*****sizeof struct:%d\n",sizeof(node));
p->next=NULL;
for(i=0;i<len;i++)
{
s=(node*)malloc(sizeof(node));
s->data=a[i];
s->next=p->next;
p->next=s;
}
return p;
}
void print(node* p)
{
node* q=p->next;
while(q!=NULL)//相当于头结点的地址的指向不=NULL;
{ //输出表中非最后一个元素
printf("%c",q->data);
q=q->next;
}
//printf("%c",q->data); //输出表中最后一个数据
printf("\n");
}
void elete(node* p,char a)//此处函数名不能是delete
{
node* e=p->next;
while(e!=NULL)
{
if(e->data==a)
{
p->next=e->next;
//free(e->data);释放的是e
free(e);
break;
}
e=e->next;
p=p->next;
}
if(e==NULL)//e 于此处就是个地址指向
printf("this node is not exist!\n");
}
//print函数另一种写法:
node* find(node* p,char x)
{
node* q=p->next;
int a=0;
//while(q->next!=NULL)*******此类语句不能再加->next,如此会忽略最后一个结点
while(q!=NULL)
{
if(q->data==x)
return q;
q=q->next;
}
return q;
}
node* insert(node* p,char d)
{
char h;
node* q=p->next,*t;
t=(node*)malloc(sizeof(node));//不要忘记开辟区间,由于忘记导致程序多次意外结束;
printf("please enter the character you want to insert:\n");
getchar();
scanf("%c",&h);
printf("********\n");
t->data=h;
printf("%c\n",h);
if(q->next==NULL)
{printf("3********\n");
t->next=q->next;
q->next=t;
}printf("1********\n");
node* w;
w=find(p,d);
if(w==NULL)
printf("fause:this character is not exist!\n");
else
{
t->next=w->next;
w->next=t;
}
}
int main()
{
//node* p,w;这种写法错误****注意 注意
node* p,*w;
char x,a,v;
p=creat();
print(p);
printf("input the number you want to find in the link:\n");
getchar();//吸收\n
scanf("%c",&x); //********************************************
//*******此处scanf竟然会吸收‘\n’,不懂!!!!!!!!**
w=find(p,x); //*********************************************
printf("please enter the character you want to delete:\n");
getchar();
scanf("%c",&a);
elete(p,a);
print(p);
printf("input the number you want to insert before:\n");
getchar();
scanf("%c",&v);
insert(p,v);
print(p);
return 0;
}
<span style="font-size:18px;color:#6600CC;"><strong>(1)creat函数(尾插法正序)
a,需要三个指针变量q,s,r
b,r->next指向s
c,q与s循环建立链表</strong></span>
node* creat()
{
node* p,*s,*r;
int i,len;
char a[30];
printf("please enter a string:\n");
scanf("%s",a);
len=strlen(a);
r=(node*)malloc(sizeof(node));
s=(node*)malloc(sizeof(node));
r->next=s;
s->data=a[0];
for(i=1;i<len;i++)
{
p=(node*)malloc(sizeof(node));
p->data=a[i];
s->next=p;
s=s->next;
}
s->next=NULL;//
return r;
}
模板分析:
1.creat函数(头插法倒序)
a,先建立空结点,malloc函数
b,依次建立后续结点
2.print函数3.find函数
a,链表为空时,返回NULL
b,链表不为空时,依次查找
4.elete函数(首先此处不能用delete)
a,依次查找后删除
b,并释放内存free函数
c,找不到该结点就报错
5.insert函数
a,链表为空时,直接插入,此时链表共有两个结点
b,链表不为空时,调用find函数
c,对find所返回的指针进行插入操作
6.change 与count函数较为简单,此处不再详述