📄 text1.c
字号:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}Linknode;
//creat the linklist!
Linknode *creat(void)
{
Linknode *head,*t,*p;
int dat;
head =(Linknode*)malloc(sizeof(Linknode));
head->next = NULL;
t=head;
scanf("%d",&dat);
while(dat!=-1)
{
p=(Linknode*)malloc(sizeof(Linknode));
p->data = dat;
p->next=NULL;
t->next=p;
t=p;
scanf("%d",&dat);
}
return head;
}
//the length of the linklist
int length(Linknode *h)
{
Linknode *p;
int i=0;
p=h->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
///inster the data from 0 to the max length!
void inster(Linknode *h,int dat,int place)
{
Linknode *p,*q,*pre;
int tem=0;
if(place<0||place>length(h))
{
printf("error!flow the max length\n");
exit(0);
}
p=h;
while(p!=NULL)
{
pre=p;
p=p->next;
if(tem==place)
{
q=(Linknode*)malloc(sizeof(Linknode));
q->next = pre->next;
q->data = dat;
pre->next = q;
}
tem++;
}
}
///delete the data
void del(Linknode *h,int dat)
{
Linknode *p,*pre=NULL;
if(!h->next)
{
printf("The Linklist is empty!\n");
}
p=h->next;
while(p&&p->data!=dat)
{
pre = p;
p = p->next;
}
if(p->data ==dat)
{
pre->next=p->next;
free(p);
printf("delete the data %d!\n",dat);
}
}
//find the data
void find(Linknode *h,int dat)
{
int place=0;
Linknode *p;
p=h->next;
if(p==NULL)
{
printf("error!\n");
return;
}
while(p!=NULL)
{
place++;
if(dat==p->data)
{
printf("the data %d,it place is:%d\n",dat,place);
break;
}
p=p->next;
}
if(place==length(h))
{
printf("the dat is not exasit!\n");
}
}
//output the number!
void output(Linknode *h)
{
Linknode *p;
p=h->next;
while(p!=NULL)
{
printf("% d",p->data);
p=p->next;
}
printf("\n");
}
void main(void)
{
Linknode *h;
// int p;
h=creat();
output(h);
printf("%d\n",length(h));
find(h,100);
inster(h,100,2);
output(h);
printf("%d\n",length(h));
find(h,100);
del(h,100);
output(h);
printf("%d\n",length(h));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -