SinglyLinkList 3 -- Insert

Implement:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct node
{
    int data;
    struct node *next;
} node;

node *LinkList;
void display(node *list);
node *insert(node *list, int data);

void main()
{
    LinkList = insert(LinkList, 1);
    display(LinkList);
    LinkList = insert(LinkList, 2);
    display(LinkList);
}

node* create(node *list)
{
    list = (node *)malloc(sizeof(node));
    return list;
}

void display(node *list)
{
    while(list)
    {
        printf("address:%d, data: %d, its next: %d\n", list, list->data, list->next);
        list = list->next;
    }
}

node* insert(node *list, int data)
{
    node* head = list;

    /* If the list is empty*/
    if(list==NULL)
    {
        list = (node *)malloc(sizeof(node));
        list->data = data;
        list->next = NULL;

        head = list;
        return head;
    }
    /* Iterate through the list till we encounter the last node. */
    while(list->next != NULL)
    {
        list = list->next;
    }

    /* Allocate memory for the new node and put data in it */
    list->next = (node *)malloc(sizeof(node));
    list = list->next;
    list->data = data;
    list->next = NULL;

    return head;
}

Procedure Figure:

Output:

gcc SinglyLinkList3.c -lcurses -o SinglyLinkList3
./SinglyLinkList3address:19963920, data: 1, its next: 0
address:19963920, data: 1, its next: 19963952
address:19963952, data: 2, its next: 0

Remark:

1) Because the code "(node *)malloc(sizeof(node));" will return a node with default value, that‘s why there is no Initialize function implemented.

2) Because C Language doesn‘t support the "&" address-of operator in parameters, so we need to restore the head pointer and return it in Insert function.

3) There will be 2 different operations between empty list and non-empty list.

时间: 2024-08-26 14:26:50

SinglyLinkList 3 -- Insert的相关文章

专利事务所信息Python爬取

数据来源:http://www.acpaa.cn/ 目前事务所的信息没有做反爬限制,还是很容易拿到数据的 没有用html解析工具,直接上正则,结果就是需要处理很多乱七八糟的空格...为了能将日期顺利的插入到数据库,做了很多转换.这个代码没用多线程. 下面是代码,Python版本为3.5,需要安装pymsql,mysql # -*- coding: UTF-8 -*- import http.client import re import pymysql def saveAgency(code,

【Leetcode】Insert Delete GetRandom O(1) - Duplicates allowed

题目链接:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ 题目: Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the c

Bulk Insert Syntax

BULK INSERT Imports a data file into a database table or view in a user-specified format. BULK INSERT [ database_name . [ schema_name ] . | schema_name . ] [ table_name | view_name ] FROM 'data_file' [ WITH ( [ [ , ] BATCHSIZE = batch_size ] [ [ , ]

实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中,vector A中的结果我们可想而知,但是vector B中的元素还会如何? 看看之前写过的程序: #include <iostream> #include <vector> int main () { std::vector<int> myvector (3,100); std::vector<int>::iterator it; it = myvector

LeetCode Insert Interval

原题链接在这里:https://leetcode.com/problems/insert-interval/ AC Java: 1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8

Java MongoDB insert

public class  Foo implements IdObject { public String id;    public String firstName;    private String lastName;        @Override     String getId() {        return id;    }        @Override    public void toDocument(Document doc) {        doc.appen

【MongoDB学习笔记6】深入MongoDB的创建/插入(insert)

简单单个文档插入用insert方法: > db.post.insert({"bar":"baz"});     WriteResult({ "nInserted" : 1 }) 批量插入,用insert方法(参数要是一个文档数组): > db.post.insert([{"_id":0},{"_id":1},{"_id":2}]);    BulkWriteResult({ 

【MongoDB】The basic operation of Mongodb, Insert\Query\Delete\Update

1, Insert MongoDB is database storing document object, the type of which is called Bson.(like JSON); Example:  // document defination Now after using command[db.posts.insert(doc)], you will insert record successfully if seeing the The following pictu

veridata实验举例(3)验证veridata查找出insert操作导致的不同步现象

veridata实验举例(3)验证veridata查找出insert操作导致的不同步现象 续接:<veridata实验举例(2)验证表BONUS与表SALGRADE两节点同步情况>,地址:点击打开链接 环境: Item Source System Target System Platform Red Hat Enterprise Linux Server release 5.4 Red Hat Enterprise Linux Server release 5.4 Hostname gc1 g