链表是通过指针连接数据元素的一种数据结构。链表由一系列结点(NODE)组成,每个结点包含两个域:存储数据元素的数据域(data field)和存储下一个结点地址的指针域(pointer field)。链表是除了数组以外使用得最多的数据结构,学习链表需要掌握以下几个重要的概念:
-
头指针(head pointer)—指向链表中第一个结点的指针。
-
首元结点—指链表中存储第一个数据元素的结点。
-
头结点(head node)—在首元结点之前附设的一个结点,该结点不存储数据元素,其指针域指向首元结点,其作用主要是为了方便对链表的操作。它可以对空表、非空表以及首元结点的操作进行统一处理。头指针指向头结点,单链表中可以没有头结点,此时头指针指向首元结点。
-
插入
-
删除
-
搜索
-
遍历
-
逆置
插入操作
在链表中插入一个新结点需要不止一步(但比数组简单),我们先通过图示来学习,暂时不涉及具体代码。首先,我们需要创建一个与链表结点有相同结构的结点,然后,确定插入位置。
假如我们要把新结点B(NewNode)插入到结点A(左边结点LeftNode)、C(右边结点RightNode)之间,那么需要让B指向C。
NewNode.next −> RightNode;
图示如下:
然后将结点A指向新结点。
LeftNode.next −> NewNode;
这样新结点就插入到两结点之间了,最终结果如下:
在链表开头插入结点步骤于此类似,之后的代码中会涉及到,在链表末尾插入结点时,最后一个结点指向新结点,新结点指向NULL。
删除操作
删除操作也需要不止一步,首先,使用搜索算法定位要删除的结点。
目标结点(TargetNode)的前驱(LeftNode)应当指向目标结点的后继。
LeftNode.next −> TargetNode.next;
然后断开目标结点与其后继的连接。
TargetNode.next −> NULL;
如果被删除的结点无需再次使用的话我们就可以释放掉它占用的内存。最终结果如下:
逆置操作
转置操作相对复杂一些,我们需要让头指针指向最后一个结点,并且逆置整个链表。
首先,我们来操作最后一个结点,其指针本应指向NULL,现在我们让它指向其前驱结点。
为了确保当前结点不会丢失,必须设置一个临时结点来保存它,并使当前结点的后继指向该临时结点,从头开始依次进行操作。
除了头指针指向的第一个节点外,所有节点都应指向其直接前驱并使其直接前驱成为新的直接后继。第一个结点指向NULL。
然后通过临时结点,使头指针指向新的首元节点。
#include
#include
#include
#include
struct node
{
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList()
{
struct node *ptr = head;
printf("\n[ ");
//start from the beginning
while(ptr != NULL)
{
printf("(%d,%d) ", ptr->key, ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
//insert link at the first location
void insertFirst(int key, int data)
{
//create a link
struct node *link = (struct node *) malloc(sizeof(struct node));
link->key = key;
link->data = data;
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
//delete first item
struct node *deleteFirst()
{
//save reference to first link
struct node *tempLink = head;
//mark next to first link as first
head = head->next;
//return the deleted link
return tempLink;
}
//is list empty
bool isEmpty()
{
return head == NULL;
}
int length()
{
int length = 0;
struct node *current;
for(current = head; current != NULL; current = current->next)
{
length++;
}
return length;
}
//find a link with given key
struct node *find(int key)
{
//start from the first link
struct node *current = head;
//if list is empty
if(head == NULL)
{
return NULL;
}
//navigate through list
while(current->key != key)
{
//if it is last node
if(current->next == NULL)
{
return NULL;
}
else
{
//go to next link
current = current->next;
}
}
//if data found, return the current Link
return current;
}
//delete a link with given key
struct node *delete(int key)
{
//start from the first link
struct node *current = head;
struct node *previous = NULL;
//if list is empty
if(head == NULL)
{
return NULL;
}
//navigate through list
while(current->key != key)
{
//if it is last node
if(current->next == NULL)
{
return NULL;
}
else
{
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
//found a match, update the link
if(current == head)
{
//change first to point to next link
head = head->next;
}
else
{
//bypass the current link
previous->next = current->next;
}
return current;
}
void sort()
{
int i, j, k, tempKey, tempData ;
struct node *current;
struct node *next;
int size = length();
k = size ;
for ( i = 0 ; i < size - 1 ; i++, k-- )
{
current = head ;
next = head->next ;
for ( j = 1 ; j < k ; j++ )
{
if ( current->data > next->data )
{
tempData = current->data ;
current->data = next->data;
next->data = tempData ;
tempKey = current->key;
current->key = next->key;
next->key = tempKey;
}
current = current->next;
next = next->next;
}
}
}
void reverse(struct node **head_ref)
{
struct node *prev = NULL;
struct node *current = *head_ref;
struct node *next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
int main()
{
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
insertFirst(4, 1);
insertFirst(5, 40);
insertFirst(6, 56);
printf("Original List: ");
//print list
printList();
while(!isEmpty())
{
struct node *temp = deleteFirst();
printf("\nDeleted value:");
printf("(%d,%d) ", temp->key, temp->data);
}
printf("\nList after deleting all items: ");
printList();
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
insertFirst(4, 1);
insertFirst(5, 40);
insertFirst(6, 56);
printf("\nRestored List: ");
printList();
printf("\n");
struct node *foundLink = find(4);
if(foundLink != NULL)
{
printf("Element found: ");
printf("(%d,%d) ", foundLink->key, foundLink->data);
printf("\n");
}
else
{
printf("Element not found.");
}
delete(4);
printf("List after deleting an item: ");
printList();
printf("\n");
foundLink = find(4);
if(foundLink != NULL)
{
printf("Element found: ");
printf("(%d,%d) ", foundLink->key, foundLink->data);
printf("\n");
}
else
{
printf("Element not found.");
}
printf("\n");
sort();
printf("List after sorting the data: ");
printList();
reverse(&head);
printf("\nList after reversing the data: ");
printList();
return 0;
}