链表之头插法,尾插法,显示,长度,查找位置(两种),删除;内联函数小记

# include<stdio.h>
# include<stdlib.h>
using namespace std;

typedef struct node
{
	int data;
	struct node * next;
}Node;
inline int len_list(Node * head)//链表长度
{
	Node* pt = head->next;
	int len = 0;
	while (pt)
	{
		len++;
		pt = pt->next;
	}

#if 0
	while (head->next)
	{
		len++;
		head->next = head->next->next;
	}
#endif
	return len;
}
int search_list(Node * head ,int search_data)//链表查找
{
	int loc = 0;
	int loc1 = 0;
	Node * pt = head->next;
	while (pt)
	{
		if (pt->data == search_data)
		{
			loc = loc1;
		}
		pt = pt->next;
		loc1++;
	}
	if (loc == 0)
	{
		printf("查无此数!!!");
		return -1;
	}
	else
	{
		return loc;
	}
	
}
Node * search_list_Node(Node * head, int search_data)
{
	head = head->next;
	while (head)
	{
		if (head->data == search_data)
			break;
		head = head->next;
	}
	if (head == NULL)
	{
		printf("查无此项!!!\n");
	}
	return head;
}


Node * creat_list_first()//头插法
{
	Node * head = (Node *)malloc(sizeof(Node));
	head->next = NULL;
	Node * cur = NULL;
	int data;
	printf("请输入节点数据:\n");
	scanf("%d", &data);
	while (data)
	{
		cur = (Node *)malloc(sizeof(Node));
		cur->data = data;
		cur->next = head->next;
		head->next = cur;
		scanf("%d", &data);
	}
	return head;
}

Node * creat_list_end()//尾插法
{
	Node * head = (Node*)malloc(sizeof(Node));
	head->next = NULL;
	Node * cur = NULL;
	Node * pt = head;
	int data;
	printf("请输入节点数据:\n");
	scanf("%d", &data);

	while (data)
	{
		cur = (Node *)malloc(sizeof(Node));
		cur->data = data;
		cur->next = NULL;
		pt->next = cur;
		pt = cur;	
		scanf("%d", &data);
	}
	return head;
}
inline void show_list(Node * head)//显示链表
{
	Node * phead = head->next;
	while (phead)
	{
		printf("%d\n", phead->data);
		phead = phead->next;
	}
}
inline void  delete_data(Node*head, int data)
{
	Node * loc_list = search_list_Node(head, data);
	//head = head->next;之所以没有这句是为了保证链表的结构完整性,加上该句的话会使链表丧失头结点
	while (head->next != loc_list) head = head->next;//此处说明是待查找数据的前一项节点;

	head->next = loc_list->next;
	free(loc_list);
	
}


int main()
{
	Node * head = (Node *)malloc(sizeof(Node));
	
	head = creat_list_end();
	show_list(head);

	search_list_Node(head, 4);

	printf("链表长度:%d\n", len_list(head));
	printf("查找数据所在位置:%d\n", search_list(head, 4) + 1);
	delete_data(head, 4);
	show_list(head);
	return 0;
}


链表系列操作(版本2)

# include<stdio.h>
# include<stdlib.h>
# include<time.h>
using namespace std;
typedef struct node
{
	int data;
	struct node * next;
}Node;
inline Node * creat_NULL_list()
{
	Node * head = (Node*)malloc(sizeof(Node));
	head->next = NULL;
	return head;
}
inline void show_list(Node* head)
{
	head = head->next;
	while (head)
	{
		printf("%d\n", head->data);
		head = head->next;
	}
}
//头插法,只要保证,新来的指针有所指向,即可
inline Node * insert_node_first(Node * head, int data)
{
	Node * cur = (Node*)malloc(sizeof(Node));
	cur->next = NULL;
	cur->data = data;
	cur->next = head->next;
	head->next = cur;
	
	return head;
}

//尾插法:保证找到链表尾结点的位置,就能按位插入
Node * insert_node_end(Node * head, int data)
{
	Node * phead = head;
	while (phead->next != NULL) phead = phead->next;//寻找尾结点的地址
	Node * cur = (Node *)malloc(sizeof(Node));
	cur->next = NULL;
	cur->data = data;
	phead->next = cur;
	phead = cur;
	
	return head;
}
int len_list(Node* head)
{
	Node* p = head->next;
	int len = 0;
	while (p)
	{
		len++;
		p = p->next;
	}

	return len;
}
inline Node* find_data(Node* head, int data)
{
	head = head->next;
	while (head->data != data)
	{
		head = head->next;
	}
	return head;
}

Node * delete_node(Node * head, Node * delet_node)
{
	Node * loc = delet_node;//删除节点
	Node * former = head;
	while (former->next != loc) former = former->next;
	former->next = loc->next;
	free(loc);
	return head;
}

int main()
{
	Node * head = creat_NULL_list();
	for (int i = 0; i < 5; i++)
	{
		int data;
		printf("请输入节点数据:\n");
		scanf("%d", &data);
		head = insert_node_end(head, data);
	}
	printf("链表数据为:\n");
	show_list(head);
	printf("共有数据%d个\n", len_list(head));
	printf("删除节点6后的链表:\n");
	head = delete_node(head, find_data(head,6));
	show_list(head);
	return 0;
 }

inline 是c++中特有的内联函数,好处在于可以弥补宏定义缺少的语法检察和语义错误,并且可以消除普通函数的压栈和出栈的内存开销;但过于复杂的函数使用内联函数,会增加代码段的空间,建议在10行代码一下,可以使用!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章