链表的基本操作(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, 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;
}

时间: 2024-11-06 13:56:58

链表的基本操作(Basic Operations on a Linked List)的相关文章

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

上篇博文中讨论了链表的一些基本操作: 链表的基本操作(Basic Operations on a Linked List) 然而,为创建一个多功能的链表,在深度学习之前我们还需要了解更多的链表操作. 在表头插入元素. 在表中插入元素. 在确定的位置插入. 在某个元素的后面插入. 从链表中删除一个元素. 删除整个链表. 在链表头部插入元素 为了在链表头部插入元素,我们要完成一项小任务:创建一个临时节点,把数据存入这个临时节点,将这个节点指向链表的头节点. /* Defining a functio

《实验课---单链表的基本操作》

//线性表---链表的基本操作#include<stdio.h>#include<stdlib.h>#include<conio.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define NULL 0typedef int Status; //函数结果状态代码typedef int ElemType; //数据元素类型//------线性表的单链表存储结构

从新定义线性链表及其基本操作

链表在空间的合理利用上和插入.删除时不需要移动等优点,因此在很多场合下,它是线性表的首先储存结构.然而它也存在着实现某些基本操作,如求线性表的长度时不如顺序储存结构的特点.因而从新定义线性链表及其基本操作 头文件: #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define MYOVERFLOW -2 typedef int Status; typedef int Elemty

数据结构学习之单向链表的基本操作(非递归实现)

[摘要]本文讨论的均为带头结点的线性链表的基本操作,包括创建.删除.插入.打印等等,欢迎大家批评指正错误. (1)单链表存储结构 typedef struct LNode { int data; struct LNode *next; }LinkList; (2)创建链表 /* 功能:构建一个空的带头节点的单链表*/ Status InitList (struct LNode **L) { (*L) = (struct LNode *)malloc(sizeof(struct LNode));

链表(二)——单向链表的基本操作(创建、删除、打印、结点个数统计)

1.指针的联动 通过两个指针分别指向前驱和后继结点,并在单向链表上进行移动,当指针指向待处理的结点时,该结点的前驱也有指针指向. 2.设有一个无序单向链表,且数据域的值均不相同,使指针pmin指向最小值结点,并使指针prem指向最小值结点的前驱结点: 代码片段: for(p = head; p; q = p, p = p->next) { if(pmin->data > p->data) { pmin = p; prem = q; } } 3.单向链表的删除算法 注:使用mallo

数据结构与算法-链表的基本操作---ShinPans

//链表操作:建立.插入.删除.查找.倒置.删除等基本操作 #include<stdio.h> #include<stdlib.h> typedef  struct LNode {       int data;       structLNode *next; }LNode,*Llist; LNode *creat_head();//创建一个空表 void creat_list(LNode *,int);//创建一个长度为n的线性链表 void insert_list(LNode

线性链表的基本操作(删除,插入等)

主要内容如题,由于感觉自己对链表这块的知识真的很薄弱. 我之前还时不时的去看链表相关的知识,也算是复习把,但是效果不尽人意. 时间可以淡忘一切,有的很慢,有的很快,而我感觉自己对链表的学习则是一瞬的时间. 哈哈,当然也没有这么夸张啦. 还是觉着把单链表的基本操作自己再撸一遍,这个感觉很有必要. 果然在编写的过程中发现很多问题,不过好在有些不太好理解的部分后面在草稿纸上画了一下才顺利编写出对应的代码.   我把代码贴上来也方便失忆后的恢复对链表的记忆.我估计后面自己看代码都有可能看不太明白.  

[数据结构]链表的基本操作

链表 可以用任意一组存储单元来存储单链表中数据元素(存储单元可以是不连续的),而且,除了存储每个数据元素ai的值以外,还必须存储指示其直接后继元素的信息.这两部分信息组成的数据元素ai的存储映像称为结点.N个结点链在一起被称为链表,当结点只包含其后继结点的信息的链表就被称为单链表. 链表定义如下数据类来存储结点信息: public class Node { Node next = null; int data; public Node(int data){ this.data = data; }

链表的基本操作之_增删改查

<span style="font-size:32px;"><strong style="background-color: rgb(51, 255, 51);">链表的基本操作之_增删改查</strong></span> #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct Node { int