链表的基本操作(Basic Operations on a Linked List)

时间:2021-10-31 20:53:27

链表可以进行如下操作:

  • 创建新链表
  • 增加新元素
  • 遍历链表
  • 打印链表

下面定义了对应以上操作的基本函数。

创建新链表

新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表。由于这个链表只有一个节点,它所指向的下一个节点为NULL。

/*
Defining a function to create a new list.
The return type of the function is struct node, because we will return the head node
The parameters will be the node, passed from the main function.
The second parameter will be the data element.
*/
struct node * createList(struct node *head,int number)
{
//creating a temporary node for our initialization purpose
//the below line allocates a space in memory that can hold the data in a node.
struct node *temp = (struct node *)malloc(sizeof(struct node)); //assigning the number to data
temp -> data = number; //assigning the next of this node to NULL, as a new list is formed.
temp -> next = NULL; //now since we have passed head as the parameter, we need to return it.
head = temp;
return head;
}

增加新元素

向链表中增加新元素默认是添加到链表尾部。我们会在以后的内容中讨论添加到链表中间或是开头的情况。

增加一个元素,有两种情况:

  • 链表已经存在,这样我们只要遍历整个链表,然后将新元素添加到链尾就好了。
  • 链表不存在,我们可使用上面的函数创建一个链表。

遍历链表

遍历链表很简单,连标的HEAD总是指向链表的第一个元素。如果我们这样做:

HEAD = HEAD -> next;

HEAD就向链尾移动了一个节点。

链表的结尾用NULL表示。因此我们只要一直执行这条语句直到遇到NULL,这就意味着完成了遍历。

/*
Defining a case when we need to element in the Linked List.
Here 2 cases can arise:-
-The Linked List can be empty, then we need to create a new list
-The Linked List exists, we need to add the element
*/
struct node * addElement(struct node *head, int number)
{
if(head == NULL)
head = createList(head, number); // we can directly call the above function
else
{
// now this is a case when we need to add an element to an existing list. //Creating a new node and assigning values
struct node * temp = (struct node*)malloc(sizeof(struct node));
temp -> data = number;
temp -> next = NULL; //Now we have created the node but, we need to insert it at the right place.
//A Linked List terminates when we encounter a NULL
//Let us traverse the List using another temporary variable.
//We will point it to the start
struct node * temp2 = head; //The limiting condition of the while loop "until temp2's NEXT is not equal to NULL
//This will happen only in case of the last node of the list.
while(temp2 -> next != NULL)
{
// We need to go to the next node
temp2 = temp2 -> next;
} //Now temp2 points at the last node of the list.
//We can add the node at this position now. temp2 -> next = temp; // the number is added to the end of the List.
} return head; // because we only need the HEAD pointer for a Linked List.
}

打印链表

和遍历链表相似,区别就在于我们在向链尾移动的同时要打印元素。

/*
A function to print the Linked List.
The return type of this function will be void, as we do not need to return anything.
We just need the HEAD as the parameter, to traverse the Linked List.
*/
void printList(struct node * head)
{
// The terminating point of a Linked List is defined when we encounter a NULL
while(head != NULL)
{
printf("%d ",head->data); //now we need to move to the next element
head = head->next;
}
}

完整的包括main函数的代码已给出:

#include<stdio.h>
#include<stdlib.h> //creating the basic structure of a node of a Linked List
struct node
{
int data;
struct node * next;
}; /* Defining a function to create a new list.
The return type of the function is struct node, because we will return the head node
The parameters will be the node, passed from the main function.
The second parameter will be the data element.
*/
struct node * createList(struct node *head,int number)
{
//creating a temporary node for our initialization purpose //the below line allocates a space in memory that can hold the data in a node.
struct node *temp = (struct node *)malloc(sizeof(struct node)); //assigning the number to data
temp -> data = number; //assigning the next of this node to NULL, as a new list is formed.
temp -> next = NULL; //now since we have passed head as the parameter, we need to return it.
head = temp;
return head;
} /*
Defining a case when we need to element in the Linked List.
Here 2 cases can arise:-
-The Linked List can be empty, then we need to create a new list
-The Linked List exists, we need to add the element
*/
struct node * addElement(struct node *head, int number)
{
if(head == NULL)
head = createList(head, number); // we can directly call the above function
else
{
// now this is a case when we need to add an element to an existing list. //Creating a new node and assigning values
struct node * temp = (struct node*)malloc(sizeof(struct node));
temp -> data = number;
temp -> next = NULL; //Now we have created the node but, we need to insert it at the right place.
//A Linked List terminates when we encounter a NULL
//Let us traverse the List using another temporary variable.
//We will point it to the start
struct node * temp2 = head; //The limiting condition of the while loop "until temp2's NEXT is not equal to NULL
//This will happen only in case of the last node of the list.
while(temp2 -> next != NULL)
{
// We need to go to the next node
temp2 = temp2 -> next;
} //Now temp2 points at the last node of the list.
//We can add the node at this position now. temp2 -> next = temp; // the number is added to the end of the List.
} return head; // because we only need the HEAD pointer for a Linked List.
} /*
A function to print the Linked List.
The return type of this function will be void, as we do not need to return anything.
We just need the HEAD as the parameter, to traverse the Linked List.
*/
void printList(struct node * head)
{
// The terminating point of a Linked List is defined when we encounter a NULL
while(head != NULL)
{
printf("%d ",head->data); //now we need to move to the next element
head = head->next;
}
} //DEFINING THE MAIN FUNCTION int main(void)
{
struct node * listHEAD = NULL; listHEAD = createList(listHEAD, ); // creating a new List with starting number 42 //adding a few number to the list
listHEAD = addElement(listHEAD, );
listHEAD = addElement(listHEAD, );
listHEAD = addElement(listHEAD, ); //finally printing our Linked List
printList(listHEAD); return ;
}