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

本文共 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

你可能感兴趣的文章
spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)转
查看>>
OFFICE 2007 序列号
查看>>
07-JAVA继承与接口
查看>>
ubuntu15.10下sublime text3 无法输入中文解决办法
查看>>
LR web_custom_request
查看>>
MySQL-DDL语言
查看>>
Java-笔记10-复习
查看>>
5月10团队博客
查看>>
意见汇总
查看>>
phpcmsv9 幻灯片管理模块_UTF8
查看>>
(转)mysql分表的3种方法
查看>>
对Java单继承的弥补——接口
查看>>
洗礼灵魂,修炼python(15)--列表进阶话题—>列表解析/列表生成器
查看>>
MySQL中优化sql语句查询常用的30种方法
查看>>
.NET CORE实践(1)--Ubuntu下的Hello World
查看>>
1755: [Usaco2005 qua]Bank Interest
查看>>
hive升级注意事项
查看>>
C#---属性和字段
查看>>
COGS1533.[HNOI2002]营业额统计
查看>>
How to reset XiaoMi bluetooth headphone Youth edition.
查看>>