链表可以进行如下操作:

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

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

创建新链表

新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表。由于这个链表只有一个节点,它所指向的下一个节点为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)的更多相关文章

  1. 关于链表的一些重要操作(Important operations on a Linked List)

    上篇博文中讨论了链表的一些基本操作: 链表的基本操作(Basic Operations on a Linked List) 然而,为创建一个多功能的链表,在深度学习之前我们还需要了解更多的链表操作. ...

  2. 数据结构算法C语言实现(五)---2.3重新定义线性链表及其基本操作

    一.简述 ...由于链表在空间的合理利用上和插入.删除时不需要移动等的优点,因此在很多场合下,它是线性表的首选存储结构.然而,它也存在着实现某些基本操作,如求线性表的长度时不如顺序存储结构的缺点:另一 ...

  3. 用Java实现单链表的基本操作

    笔试题中经常遇到单链表的考题,下面用java总结一下单链表的基本操作,包括添加删除节点,以及链表转置. package mars; //单链表添加,删除节点 public class ListNode ...

  4. C++学习笔记48:链表的基本操作

    //链表的基本操作 //生成链表,插入结点,查找结点,删除结点,遍历链表,清空链表 //链表类模板 //LinkedList.h #ifndef LINKEDLIST_H #define LINKED ...

  5. PHP单链表的基本操作

    链表的实现 数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X 上代 ...

  6. C语言链表的基本操作

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  7. 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 ...

  8. (链表) lintcode 219. Insert Node in Sorted Linked List

    Description   Insert a node in a sorted linked list.   Example Example 1: Input: head = 1->4-> ...

  9. (链表 双指针) 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 ...

随机推荐

  1. c语言for语句

    首先呢 for语句是由4部分组成 for(表达式1;表达式2;表达式3) 循环体: 注意 1:循环中的表达式用;隔开 表达式1通常用来呢赋初值 表达式2通常用来循环控制也就是循环条件 表达式3通常就是 ...

  2. 关于bootstrap--网格系统

    1. 2.偏移列(col-md-offset-*):为了在大屏幕显示器上使用偏移,请使用 .col-md-offset-* 类.这些类会把一个列的左外边距(margin)增加 * 列,其中 * 范围是 ...

  3. ubuntu14.04 + cocos2d-x-3.6 + eclipse发布android

    cocos2d-x-2.2.6版本 :http://www.cnblogs.com/weishuan/p/4698470.html 接下来是3.6了 ,准备好下面四个东东,我把这些都放在XXX/App ...

  4. 原生javascript 获得css样式有几种方法?

    css 样式分为行内样式和 外部样式: 1.javascript 获得行内样式 : 可以使用  ele.style."属性名称"(如果遇到属性名称带有"-", ...

  5. web前端代码规范——css代码规范

    Bootstrap CSS编码规范 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 为选择器分组时,将单独的选择器单独放在一行. 为了代码的易读性,在每个 ...

  6. java后台訪问url连接——HttpClients

    java后台訪问url,并传递数据--通过httpclient方式 须要的包,包可能多几个额外的,假设无用或者冲突删除就可以.httpclient是使用的是4.4.1的版本号:http://downl ...

  7. Solr 安装与集成IK中文分词器

    创建wangchuanfu core 1.  在example目录下创建wangchuanfu-solr文件夹: 2.  将./solr下的solr.xml拷贝到wangchuanfu-solr目录下 ...

  8. IIS 7管理API——Microsoft.Web.Administration介绍

    原文:http://www.cnblogs.com/dflying/archive/2006/04/17/377276.html 本文翻译整理自Carlos Aguilar Mares的blog文章: ...

  9. NSString 使用小结

    http://blog.csdn.net/ms2146/article/details/8654148

  10. [Mugeda HTML5技术教程之17] 理解Mugeda访问统计结果

    1. 功能简介 Mugeda提供动画统计功能,使得动画制作者可以直观的了解动画的浏览情况,包括浏览量,参与度,以及观看者的分布情况. 目前统计功能主要展示动画内容和广告工程的统计数据.在动画被发布或导 ...