//单链表的基本操作(增删改查...)
//新创建的链表为:1->7->4->0->9->4->8
//7 find!
//7前插入是:
//1->4->7->4->0->9->4->8
//4 find!
//4后插入是:
//1->4->8->7->4->0->9->4->8
//7 find!
//删除7后是:1->4->8->4->0->9->4->8
//删除后的链表为:1->4->8->4->0->9->4->8
//4 find!
//修改后的链表为:18->4->8->4->0->9->4->8
//反转后的链表为:8->4->9->0->4->8->4->18
//链表的长度为:8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 10
//定义链表
typedef struct node
{
int data;
struct node *next;
}Node;
Node *CreatList(Node *head, int length);
void PrintList(Node *head);
Node *FindList(Node *head, int a);
Node *FindPreNode(Node *head, Node *p);
Node *FindNextNode(Node *head, Node *p);
Node *InsertListEnd(Node *head, int index, int a);
Node *InsertListHead(Node *head, int index, int a);
Node *DeleteListHead(Node *head);
Node *DeleteListTail(Node *head);
Node *DeleteList(Node *head, int a);
Node *ModifyList(Node *head, int elem, int modify);
int LengthList(Node *head);
Node *InvertList(Node *head);
int Is_Circular(Node *head);
int main()
{
Node *head = NULL;
head = CreatList(head, 7);
printf("新创建的链表为:");
PrintList(head);
head = InsertListHead(head, 7, 4);
head = InsertListEnd(head, 4, 8);
head = DeleteList(head, 7);
printf("删除后的链表为:");
PrintList(head);
Node *p = FindList(head, 4);
head = ModifyList(head, 1, 18);
printf("修改后的链表为:");
PrintList(head);
head = InvertList(head);
printf("反转后的链表为:");
PrintList(head);
printf("链表的长度为:%d", LengthList(head));
}
//创建链表
Node *CreatList(Node *head, int length)
{
Node *temp, *tail;
for(int i = 1; i <= length; i++)
{
temp = (Node *)malloc(sizeof(Node));
if(temp == NULL)
{
printf("malloc error!!\r\n");
return NULL;
}
else
{
temp->data = rand() % Max;
temp->next = NULL;
if(i == 1)
head = temp;
else
tail->next = temp;
tail = temp;
}
}
return head;
}
//打印链表
void PrintList(Node *head)
{
if(head == NULL)
printf("Empty List! \r\n");
while(head)
{
printf("%d", head->data);
head = head->next;
if(head)
printf("->");
}
printf("\r\n");
}
//查询a在链表的位置
Node *FindList(Node *head, int a)
{
Node *temp = head;
if(head == NULL)
{
printf("Empty List! \r\n");
return NULL;
}
else
{
while(temp)
{
if(temp->data == a)
{
printf("%d find! \r\n", a);
return temp;
}
temp = temp->next;
}
printf("%d not find! \r\n", a);
return 0;
}
}
//前驱节点的查找
Node *FindPreNode(Node *head, Node *p)
{
Node *temp = head;
if(p == temp)
{
printf("%d无前驱节点", p->data);
return NULL;
}
else
{
while((temp->next != p) && (temp != NULL))
{
temp = temp->next;
}
return temp;
}
}
//后继节点的查找
Node *FindNextNode(Node *head, Node *p)
{
Node *temp = head;
while(temp && (temp != p))
{
temp = temp->next;
}
temp = temp->next;
return temp;
}
//在节点后面插入元素
Node *InsertListEnd(Node *head, int index, int a)
{
Node *pt = (Node *)malloc(sizeof(Node));
pt->data = a;
//在特定位置插入
// for(int i = 1; i < index; i++)
// {
// temp = temp->next;
// }
// if(temp->next == NULL)
// {
// temp->next = pt;
// pt->next = NULL;
// }
// else
// {
// pt->next = temp->next;
// temp->next = pt;
// }
Node *temp = FindList(head, index);
if(temp->next == NULL)
{
temp->next = pt;
pt->next = NULL;
}
else
{
//先链接后面
pt->next = temp->next;
temp->next = pt;
printf("%d后插入是:\r\n", index);
PrintList(head);
}
return head;
}
//在节点前面插入元素
Node *InsertListHead(Node *head, int index, int a)
{
Node *pt = (Node *)malloc(sizeof(Node));
pt->data = a;
//在特定位置插入
// if(index == 1)
// {
// pt->next = head;
// head = pt;
// }
// else
// {
// for(int i = 1; i < index - 1; i++)
// {
// temp = temp->next;
// }
// pt->next = temp->next;
// temp->next = pt;
// }
Node *temp = FindList(head, index);
if(temp == head)
{
pt->next = head;
head = pt;
}
else
{
//寻找前驱节点
Node *pre = FindPreNode(head, temp);
pre->next = pt;
pt->next = temp;
printf("%d前插入是:\r\n", index);
PrintList(head);
}
return head;
}
//删除链表头
Node *DeleteListHead(Node *head)
{
Node *temp = head;
if(temp == NULL)
{
printf("List is Empty! \n");
return NULL;
}
head = temp->next;
free(temp);
return head;
}
//删除链表尾
Node *DeleteListTail(Node *head)
{
Node *temp = head;
if(temp == NULL)
{
printf("List is Empty! \n");
return NULL;
}
while(temp->next != NULL)
{
temp = temp->next;
}
//寻找前驱节点
Node *pre = FindPreNode(head, temp);
free(temp);
pre->next = NULL;
return head;
}
//删除链表中的任意
Node *DeleteList(Node *head, int a)
{
if(head == NULL)
{
printf("List is Empty! \n");
return NULL;
}
Node *temp = FindList(head, a);
if(temp == NULL)
{
printf("No find! \n");
return NULL;
}
if(temp == head)
{
head = DeleteListHead(head);
}
else if(temp->next == NULL)
{
head = DeleteListTail(head);
}
else
{
Node *pt = head;
while(pt->next != temp)
{
pt = pt->next;
}
pt->next = temp->next;
free(temp);
printf("删除%d后是:", a);
PrintList(head);
}
return head;
}
//修改链表元素
Node *ModifyList(Node *head, int elem, int modify)
{
Node *temp = head;
while(temp != NULL)
{
if(temp->data == elem)
temp->data = modify;
temp = temp->next;
}
return head;
}
//查看链表长度
int LengthList(Node *head)
{
int length = 0;
while(head)
{
length++;
head = head->next;
}
return length;
}
//倒置链表---->分出来
Node *InvertList(Node *head)
{
if(head == NULL || head->next == NULL)
return head;
else
{
Node *tmp = NULL;
Node *ptmp = NULL;
while(head != NULL)
{
//方法一
// tmp = head;
// head = head->next;
// tmp->next = ptmp;
// ptmp = tmp;
//方法二
tmp = head->next;
head->next = ptmp;
ptmp = head;
head = tmp;
}
head = ptmp;
return head;
}
}
//判断链表是否有环----------不理解
/*判断链表有环*/
int Is_Circular(Node *head)
{
if(head == NULL || head->next == NULL)
{
return 0;
}
/*快慢指针,当二者相等时,一定有环*/
Node *p1 = head;
Node *p2 = head;
while(p1 != NULL && p2 != NULL)
{
p2 = p2->next;
if(p1 == p2)
return 1;
p2 = p2->next;
p1 = p1->next;
}
return 0;
}
单链表的基本操作大全(增删改查...)-C语言
最新推荐文章于 2024-09-21 12:05:51 发布