单链表的基本操作

本文详细介绍了链表的创建(单链表和循环链表)、删除节点的方法、逆置链表以及链表的排序算法,包括冒泡排序、插入排序和头节点置空的排序方式。还涉及了约瑟夫环问题和链表查询中的跳表概念。
摘要由CSDN通过智能技术生成

链表

struct List
{
	int data;
	struct List* next;
}

创建链表

单链表

实现一:
struct List* listCreate()
{
	int data;
    struct List* head = NULL;
    struct List* pre = NULL;
    struct List* current = NULL;
    
    while(scanf("%d",&data) && data != -1)
    {
        current = (struct List*)malloc(sizeof(struct List));
        if(head == NULL)
            head = current;
        else
            pre->next = current;
        current->next = NULL;
        current->data = data;
        pre = current;
    }
    return head;
}
实现二:
struct List* listCreate()
{
    struct List* head;
    struct List* current;
    head = (struct List*)malloc(sizeof(struct List));
    current = head;
    while (scanf("%d", &data) && data != -1)
    {
        current->next = (struct List*)malloc(sizeof(struct List));
        current = current->next;
        current->data =  current;
        current->next = NULL;
    }
    return head;
}
错例
struct List* listCreate()
{
	int data;;
	struct List* current = NULL;
	struct List* head = current;
	while (scanf("%d", &data) && data != -1)
	{
		current = (struct List*)malloc(sizeof(struct List));
		if (head == NULL)
			head = current;
		current->data = data;
		current = current->next;
	}
	return head;
}

在使用malloc函数开辟的空间中,不要进行指针的移动,因为一旦移动之后可能出现申请的空间和释放空间大小的不匹配

循环链表

https://pic2.zhimg.com/v2-6d8f7d01b3b74ce646e73b76ee414c40_r.jpg

单独创建

struct List* circle_listCreate()
{
	int data;
    struct List* head = NULL;
    struct List* pre = NULL;
    struct List* current = NULL;
    
    while(scanf("%d",&data) && data != -1)
    {
        current = (struct List*)malloc(sizeof(struct List));
        if(head == NULL)
            head = current;
        else
            pre->next = current;
        current->next = head;
        current->data = data;
        pre = current;
    }
    return head;
}

逐节点创建

void Append(struct List** L,int data)
{
    struct List* head = *L;
    struct List* newNode = NULL;
    if((*L) == NULL)
    {
        (*L) = (struct List*)malloc(sizeof(struct List));
        (*L)->data = data;
        head = (*L);
        (*L)->next = head;
    }
    else
    {
        while ((*L)->next != head){
            (*L) = (*L)->next;
        }
        newNode = (struct List*)malloc(sizeof(struct List));
        newNode->data = data;
        (*L)->next = newNode;
        newNode->next = head;
        *L = head;
    }
}

约瑟夫环问题

void Append(struct List** L,int data)
{
    struct List* head = *L;
    struct List* newNode = NULL;
    if((*L) == NULL)
    {
        (*L) = (struct List*)malloc(sizeof(struct List));
        (*L)->data = data;
        head = (*L);
        (*L)->next = head;
    }
    else
    {
        while ((*L)->next != head){
            (*L) = (*L)->next;
        }
        newNode = (struct List*)malloc(sizeof(struct List));
        newNode->data = data;
        (*L)->next = newNode;
        newNode->next = head;
        *L = head;
    }
}
void Display(struct List* L,int num)
{
    struct List* head = L;
    struct List* pre = NULL;
    struct List* kill = NULL;
    int nodeNum = 0;
    while (L->next != head)
    {
        nodeNum++;
        L = L->next;
    }
    pre = L;
    L = L->next;
    nodeNum++;
    while (nodeNUm)
    {
        if (nodeNum == 1)
        {
            printf("%d",L->data);
            free(L);
            return;
        }
        for (int i=1; i < m; i++)
        {
            pre = L;
            L = L->next;
        }
        printf("%d ", L->data);
        kill = L;
        L = L->next;
        free(kill);
        nodeNum--;
    }
}

删除节点

实现方式一:

struct list* listDelete(struct list* L,int data)
{
    struct list* pre = L;
    struct list* head = L;
    struct list* kill;
    
    while(head != NULL && head->data == m)
    {
        kill = head;
        head = head->next;
        free(kill);
    }
    if(head == NULL)
        return head;
    
