c++删除链接列表元素
单链接列表让我们从最简单的链接列表开始:每个节点只有一个链接的单链接列表。 该节点除了包含的数据(可能是短整数值到复杂结构类型的任何数据)以外,还具有指向
单链列表中的下一个节点。 如果遇到单链接列表的末尾,则该指针将为NULL。
单链列表仅以一种方式传播; 从头到尾。
没有办法返回上一个节点,这与稍后将要检查的双链表中可能发生的情况不同。
例如,我选择了一个单链列表,该列表越来越多地排序,并且每个int类型的值都是唯一的。 也就是说,每个值都大于前一个值,而小于下一个值。
例如:3-> 6-> 10-> 100-> NULL。
首先,让我们编写文件slistdefs.h,其中将包括要使用的结构以及slist函数的原型。 请注意,slist是单链接列表的缩写。
/**
*
*
* FILE: slistdefs.h
*
*
* Description:
*
* Function defines the structure to be used for the single-linked list,
* as well as the prototypes for the functions to be used for the
* single-linked list.
*
*
*/
#ifndef _slistdefs_h
#define _slistdefs_h
#include <stdio.h>
#include <stdlib.h>
/**
*------------------------------------------------------------
*
* Node for the single-linked list has two members:
*
* 1) a integer value which is unique in the single-linked list.
*
* 2) a pointer to the next node in the list which has larger value,
* since nodes are inserted increasingly in the single-linked list
* and each value is unique.
*
*------------------------------------------------------------
*/
struct slist{
int value;
struct slist *next;
};
/**
*
*
* Synopsis:
*
* #include <stdio.h>
* #include <stdlib.h>
*
* void insertValue( struct slist **head, int value )
*
*
* Description:
*
* Function insertValue adds a new struct slist node in the
* single-linked list with the value specified as parameter.
* The struct slist node is inserted increasingly according to the
* values of the struct slist nodes.
*
*
* Parameters:
*
* head: pointer to the head of the single-linked list.
*
* value: value of the new struct slist node to be inserted.
*
*
* Assertions:
*
* none.
*
*
* Returns:
*
* Nothing.
*
*
*/ void insertValue ( struct slist **head, int value );
/**
*
*
* Synopsis:
*
* #include <stdio.h>
*
* struct slist **searchValue( struct slist **head, int value )
*
*
* Description:
*
* Function searchValue, traverses the single-linked list and searches
* for a struct slist node with the value given as parameter.
*
*
* Parameters:
*
* head: pointer to the head of the single-linked list.
*
* value: value to be searched for in the single-linked list.
*
*
* Assertions:
*
* none.
*
*
* Returns:
*
* Function searchValue, returns NULL if no struct slist node
* is in the single-linked list with that value.
*
* Otherwise, it returns the address of the pointer that points to the
* struct slist node with that value. That is the head of the list if the
* first struct slist node contains that value, or the struct slist next pointer
* of the previous struct slist node from the one that has that value.
*
*
*/ struct slist ** searchValue ( struct slist **head, int value );
/**
*
*
* Synopsis:
*
* #include <stdio.h>
* #include <stdlib.h>
*
* void deleteValue( struct slist **head, int value )
*
*
* Description:
*
* Function deleteValue searches the single-linked list for
* a struct slist node with the value given as parameter and deletes
* it from the single-linked list.
*
*
* Parameters:
*
* head: pointer to the head of the single-linked list.
*
* value: value of the struct slist node to be deleted.
*
*
* Assertions:
*
* none.
*
*
* Returns:
*
* Nothing.
*
*
*/ void deleteValue ( struct slist **head, int value );
/**
*
*
* Synopsis:
*
* #include <stdio.h>
*
* void printValues( struct slist *head )
*
*
* Description:
*
* Function printValues prints on stdout the values of the
* struct slist nodes of the single-linked list.
*
*
* Parameters:
*
* head: pointer to the first node in the single-linked list, or NULL
* if list is empty.
* Gets the value of the head pointer.
*
*
* Assertions:
*
* none.
*
*
* Returns:
*
* Nothing.
*
*
*/ void printValues ( struct slist *head );
/**
*
*
* Synopsis:
*
* #include <stdio.h>
* #include <stdlib.h>
*
* void freeValues( struct slist **head )
*
*
* Description:
*
* Function freeValues frees all the struct slist nodes
* of the single-linked list.
*
*
* Parameters:
*
* head: pointer to the head of the single-linked list.
*
*
* Assertions:
*
* none.
*
*
* Returns:
*
* Nothing.
*
*
*/
void freeValues ( struct slist **head );
#endif
现在,让我们为每个功能创建其他文件:
/**
*
*
* FILE: deleteValue.c
*
*
* Description:
*
* File contains deleteValue function as declared in slistdefs.h file.
*
*
*/
#include "slistdefs.h"
/**
*------------------------------------------------------------
*
* Insert a struct slist node in the single-linked list.
*
*------------------------------------------------------------
*/ void deleteValue ( struct slist **head, int value ){
struct slist **deleteNode = searchValue( head, value );
/** node to be deleted exists */
if ( deleteNode != NULL ){
/** keep node to be deleted to free later */
struct slist *freeNode = *deleteNode;
/**
*
* deleteNode points to the pointer that points to the
* node to be deleted.
* This pointer must no longer point to that node, but the node
* after that, or NULL if no node exits. In either case, the next
* pointer of the node to be deleted contains the answer...
*
*/
*deleteNode = ( *deleteNode )->next;
free( freeNode );
}
} // void deleteValue( struct slist **head, int value )
/**
*
*
* FILE: insertValue.c
*
*
* Description:
*
* File contains insertValue function as declared in slistdefs.h file.
*
*
*/
#include "slistdefs.h"
/**
*------------------------------------------------------------
*
* Insert a struct slist node in the single-linked list.
*
*------------------------------------------------------------
*/ void insertValue ( struct slist **head, int value ){
/** pointer to the new struct slist node */
struct slist *add;
/**
*
* struct slist **p : *p is the pointer that points to the node
* that has value larger than the value of the new node to be inserted.
* That pointer must be made to point to the new node, and the next
* pointer of the new node must be made to point to whatever *p points
* at.Note, that *p might be the head of the single-linked list if the list is
* empty,or the next pointer of the last node, if the new one has the
* largest value.
*
*/
struct slist **p;
if ( ( add = ( struct slist * )malloc( sizeof( struct slist ) ) ) == NULL ){
( void )fprintf( stderr, "\nerror allocating memory%c", '\0' );
exit( EXIT_FAILURE );
}
for ( p = head ; *p != NULL && ( *p )->value <= value ; p = &( ( *p )->next ) ){
if ( ( *p )->value == value ){
/** do nothing; value already in list */
return ;
}
}
add->value = value;
/** add points to the node with > value ; NULL if in the end or start */
add->next = *p;
/** head, or next pointer of previous node points to the new node */
*p = add;
} // void insertValue( struct slist **head, int value )
/**
*
*
* FILE: searchValue.c
*
*
* Description:
*
* File contains searchValue function as declared in slistdefs.h file.
*
*
*/
#include "slistdefs.h"
/**
*------------------------------------------------------------
*
* Insert a struct slist node in the single-linked list.
*
*------------------------------------------------------------
*/ struct slist ** searchValue ( struct slist **head, int value ){
for ( ; *head != NULL && ( *head )->value <= value ; head = &( ( *head )->next ) ){
if ( ( *head )->value == value ){
/** value found in slist */
return ( head );
}
}
/** value not found in slist */
return ( NULL );
} // struct slist **searchValue( struct slist **head, int value )
/**
*
*
* FILE: printValues.c
*
*
* Description:
*
* File contains printValues function as declared in slistdefs.h file.
*
*
*/
#include "slistdefs.h"
/**
*------------------------------------------------------------
*
* Print values from the struct slist nodes in the single-linked list.
*
*------------------------------------------------------------
*/ void printValues ( struct slist *head ){
for ( ; head != NULL ; head = head->next ){
printf( "\nValue:\t%d\n", head->value );
}
} // void printValues( struct slist *head )
/**
*
*
* FILE: freeValues.c
*
*
* Description:
*
* File contains freeValues function as declared in slistdefs.h file.
*
*
*/
#include "slistdefs.h"
/**
*------------------------------------------------------------
*
* Free all the struct slist nodes in the single-linked list.
*
*------------------------------------------------------------
*/ void freeValues ( struct slist **head ){
/**
*
* previousNode: points to the node to be freed.
*
* nextNode: points to the next node from the one to be freed. Keep the
* single-linked list
* in this way.
*
*/
struct slist *previousNode, *nextNode;
/**
*
* previousNode points to the node to be freed.
* Before it gets freed, however, nextNode must be made to point to the
* next node from the one
* that previousNode points at. Note that in each step, previousNode
* must be made to point to nextNode
* so as nextNode will always point to the struct slist node that is the
* next from the the
* one that previousNode points at.
*
*/
for ( previousNode = nextNode= *head ; previousNode != NULL ; previousNode = nextNode ){
nextNode = previousNode->next;
free( previousNode );
}
/**
*
* Safely, make the head of the single-linked list NULL.
* If this is not made, any attempt to insert, delete, search or print
* will fail.
*
*/
*head = NULL;
} // void freeValues( struct slist **head )
该代码已准备好进行复制和粘贴,以便您可以立即运行测试并修改功能,以满足您的需求。
您可以添加消息,例如在删除时不存在值时; 您可能想通知用户未进行删除。 您也可以在许多其他地方添加打印件。 添加断言以检查每个值是否唯一。
您可以添加更多功能来在单链列表上进行一些练习,或者完全更改我所提供代码的概念。
您可以编写一个函数int
getLength (struct slist * head)返回列表中的节点数。编写函数以返回值较小和较大的节点,而编写更具挑战性的函数则是将列表复制到另一个节点,或者是将列表反向的函数。 尝试编写一些函数的递归版本。
除了查看更多的数据类型和代码,例如
双向链表和二叉搜索树 。翻译自: https://bytes.com/topic/c/insights/799744-single-linked-list-c
c++删除链接列表元素