鏈表之頭插法,尾插法,顯示,長度,查找位置(兩種),刪除;內聯函數小記

# 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行代碼一下,可以使用!!!

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