Table represents linear chain - single linked list

Single list

definition

Linear chain table stores also known as single chain, it refers to the storage unit via a data element to store any set of linear table. Each node of the linked list, in addition to the information storage element itself, also need to store a pointer to its successor node. I.e., a single linked list structure is divided into two parts, wherein the data is a data field used to store elements; Next pointer field is used to store the address of its successor node.
Single linked list node type is described as follows:

typedef struct LNode{		//定义单链表结点类型
	ElemeType data;			//数据域
	struct LNode *next; 	//指针域
}LNode,*LinkList;

Since the single list is discretely distributed in the storage space, so that the single linked list stored in the storage structure nonrandom.
Commonly used to identify a single head pointer list, such as a single linked list L, the head pointer is NULL represented as an empty table; Further, for ease of operation, will add a node before the first node a single linked list, become head node point, the head node pointer field points to the first element of a linear list of nodes.
Single chain logical structure as follows:
Here Insert Picture Description

Basic operation on a single list

Single list is constantly user application storage unit and changes the link relation obtained by a special data structure, called the head of the chain on the left of the list, referred to as the right end of the chain.
1, establishing the first interpolation single chain
which starts with an empty table, generate a new node, and the read data stored in the data field structure of the new node, the new node is then inserted into the current list the header, i.e. the right end of the list as a fixed, continuously extending leftward list obtained. Interpolation was first head is the end node.
Here Insert Picture Description
Establishing a single head of the list interpolation algorithm is as follows:

	LinkList List_HeadInsert(LinkList &L){			//逆向建立单链表
		LNode *s;
		int x;
		L = (LinkList)malloc(sizeof(LNode));		//创建头结点
		L->next = NULL;								//初始化链表为空
		scanf("%d",&x);								//输入结点的值
		while(x! = 9999 ){							//输入9999表示结束
			s = (LNode*)malloc(sizeof(LNode));		//创建新结点
			s->data = x;							
			s->next = L->next;
			L->next = s;							//将新结点插入表中,L为头指针
			scanf("%d",&x);
		}
		return L;
	}		

When using the interpolation method to establish the first single chain, and data read sequence order of the elements in the list generated is reversed. Wherein each node of the insertion time O (1), provided single chain length is n, then the total time complexity is O (n-);
2, single chain interpolation establishing the end
which is inserted into the new current node end of the linked list table, this must add a tail pointer r, always point to the end of the current node list. List of upcoming fixed at left, continue to extend the list to the right. Interpolation was first end of the head node.
Here Insert Picture Description
Establishing single chain tail interpolation algorithm is as follows:

	LinkList List_TailInsert(LinkList &L){				//建立正向单链表
		int x;											//设元素类型为整型
		L = (LinkList)malloc(sizeof(LNode));
		LNode *s, *r = L;								//r为表尾指针
		scanf("%d",&x);									//输入结点的值
		while("x != 9999 "){							//输入9999表示结束
			s = (LinkList *)malloc(sizeof(LNode));		//创建头结点
			s->data = x;							
			r->next = s;
			r = s;							 		   //将新结点插入表中,L为头指针
			scanf("%d",&x);
		}
		r->next = NULL;								  //尾结点指针置空
		return L;
	}

因为附设一个指向表尾结点的指针,故时间复杂度和头插法相同;
3、按序号查找结点的值
在单链表中从第一个结点出发,顺时针 next 域逐个往下搜索,直到找到第 i 个结点为止,否则返回最后一个结点指针域为 NULL。
按序号查找结点值的算法如下:

LNode *GetElem(LinkList L, int i){
	int j = 1;							//计数,初始为1;
	LNode *p = L->next;					//头结点指针赋给p;
	if(i == 0)							//若 i=0,则返回头结点
		return 1;
	if(i < 1)							//若 i 无效,则返回NULL;
		return NULL;
	while(p && j < i){					//从第 1 个结点开始找,查找第 i 个结点
		p = p->next;
		j++;
	}
	return p;							//返回第 i 个结点的指针,若 i 大于表长则返回 NULL;
}

按序号查找操作的时间复杂度为O(n);
4、按值查找表结点
从单链表的第一个结点开始,从后往前依次比较表中各结点数据域的值,若结点数据域的值等于给定 e ,则返回该结点的指针;若整个单链表中没有这样的结点,则返回NULL。
按值查找表结点的算法如下:

LNode *LocateElem(Linklist L, ElemType e){
	LNode *p = L->next;
	while(p != NULL && p-> data != e)		//从第1个结点开始查找data域为e的结点
		p = p->next;		
	return p;								//找到后返回该结点指针,否则返回NULL
}

按值查找操作的时间复杂度为O(n)。
5、插入结点操作
插入结点操作是将值为 x 的结点新插入到单链表的第 i 个位置上。先检查插入位置的合法性,然后找到插入位置的前驱结点,即第 i -1 个结点,然后在其后插入新结点。其操作过程如图所示。
image
实现插入结点的代码片段如下:

	p = GetElem(L, i-1);				//查找插入位置的前驱结点
	s->next = p->next;
	p->next = x;									

插入结点操作的时间复杂度为O(n)。
另外:前插是指在某结点的前面插入一个新的结点,后插定义刚好和它相反。在单链表的插入算法中,通常使用后插操作。
以上面的算法为例,首先调用GetElem()找到第 i - 1 个结点,即插入结点的前驱结点后,在对其执行后插操作。由此可知,对结点的前插操作均可转化为后插操作,前提是从单链表的头结点开始顺序查找到其前驱结点,时间复杂度为O(n).
此外,可采用另外一种方法将其转化为后插操作来实现,设待插入结点为 * s,将 * s插入到 * p的前面,我们仍然将 * s插入到 * p的后面,然后将 p->data 与 s->data 交换,这样既满足了逻辑关系,又使得时间复杂度为O(1),算法代码片段如下:

	s->next = p->next;				//修改指针域,不能颠倒
	p->next = s;
	temp = p->data;					//交换数据域部分
	p->data = s->data;
	s->data = temp;

6、删除结点操作
删除结点操作是将单链表的第 i 的结点删除。先检查要删除位置的合法性, 后查找表中第 i-1 个结点,即被删除结点的前驱结点,然后再将其删除。其操作过程如图所示。
Here Insert Picture Description
实现删除结点的代码片段如下:

	p = GetMlem(L, i-1);				//查找删除结点的前驱结点
	q = p->next;
	p->next = q->next;
	free(q);							//释放结点的存储空间

删除结点操作的时间复杂度为O(n)。
另外:删除结点 * p的操作可通过删除 * p的后继结点来实现,实质就是将其后继结点的值赋予自身,然后删除后继结点,也使得时间复杂度为O(1)。
实现算法部分代码如下:

	q = q->next;					//令 q 指向*p的后继结点
	p->data = p->next->data;		//和后继结点交换数据域
	p->next = q->next;				//将*p结点从链中断开
	free(q);						//释放后继结点的存储空间

Table 7 long seek operation,
seek operation table length is the number of data calculation unit linked list node (excluding the head node), it is necessary sequentially from the first node in order to access the table for each node, as this requires setting a counter variable for each access a node counter is incremented until an empty access node. Calculation complexity of O (n).
[Note] single list does not include the length of the head node, the node is not the lead and the lead single linked list node slightly different requirements on the operation of the long table. It does not lead to a single node list, if the table is empty, to be processed separately.

Released nine original articles · won praise 10 · views 3554

Guess you like

Origin blog.csdn.net/weixin_44480968/article/details/104338083