1. 程式人生 > >單鏈表插入和刪除結點c語言的實現

單鏈表插入和刪除結點c語言的實現

插入節點的方法比較簡單,只需宣告一個索引的指標p和宣告一個記錄p下一個指標的next。實現過程就是,申請記憶體的指標pNew.讓p->next = pNew, pNew->next = next.

如此一來就實現了往p後面插入一個數據。

/*
	===================
	插入結點(某結點後)
	===================
*/
Node* Insert(Node* List, int score)
{
	Node *p,*next;
	for (p=List; p; p=p->next)
	{
		next = p->next;
		if (p->score == score)
		{
			Node* pNew = (Node*)malloc(sizeof(Node));
			printf("輸入插入的資料:\n");
			scanf("%d",&pNew->score);
			p->next = pNew;
			pNew->next = next;
		}
	}
	return List;
}


刪除結點:

1.先建立一個索引的指標和一個記錄的指標,分別用於向後遍歷索引連結串列和記錄需要刪除結點的上一個結點。

2.若是第一個結點就是需要刪除的結點,head=elem->next.即頭指標指向第二個結點,接著釋放索引指標。

3.若是結點在連結串列的中間,temp->next = elem->next.即刪除結點的上一個指標指向下一個指標。然後釋放索引指標。

實現程式碼

/*
	=============
	刪除結點
	=============
*/
Node* delete_node(Node* head, int score)
{
   Node *elem = head;//用於索引的指標
   Node *temp = NULL;//用於記錄刪除結點的上一個結點

   if (elem->score == score)//刪除的正好是第一個結點
   {
		head = elem->next;
		free(elem);
   }
	else
	{
		while (elem != NULL)//沒有到盡頭
		{
			temp = elem;//記錄儲存好
			elem = elem->next;//往後索引

			if (elem == NULL)//遍歷完也沒找到
			{
				printf("沒有找到要刪除的結點\n");
			}
			else if(elem->score == score)//找到
			{
				temp->next = elem->next;//刪除結點的前一個結點指向刪除結點的後一個結點
				free(elem);//釋放刪除結點的記憶體
				break;
			}
			else
			{
			printf("沒有找到要刪除的結點\n");
			}
		
		}
	}

	return head;
	
}

完整程式碼
// linklisttest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"  
#include "stdio.h"  
  
typedef struct stu//建立結構體
{
	int score;
	struct stu *next;
}Node;

Node* create_f();//頭插法建立連結串列
Node* create_e();//尾插法建立連結串列
Node* delete_node(Node* head, int score);//刪除結點
void display(Node *link);//連結串列的輸出
/*主函式*/
int main()
{

	Node *link = create_e();
	display(link);
	display(delete_node(link,99));
	return 0;
}

/*
===================
插入結點(某結點後)
===================
*/
Node* Insert(Node* List, int score)
{
Node *p,*next;
for (p=List; p; p=p->next)
{
next = p->next;
if (p->score == score)
{
Node* pNew = (Node*)malloc(sizeof(Node));
printf("輸入插入的資料:\n");
scanf("%d",&pNew->score);
p->next = pNew;
pNew->next = next;
}
}
return List;
}
/*
	====================
	功能:尾插法建立連結串列
	返回:頭部結點指標
	====================
*/
Node* create_e()
{
	Node* head;//頭部
	Node* pnew;//建立新的結點
	Node* tail;//尾部
	int score = 0;

	printf("請輸入學生成績,成績為負數時退出輸入。\n");
	head = (Node*)malloc(sizeof(Node));//先建立一個空連結串列

	tail = head;//tail指向頭部
	scanf("%d",&score);
	while (score >= 0)
	{
		pnew = (Node*)malloc(sizeof(Node));//建立新節點
		pnew->score = score;
		//***向後延伸生成連結串列****
		tail->next = pnew;//第一個tail就是head,說明head中的資料並沒初始化,所以返回next
		tail = pnew;
		scanf("%d",&score);
	}
	tail->next = NULL;//尾部結束時指向空
	return head->next;//head中的資料並沒初始化,所以返回next
}

/*
	=============
	刪除結點
	=============
*/
Node* delete_node(Node* head, int score)
{
   Node *elem = head;//用於索引的指標
   Node *temp = NULL;//用於記錄刪除結點的上一個結點

   if (elem->score == score)//刪除的正好是第一個結點
   {
		head = elem->next;
		free(elem);
   }
	else
	{
		while (elem != NULL)//沒有到盡頭
		{
			temp = elem;//記錄儲存好
			elem = elem->next;//往後索引

			if (elem == NULL)//遍歷完也沒找到
			{
				printf("沒有找到要刪除的結點\n");
			}
			else if(elem->score == score)//找到
			{
				temp->next = elem->next;//刪除結點的前一個結點指向刪除結點的後一個結點
				free(elem);//釋放刪除結點的記憶體
				break;
			}
			else
			{
			printf("沒有找到要刪除的結點\n");
			}
		
		}
	}

	return head;
	
}

/*
   =================
   功能:輸出連結串列
   =================
*/
void display(Node *link)
{
	Node *p = link;
	while (p != NULL)
	{
		printf("%d\t",p->score);
		p = p->next;
	}
	puts("\n");
}

/*
	====================
	功能:頭插法建立連結串列
	返回:連結串列頭指標
	====================
*/
Node* create_f()
{
	Node *head;//連結串列頭
	Node *pnew;//用建立新的結點
	int score = 0;

	head = NULL;//頭部置NULL
	printf("請輸入學生成績,成績為負時退出輸入\n");
	while (score >= 0 )
	{
		pnew = (Node*)malloc(sizeof(Node));//建立新的結點
		scanf("%d",&score);
		pnew->score = score;
		//*******頭插法的實現******
		pnew->next = head;
		head = pnew;

	}
	return head;
}