单链表
单链表的组成就是一个一个的节点
1、数据元素本身,其所在的区域称为数据域;
2、指向直接后继元素的指针,所在的区域称为指针域;
单链表
1、student *head=Init_list();//初始化
2、Create_student(head,arr,5);//创建列表
3、Head_insert(head,66);//头部插入
4、Tail_insert(head,77);//尾部插入
5、Insert_list(head,22,88);//指定插入
6、Head_delete(head);//头部删除
7、Tail_delete(head);//尾部删除
8、MId_delete(head,88);//指定删除
9、Del_node(head,4);//节点删除
10、List_Rotate(head);//倒置
11、printf("\n链表长度:%d",Length_list(head));//链表长度
12、Clear_List(head);//清空链表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
struct student
{
int date;
struct student *next;
};
student *Init_list()//初始化
{
student * head=(student*)malloc(sizeof(student));//申请动态内存
if(head==NULL)
{
printf("申请内存失败!");
return head;
}
head->date=0;
head->next=NULL;
return head;
}
void print_student(student *head)//打印
{
student *p=head;
while(p->next!=NULL)
{
p=p->next;
printf("%d ",p->date);
}
}
void Create_student(student *head,int *arr,int size)//创建列表
{
student *cur,*p;
p=head;
for(int i=0;i<size;i++)
{
cur=(student*)malloc(sizeof(student));
cur->date=arr[i];
cur->next=NULL;
p->next=cur;
p=cur;
}
printf("初始链表:");
print_student(head);
}
void Head_insert(student *head,int num)//头部插入
{
student *p=head;
student *new_node=(student *)malloc(sizeof(student));
new_node->next=p->next;//p指向赋给new-node
p->next=new_node;//头指向的第一个new-node
new_node->date=num;//插入num
printf("\n");
printf("头部插入:");
print_student(head);
}
void Tail_insert(student *head,int num)//尾部插入
{
student *p=head;
student *new_node=(student *)malloc(sizeof(student));
while(p->next!=NULL)
{
p=p->next;
}
//此时的p->date为最后一个值
p->next=new_node;//p-next指向最后一位
new_node->next=NULL; //new-node是尾,它的下一位为空
new_node->date=num;
printf("\n");
printf("尾部插入:");
print_student(head);
}
void Insert_list(student *head,int num1,int num2)//指定插入
{
student *p=head;
student *new_node=(student *)malloc(sizeof(student));
while(p->date!=num1)
{
p=p->next;
}
//当p->date为num1时跳出
new_node->next=p->next;
p->next=new_node;
new_node->date=num2;
printf("\n");
printf("指定插入:");
print_student(head);
}
void Head_delete(student *head)//头部删除
{
student *p=head;
p->next=p->next->next;//将头直接指向第二位
printf("\n");
printf("头部删除:");
print_student(head);
}
void Tail_delete(student *head)//尾部删除
{
student *p=head,*pre=head->next;
while(pre->next!=NULL)
{
p=p->next;
pre=pre->next;
}
p->next=NULL;
printf("\n");
printf("尾部删除:");
print_student(head);
}
void MId_delete(student *head,int num)//指定删除
{
student *p=head,*pre=head->next;
while(pre!=NULL)
{
p=p->next;
pre=pre->next;
if(pre->date==num)//找到num跳出
{
break;
}
}
p->next=pre->next;//将num的前一位指向num的下一位
printf("\n");
printf("指定删除:");
print_student(head);
}
int Del_node(student *head,int index)//删除指点节点
{
student *cur_node=head;
student *del_node;
int i=1;
while(cur_node && i<index)
{
cur_node=cur_node->next;
i++;
}
del_node=cur_node->next; //此时的cur_node是待删除节点的上1个节点
cur_node->next=del_node->next;
free(del_node);
printf("\n");
printf("节点删除:");
print_student(head);
return 1;
}
void List_Rotate(student * head)//倒置
{
student *p=head;
student *cur=NULL,*per=NULL;
cur=p->next->next;// 第1个节点变为尾,即断开第1个节点与第2个节点的链接
p->next->next=NULL;//断开第一个几点
while(cur!=NULL)
{
per=cur->next;//保存当前的后继节点
cur->next=p->next;//将当前的节点插入到头节点之后
p->next=cur;//头节点链上当前节点 。这两部是实现插入的关键
cur=per;;//遍历下个节点
}
printf("\n");
printf("倒置链表:");
print_student(head);
}
int Length_list(student *head)//链表长度
{
student *p=head;
int length=0;
while(p->next!=NULL)
{
p=p->next;
length++;//每指向下一位一次,长度+1
}
return length;
}
int DestroyList(student *head)//销毁整个链表 销毁则是把整个链表的内存都释放掉
{
student *p=head;//删除前保留上1个节点
if(head=NULL)
{
return 0;
}
while(head!=NULL)
{
p=head->next;
free(head);
head=p;
}
return 0;
}
int Clear_List(student *head)//清空链表
{
student *p,*q;
if(head==NULL)
{
return 0;
}
p=head->next;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
head->next=NULL;
printf("\n");
printf("清空链表:");
print_student(head);
return 1;
}
int main()
{
int arr[]={11,22,33,44,55};
student *head=Init_list();//初始化
Create_student(head,arr,5);//创建列表
Head_insert(head,66);//头部插入
Tail_insert(head,77);//尾部插入
Insert_list(head,22,88);//指定插入
Head_delete(head);//头部删除
Tail_delete(head);//尾部删除
MId_delete(head,88);//指定删除
Del_node(head,4);//节点删除
List_Rotate(head);//倒置
printf("\n链表长度:%d",Length_list(head));//链表长度
Clear_List(head);//清空链表
return 0;
}
运行结果