链表的查找
int findLink(struct Link *head,int data)
{
struct Link *point;
point = head;
while(point !=NULL){
if(point->data == data){
return 1;
}
point = point->next;
}
return 0;
}
链表的个数计算
int totalLinkNum(struct Link *head)
{
struct Link *point;
point = head;
int cnt = 0;
while(point !=NULL){
cnt++;
point = point->next;
}
return cnt;
}
链表的后方插入
void houInsertLink(struct Link *p,int data,struct Link *new)
{
while(p!=NULL){
if(p->data == data){
new->next = p->next;
p->next = new;
}
p = p->next;
}
}
链表的前面插入
struct Link *qianInsertLink(struct Link *head,int data,struct Link *new)
{
struct Link *p;
p=head;
if(head->data==data){
new->next = head;
return new;
}
while(p->next!=NULL){
if(p->next->data == data){
new->next = p->next;
p->next = new;
return head;
}
p = p->next;
}
}
总代码
#include <stdio.h>
struct Link
{
int data;
struct Link *next;
};
void printLink(struct Link *head)
{
struct Link *point;
point = head;
while(point!=NULL){
printf("%d ",point->data);
point = point->next;//开始的时候先打印链表的头,然后将他内部的指向自己的指针的地址传递过来(指向下一个的地址)
}
putchar('\n');
}
void houInsertLink(struct Link *p,int data,struct Link *new)
{
while(p!=NULL){
if(p->data == data){
new->next = p->next;
p->next = new;
}
p = p->next;
}
}
struct Link *qianInsertLink(struct Link *head,int data,struct Link *new)
{
struct Link *p;
p=head;
if(head->data==data){
new->next = head;
return new;
}
while(p->next!=NULL){
if(p->next->data == data){
new->next = p->next;
p->next = new;
return head;
}
p = p->next;
}
}
int findLink(struct Link *head,int data)
{
struct Link *point;
point = head;
while(point !=NULL){
if(point->data == data){
return 1;
}
point = point->next;
}
return 0;
}
int totalLinkNum(struct Link *head)
{
struct Link *point;
point = head;
int cnt = 0;
while(point !=NULL){
cnt++;
point = point->next;
}
return cnt;
}
int main()
{
int data;
int ret;
struct Link *head = NULL;
struct Link Test1 = {1,NULL};
struct Link Test2 = {2,NULL};
struct Link Test3 = {3,NULL};
struct Link Test4 = {4,NULL};
Test1.next = &Test2;
Test2.next = &Test3;
Test3.next = &Test4;
head = &Test1;
struct Link new = {100,NULL};
head = qianInsertLink(head,1,&new);
printLink(head);
struct Link new2 = {101,NULL};
head = qianInsertLink(head,2,&new2);
printLink(head);
struct Link new3 = {104,NULL};
head = qianInsertLink(head,4,&new5);
printLink(head);
printf("总共有%d个数据\n",totalLinkNum(head));
/*ret = findLink(head,data);
if(ret==1){
printf("有该数据\n");
}else{
printf("没有该数据\n");
}*/
//insertLink(&Test1,newData,&pNew);
//printLink(&Test1);
return 0;
}