数据的创建,前插后插,遍历,计数,动态内存开辟动态创建等
#include <stdio.h>
#define dedata 8
struct Test
{
int data;
struct Test *next;
};
void Printlink(struct Test *point)
{
//struct Test *point;
//point = head;
int count = 0;
while(point != NULL)
{
printf("%d ",point->data);
point = point->next;
count++;
}
putchar('\n');
printf("Total number of link is %d\n",count);
}
int search_number(struct Test *point,int data)
{
int count = 0;
while(point != NULL)
{
count++;
if(point->data == data)
{
return 1;
}
point = point->next;
}
return 0;
}
int insert_number_from_beh(struct Test *point,int insert_data,struct Test *new)
{
while(point != NULL)
{
if(point->data == insert_data)
{
new->next = point->next;
point->next = new;
return 1;
}
point = point->next;
}
return 0;
}
struct Test* insert_number_from_head(struct Test *head,int insert_data,struct Test *new)
{
struct Test *p;
p = head;
if(p->data == insert_data)
{
new->next = p;
head = new;
return head;
}
while(p != NULL)
{
if(p->next->data == insert_data)
{
new->next = p->next;
p->next = new;
printf("find it\n");
return head;
}
p = p->next;
}
printf("Not find it\n");
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,};
int i = 0;
// struct Test *retr;
for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
{
printf("%d ",arr[i]);
}
putchar('\n');
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
struct Test t4 = {4,NULL};
struct Test t5 = {100,NULL};
struct Test t6 = {110,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
printf("use t1 to print three umbers.\n");
//printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);
Printlink(&t1);
int findata = dedata;
int ret = search_number(&t1,findata);
if(ret == 1)
printf("find the number %d\n",findata);
else
printf("Not find\n");
insert_number_from_beh(&t1,4,&t5);
Printlink(&t1);
struct Test *retr = insert_number_from_head(&t1,1,&t6);
Printlink(retr);
return 0;
}
标签:struct,point,int,next,链表,Test,data
From: https://blog.51cto.com/u_15906483/6193463