数组与链表

数据存放在内存中,在内存中的组织形式只有两种:

衡量一个算法是否优越:

  • 时间复杂度(耗费时间)
  • 空间复杂度(占用内存) 

1、数组的管理 

int a[100];//就是在内存中申请100个连续的sizeof(int)空间
int *p = (int *)malloc(100xsizeof(int));//在堆空间中申请100个连续的int空间

对空间进行访问:得到第10个成员,则a[9],O(1)的成员访问时间(这里O(1)指的是时间复杂度,代表固定的时间,与变量无关)。

如果要在这100个空间中插入/删除一个数,需将后面所有的成员后移/前移。所以数组一般不会去添加或删除成员,其最大的优点是可以一次性访问某个成员,借助了数组下标的优越性。

2、数组的应用

要得出1~127这些范围中某个数的二进制编码中,从低位开始,第一次出现1的bit位编号。

例如:3——>0000 0011——>bit0   4——>0000 0100——>bit2

一个比较简单的方法:
如果data&0x01 == 1 ,已经找到这个bit位号了
否则data>>1,循环检测
虽然最多检测8次,但是我们仍然没有办法固定得到这个值的具体时间。
可以构建一个表:a[127] = {0,1,0,2,0,1,0,3......}
随便输入一个1~127之间的数n,可直接得到结果为a[n]。这样就能够在确定的时间内得到期望的结果。缺点是要事先构建表,占用内存空间。

3、链表的添加操作和删除链表结点

#include<stdio.h>
#include<stdlib.h>

typedef struct linklist
{
    int data;//可以是字符,结构体,指针等
    struct linklist *next;//指向下一个链表的指针,单链表
}linknode,*linklistp;

//在头部添加
linkslitp insert_head(linklistp head,linklistp newnode)
{
        newnode->next = head;
        head = newnode;
        return head;
}

//在尾部添加
linklistp insert_tail(linklistp head,linklistp newnode)
{
    if(head==NULL)
    {
        newnode->next=NULL;
        head = newnode;
    }
    else
    {
        linklistp temp = head;
        while(temp->next!=NULL)
        {
           temp = temp->next;
        }
        temp->next = newnode;
        newnode->next = NULL;
    }
    return head;
}

//在中间某个位置添加
linklistp insert_local(linklistp head,linklistp newnode,int data)
{
     linklistp temp = head;
	 if (temp == NULL)
		 return NULL;
	 if (data == temp->data)
	 {
		 newnode->next = head;
		 head = newnode;
		 return head;
	 }	
	 linklistp prev = head;
	 temp = head->next;
	 while (temp != NULL && data != temp->data)
	 {
		 prev = temp;
		 temp = temp->next;
		 if (temp == NULL)
			 return NULL;
	 }
	 prev->next = newnode;
	 newnode->next = temp; 
	 return head;
}

//删除链表中的某个结点
linklistp delnode(linklistp head, int data)
{
	linklistp temp = head;
	if (temp == NULL)
		return NULL;
	if (temp->data == data)
	{
		head = head->next;
		free(temp);
		return head;
	}
	linklistp prev = head;
	temp = head->next;
	while (temp!=NULL && temp->data != data)
	{
		prev = temp;
		temp = temp->next;
	}
	if (temp == NULL)
		return NULL;
	prev->next = temp->next;
	free(temp);
	return head;
}

//输出函数
void output(linklistp head)
{
    linklistp temp = head;
    while(temp)
    {
        printf("%d",temp->data);
        temp = temp->next;    
    }
    printf("\n");
}

int main()
{
    linklistp head = NULL;
    int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
    for(int i=0;i<10;i++)
    {
       linklistp newnode = (linklistp)malloc(sizeof(linknode));
       newnode->data = a[i];
       //分别在头部尾部添加
       head = insert_tail(head,newnode);//head = insert_head(head,newnode);
       output(head);
       //getchar();//用于调试看    
    }     
    
    //在链表某个位置插入
	int data;
	linklistp newnode2 = (linklistp)malloc(sizeof(linknode));
	newnode2->data = 66;
	printf("pls input need insert data:");
	scanf_s("%d", &data);
	linklistp result1 = insert_local(head, newnode2, data);
	if (result1 == NULL)
		printf("del failure or not has this node \n");
	else
		output(result1);   

    //删除链表中某个结点
	printf("pls input need del data:");
	scanf_s("%d", &data);
	linklistp result2 = delnode(head, data);
	if (result2 == NULL)
		printf("del failure or not has this node \n");
	else
		output(result2);

}

判断是否有环形链表

#include<iostream>
using namespace std;

typedef struct node
{
	int data;
	struct node *next;
}s_node;

// 构建环形链表
s_node* make_ring_chain_table(s_node *head, int data) {
	s_node* temp = head;
	s_node* ring_node = NULL;
	if (temp == NULL) return NULL;
	while (temp->next != NULL) {
		temp = temp->next;
		if (temp->data == data) {
			ring_node = temp;
		}
	}
	temp->next = ring_node;
	return head;
}

// 快慢指针判断是否是环形链表,每次慢指针走一步,快指针走两步,当慢指针入环时,快慢指针之间架设间隔有N个链表,下次每走一次,二者间隔N-1,直至间隔为0相遇说明有环形链表,否则快指针先到达NULL
bool is_ring_chain_table(s_node* head)
{
	bool ret = false;
	s_node* temp = head;
	s_node* p_fast = head;
	s_node* p_slow = head;
	if (temp == NULL) return false;

	while (p_fast->next->next != NULL) {
		p_slow = p_slow->next;
		p_fast = p_fast->next->next;
		if (p_slow == p_fast) {
			ret = true;
			break;
		}
	}

	// 计算环长度
	int n = 0;
	while (p_slow->next != p_fast) {
		p_slow = p_slow->next;
		n++;
	}
	cout <<"ring length:"<< n + 1 << endl;

	// 计算环入口点
	p_slow = head;
	n = 0;
	while (p_slow != p_fast) {
		p_slow = p_slow->next;
		p_fast = p_fast->next;
		n++;
	}
	cout << "ring node:" << n<< endl;
	return ret;
}

// 如果有节点到了两次,说明有环,第一个出现两次的节点为环形入口点,在s_node中加入cnt变量,
该节点每到达一次自加1
bool is_ring_chain_table2(s_node *head)
{
	s_node *temp = head;
	if (head == NULL) return false;

	while (temp->next != NULL) {
		temp = temp->next;
		temp->cnt += 1;
		if (temp->cnt == 2) {
			cout << temp->data << endl;
			return true;
		}
	}
	return false;

}


int main()
{
	s_node* head = NULL;
	int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
	for (int i = 0; i < 10; i++) {
		s_node *newnode = (s_node*)malloc(sizeof(s_node));
		newnode->data = a[i];
		head = insert_tail(head, newnode);
	}

	head = make_ring_chain_table(head, 5);
	bool res = is_ring_chain_table(head);
	cout << res << endl;
}

链表是否有环、环长度、环起点-CSDN博客

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值