构成链表
头文件
#include<stdio.h> #include<stdlib.h> #include<string.h>
- 创建节点(结构体)
{
int a;
struct Node *next;
};//创建节点
-
全局定义链表头尾指针
struct Node *head=NULL;
struct Node *end=NULL;
- 创建链表(增—尾添加)AddListTill(函数)
void AddListTill(int a)
{
struct Node *temp=(struct Node*)malloc(sizeof(struct Node));
temp->a=a;
temp->next=NULL;
if(head==NULL)
{
head=temp;
end=temp;
}else{
end->next=temp;
end=temp;
}
1.创建一个节点
(给节点在堆区分配一段内存)
2.给节点数据进行赋值
3.尾添加(两种情况:a.一个节点都没有Head=NULL b.已经有了节点,添加到尾巴上)
)
- 查—遍历链表(函数)
1.普通的查(Scanlist)
void Scanlist()
{
struct Node *temp=Head;
while(temp!=NULL)
temp=temp->next;//++操作
}
2.查询指定的节点(遍历,一个一个找)(FindNode)(结构体函数)
struct Node *FindNode(int a)
{
struct Node *temp=head;
while(temp!=NULL)
{
if(a==temp->a)
{
return temp;
}
temp=temp->next;
}
return NULL;
}
定义一个临时变量指向头,对数据中的节点进行判断,若找到,返回该节点,没找到,返回NULL
- 增—在指定位置插入节点(函数)(AddListRand)
1.没有节点(NULL=Head)
2.没找到此节点(FindNode)—返回为NULL
3.有此节点(在连接到链表上时,分为两种情况,找到的节点在尾部,找到的节点在中间)
void AddListRand(int index,int a)//在指定位置index增加节点a
//分为三种情况
{
if(head=NULL)
{
printf("链表没有节点");
return;
}//
struct Node *pt=FindNode(index);//pt就是找指定位置的指针
if(NULL==pt)
{
printf("没有指定节点");
return;
}//
struct Node *temp=(struct Node*)malloc(sizeof(struct Node));
temp->a=a;
temp->next=NULL;
if(pt==end)//在尾部
{
end->next=temp;
end=temp;
}else{
pt->next=temp->next;
pt->next=temp;
}
- 删—链表清空(全部删除)(函数FreeList)
1.一个一个清空
2.记录头结点的位置,实现++,释放
3.头尾清空
void FreeList()
{
struct Node *temp=head;
while(temp!=NULL)
{
struct Node *pt=head;
temp->next=temp;
free(pt);
}
head=NULL;
end=NULL;
}
- 删—尾删除(函数DeleteListTail)
1.链表为空
2.只有一个节点
3.有大于1的节点(找到尾节点的前一个节点)
void DeleteListTail()
{
if(head==NULL)
printf("链表为空,无需删除");
if(head==end)
{
free(head);
head=NULL;
end=NULL;
}
else{
struct Node *temp=head;
while(temp->next!=end)
{
temp=temp->next;
}
free(end);
end=temp;
end->next=NULL;//注意尾巴指针为空
}
}
- 删—删除头(函数FreeHeadList)
1.空节点
2.非空节点
void FreeHeadList()
{
struct Node *temp=head;//记住旧的头节点
if(head==NULL)
{
printf("链表为空");
}else{
head=head->next;
free(temp);
}
}
- 删—删除指定节点(函数DeleteListRand)
1.没有节点
2.没有指定节点
3.找到了指定节点:a.只有一个节点
b.只有两个节点(看删除头还是删除尾)
c.有多个节点(看删除头还是删除尾还是删除中间某个节点//直接跳过temp节点)
void DeleteListRand(int a)
{
if(head==NULL)
printf("链表没东西");//
struct Node *temp=FindNode(a);
if(NULL==temp)
{
printf("查无此点");
}//无指定节点
if(head==end)
{
free(head);
head=NULL;
end=NULL;
}//有一个节点
else if(head->next=end)
{
if(end==temp)
{
DeleteListTail();
}else if(head==temp)
{
DeleListHead();
}
}//有两个节点
else
{
if(end==temp)
DeleteListTail();
else if(head==temp)
DeleteListHead();
else//删除中间某节点
{
struct Node *pt=head;
while(pt->next!=temp)
{
pt=pt->next;
}
pt->next=temp->next;
free(temp);
}
}
}
主函数
void main()
{
struct Node *pFind;
for(i=0;i<5;i++)
{
AddListTill(i);
}
AddListRand(4,14);
DeleteListTail();
DeleteListRand(4);
ScanList();
FreeList();
pFind=FindNode(5);
if(pFind!=NULL)
{
printf("找到%d",pFind->a);
}else{
printf("No Find");
删除 (两个指针相互移动的那种)
struct ListNode *readlist()//创建链表
{
struct ListNode *head=NULL,*end=NULL;//这里头尾指针一定要指空!!!!!!(警告)
int num;
while(scanf("%d",&num)&&num!=-1)
{
struct ListNode *p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->data=num;
if(head==NULL)
{
head=end=p;
}else{
end->next=p;
end=p;
p->next=NULL;
}
}
return head;
}
struct ListNode *deletem( struct ListNode *L, int m )
{
struct ListNode *temp,*p,*q;
temp=L;
while(temp->data==m&&temp->next!=NULL)
{
p=temp->next;
free(temp);
temp=p;
}删除头
if(temp->data==m)
{
return NULL;
}删除尾
p=temp;
q=temp->next;//q找要删除的点
while(q)
{
if(q->data==m)
{
p->next=q->next;
free(q);
q=p->next;
}else{
p=q;
q=q->next;
}
}
return temp;
} 删除中间