    pre = head;
    kill = head->next;
    while(kill!=NULL)
    {
        if(kill->data == m)
        {
            pre->next = kill->next;
            free(kill);
            kill = pre->next;
        }
        else
        {
            pre = kill;
            kill = kill->next;
        }
    }
    return head;
}

实现方式二:

struct list* listDelete(struct list** L,int data)
{
    struct list* head = (*L), * pre = (*L);
    struct list* newL = *L;
    struct list* kill = NULL;
    while (*L != NULL)
    {
        if((*L)->data == data)
        {
            if((*L) == newL)
                newL == newL->next;
            else
                pre->next = (*L)->next;
            kill = (*L);
            (*L) = (*L)->next;
            free(kill);
        }
        else
        {
            pre = (*L);
            (*L) = (*L)->next;
        }
    }
    *L = newL;
    return head;
}

删除节点并建立新链表

struct list* list_Delete_Create(struct list** L) //数据为奇数存为新链表
{
    struct list* newhead = NULL, * newcurrent = NULL, * newpre = NULL;
    struct list* newL = *L;
    struct list* kill = NULL;
    struct list* pre = *L;
    while (*L)
    {
        if((*L)->data%2 == 1)
        {
            newcurrent = (struct list*)malloc(sizeof(struct list));
            if(newhead == NULL)
                newhead = newcurrent;
            else
                newpre->next = newcurrent;
            newcurrent->data = (*L)->data;
            newcurrent->next = NULL;
            newpre = newcurrent;
            
            if((*L) == newL)
                newL = newL->next;
            else
                pre-next = (*L)->next;
            kill = (*L);
            (*L)=(*L)->next;
            free(kill);
        }
        else
        {
            pre = (*L);
            (*L) = (*L)->next;
        }
    }
    *L = newL;
    return newhead;
}

逆置链表

实现:

struct list* reverse(struct list* L)
{
    struct list* newhead = NULL, * current;
    while (L != NULL)
    {
        current = (struct list*)malloc(sizeof(struct list));
        current->data = L->data;
        L = L->next;
        current->next = newhead;
        newhead = current;
    }
    free(L);
    return newhead;
}

链表排序

实现一:

struct List* listSort(struct List** head, int n, int(*compare)(float a, float b)) {
	int i, j;
	struct List *temp;
    struct List *current;
    struct List *init = *head;
	for (i = 0; i < n; i++) {
		current = *head;
		temp = *head;
		for (j = 0; j < n - i - 1; j++) {
			if ((*compare)(current->data, current->next->data)) {
				if (current == *head) {
					*head = current->next;
					temp = current->next;
					current->next = temp->next;
					temp->next = current;
                    init = *head;
				}
				else {
					temp->next = current->next;
					current->next = current->next->next;
					temp->next->next = current;
					temp = temp->next;
				}
			}
			else {
				temp = current;
				current = current->next;
			}
		}
	}
	return init;
}
int Descending(float a, float b) {
	return a > b;
}

int Ascending(float a, float b) {
	return a < b;
}

实现二:

void listSort(struct List* head, int n, int(*compare)(float a, float b)) {
	int i, j;
	struct List *temp;
    struct List *current;
	for (i = 0; i < n; i++) {
		current = head;
		temp = head;
		for (j = 0; j < n - i - 1; j++) {
			if ((*compare)(current->data, current->next->data)) {
				if (current == head) {
					head = current->next;
					temp = current->next;
					current->next = temp->next;
					temp->next = current;
				}
				else {
					temp->next = current->next;
					current->next = current->next->next;
					temp->next->next = current;
					temp = temp->next;
				}
			}
			else {
				temp = current;
				current = current->next;
			}
		}
	}
}

实现三:

//头节点置为空

void sortlist(struct List* head, int n, int(*compare)(float a, float b))
{
    int i, j;
	struct List *temp;
    struct List *current;
	for (i = 0; i < n; i++) {
		temp = head;
		current = head->next;
		for (j = 0; j < n - i - 1; j++) {
			if ((*compare)(current->data, current->next->data)) {
				temp->next = current->next;
				current->next = current->next->next;
				temp->next->next = current;
				temp = temp->next;
			}
			else {
				temp = current;
				current = current->next;
			}
		}
	}
}

链表查询(跳表)

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值