博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链表的基本操作(Basic Operations on a Linked List)
阅读量:5938 次
发布时间:2019-06-19

本文共 6578 字,大约阅读时间需要 21 分钟。

链表可以进行如下操作:

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

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

创建新链表

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

/*Defining a function to create a new list.The return type of the function is struct node, because we will return the head nodeThe 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
#include
//creating the basic structure of a node of a Linked Liststruct 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 nodeThe 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 FUNCTIONint main(void){ struct node * listHEAD = NULL; listHEAD = createList(listHEAD, 42); // creating a new List with starting number 42 //adding a few number to the list listHEAD = addElement(listHEAD, 31); listHEAD = addElement(listHEAD, 98); listHEAD = addElement(listHEAD, 100); //finally printing our Linked List printList(listHEAD); return 0;}
View Code

 

转载于:https://www.cnblogs.com/programnote/p/4720905.html

你可能感兴趣的文章
时间戳
查看>>
Singularity:基于Mesos的服务部署和作业调度平台
查看>>
Kubernetes中的Job和CronJob,批量任务执行
查看>>
vi、vim、nano如何退出?
查看>>
互联网商道:自己吃饭,别人给钱
查看>>
我的友情链接
查看>>
#TCP你学得会# 之 TCP端口选择那些事儿
查看>>
我的友情链接
查看>>
Linux--rsync
查看>>
SQL--mysql忘记root密码
查看>>
C语言中对变量定义理解的重要性。
查看>>
VMware Horizon 7问题点处理
查看>>
添加MyEclipse WebSphere Portal Server支持(二)
查看>>
DevExpress v17.2新版亮点—DevExtreme篇(三)
查看>>
Oracle 之 管理
查看>>
shell编程——getopts用法小结
查看>>
首页功能添加(一)
查看>>
[LeetCode]59. Spiral Matrix II
查看>>
DHCP冲突的解决方法
查看>>
log4j1.x maven下配置
查看>>