链表的基本操作(Basic Operations on a Linked List)
链表可以进行如下操作:
- 创建新链表
- 增加新元素
- 遍历链表
- 打印链表
下面定义了对应以上操作的基本函数。
创建新链表
新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表。由于这个链表只有一个节点,它所指向的下一个节点为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 ;
}
链表的基本操作(Basic Operations on a Linked List)的更多相关文章
- 关于链表的一些重要操作(Important operations on a Linked List)
上篇博文中讨论了链表的一些基本操作: 链表的基本操作(Basic Operations on a Linked List) 然而,为创建一个多功能的链表,在深度学习之前我们还需要了解更多的链表操作. ...
- 数据结构算法C语言实现(五)---2.3重新定义线性链表及其基本操作
一.简述 ...由于链表在空间的合理利用上和插入.删除时不需要移动等的优点,因此在很多场合下,它是线性表的首选存储结构.然而,它也存在着实现某些基本操作,如求线性表的长度时不如顺序存储结构的缺点:另一 ...
- 用Java实现单链表的基本操作
笔试题中经常遇到单链表的考题,下面用java总结一下单链表的基本操作,包括添加删除节点,以及链表转置. package mars; //单链表添加,删除节点 public class ListNode ...
- C++学习笔记48:链表的基本操作
//链表的基本操作 //生成链表,插入结点,查找结点,删除结点,遍历链表,清空链表 //链表类模板 //LinkedList.h #ifndef LINKEDLIST_H #define LINKED ...
- PHP单链表的基本操作
链表的实现 数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X 上代 ...
- C语言链表的基本操作
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe.
Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe. htt ...
- (链表) lintcode 219. Insert Node in Sorted Linked List
Description Insert a node in a sorted linked list. Example Example 1: Input: head = 1->4-> ...
- (链表 双指针) leetcode 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- HDU--3466(0-1背包+贪心/后效性)
题意是: 给你一些钱 m ,然后在这个国家买东西, 共有 n 件物品,每件物品有 价格 P 价值 V 还有一个很特别的属性 Q, Q 指 你如过想买这件物品 你的手中至少有这钱Q . 虽 ...
- Django之验证码 + session 认证
验证码 + session认证 目录结构 . └── project ├── app01 │ ├── admin.py │ ├── apps.py │ ├── __init__.py │ ...
- 万恶DevExpress
公司需要,开始了DevExpress的学习之旅,说它万恶也只是在不了解它的情况下,熟悉之后能很方便的实现很多想要的功能 这里简单写一下要整理的内容,也就是大纲,以后再慢慢添加 一.控件和组件 date ...
- 使用sublime text2怎样新建文件高速生成HTML头部信息?
前提须要安装Emmet插件.安装完毕后重新启动sublime. 输入下面简写,按Tab. html:4t <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...
- IIS PHP 配置 问题总结
今天帮助朋友解决一个IIS配置PHP的问题.大概是这样子的. IIS 与 PHP配置好了之后不能訪问,出现例如以下错误: HTTP 错误 500.19 - Internal Server Error ...
- 当chm文档点击左侧,右侧无内容时的解决方案
右击chm文件->属性->安全选项卡,选择你登陆计算机的用户名,把权限改成完全控制就可以显示了
- (转).net开发者对android开发一周的学习体会
春节期间,相对比较闲,上班时也没什么事情做.利用这一周的时间,简单的学习了一下移动方面的开发.主要是针对android,其实我对IOS更感兴趣 (因为我用iphone),苦于暂时没有苹果电脑,只能把它 ...
- 不指定order by时Sql的排序
在sql中不指定Order by,排序是按照主键吗?答案是不一定.举个例子: 查询AttendanceEmpRank表,主键是AttendanceEmployeeRankId,而且是聚集索引 ...
- Tomcat项目部署方式
一.静态部署 1.直接将web项目文件件拷贝到webapps 目录中 Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.所以可以将JS ...
- IOS 代码管理工具
代码管理工具国内主要用的是SVN 国外主要是Git