创建链表、往链表中插入数据、删除数据等操作,以单链表为例。


1.使用C语言创建一个链表:

typedef struct nd{
  int data;
  struct nd* next; } node;
//初始化得到一个链表头节点
node* init(void){
   node* head=(node*)malloc(sizeof(node));
  if(head==NULL) return NULL;
  head->next=NULL;
  return head;
}
//在链表尾部插入数据
void insert(node* head,int data){
   if(head==NULL) return;
  node* p=head;
  while(p->next!=NULL)
    p=p->next;
  node* new=(node*)malloc(sizeof(node));
   if(new==NULL) return;
  new->data=data;
  new->next=NULL;//新节点作为链表的尾节点
  p->next=new;//将新的节点链接到链表尾部
}
//从链表中删除一个节点,这里返回值为空,即不返回删除的节点
void delete(node* head,int data){
  if(head==NULL) return ;
  node *p=head;
  if(head->data==data){//如何头节点为要删除的节点
    head=head->next;//更新链表的头节点为头节点的下一个节点
    free(p);
    return;
  }
  node *q=head->next;
  while(q!=NULL){
     if(q->data==data){//找到要删除的节点q
      node *del=q;
      p->next=q->next;
       free(del);
     }
    p=q;//不是要删除的节点,则更新p、q,继续往后找
    q=q->next;
   }
}

2.Java创建链表

创建一个链表

class Node {
  Node next = null;
   int data;
  public Node(int d) { data = d; }
  void appendToTail(int d) {//添加数据到链表尾部
    Node end = new Node(d);
    Node n = this;
    while (n.next != null) { n = n.next; }
    n.next = end;
  }
}

从单链表中删除一个节点

Node deleteNode(Node head, int d) {
   Node n = head;
  if (n.data == d) { return head.next; /* moved head */ }
  while (n.next != null) {
    if (n.next.data == d) {
       n.next = n.next.next;
       return head; /* head didn’t change */
    } n = n.next;
   }
}

作者:Viidiot   微信公众号:linux-code

[google面试CTCI] 2-0.链表的创建的更多相关文章

  1. [google面试CTCI] 2-1.移除链表中重复元素

    [链表] Q:Write code to remove duplicates from an unsorted linked list      FOLLOW UP      How would yo ...

  2. [google面试CTCI] 2-2 找出链表的倒数第n个节点元素

    [链表] Q:Implement an algorithm to find the nth to last element of a singly  linked list . 题目:找出链表的倒数第 ...

  3. [google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?

    [链表] Q:Implement an algorithm to delete a node in the middle of a single linked list, given only acc ...

  4. [google面试CTCI] 1-7.将矩阵中特定行、列置0

    [字符串与数组] Q:Write an algorithm such that if an element in an MxN matrix is 0, its entire row and colu ...

  5. [google面试CTCI] 1-8.判断子字符串

    [字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...

  6. [google面试CTCI] 1-6.图像旋转问题

    [字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...

  7. [google面试CTCI] 1-5.替换字符串中特定字符

    [字符串与数组] Q:Write a method to replace all spaces in a string with ‘%20’ 题目:写一个算法将一个字符串中的空格替换成%20 解答: ...

  8. [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成

    [字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...

  9. [google面试CTCI]1-3.字符串去重

    [字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...

随机推荐

  1. 在ASP.net中的UpdatePanel,弹窗失败解决办法

    原文:在ASP.net中的UpdatePanel,弹窗失败解决办法 最开始我用: Response.Write("<script>alert('和哈呵呵呵呵呵呵!')</s ...

  2. 【网络流量-二部图最大匹配】poj3041Asteroids

    /* 这个问题将是每行一个x作为节点x,没有列y作为节点y,障碍物的坐标xy来自x至y的 边缘.图建的问题后,变成,拿得最少的点,因此,所有这些点与相邻边缘,即最小 点覆盖,与匈牙利算法来解决. -- ...

  3. C#快递跟踪(基于快递100深度定制)

    本文主要介绍快递跟踪的相关信息.如根据快递单号预测所属快递公司,判断快递是否已被签收,以及改良官方model后可在不用申请授权的情况下实现json,html,xml及text等多种格式以及单行多行,降 ...

  4. 2014鞍山直播比赛H称号HDU5077(DFS修剪+通过计)

    NAND Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Sub ...

  5. MVC 接受Flash上传图片

    /// <summary> /// 经Flash上传图片 /// </summary> /// <param name="uid"></p ...

  6. GitHub Top 100 简介

    主要对当前 GitHub 排名前 100 的项目做一个简单的简介, 方便初学者快速了解到当前 Objective-C 在 GitHub 的情况. GitHub 地址:https://github.co ...

  7. .net的自定义JS控件,运用了 面向对象的思想 封装 了 控件(.net自定义控件开发的第一天)

    大家好!我叫刘晶,很高兴你能看到我分享的文章!希望能对你有帮助! 首先我们来看下几个例子 ,就能看到 如何 自定义控件! 业务需求: 制作  一个   属于 自己的    按钮 对象    ,然后 像 ...

  8. php正则函数学习

    原文:php正则函数学习 <?php /** * php正则函数学习 * * 原来的ereg 和eregi 函数已经废弃掉了,目前版本用preg_match代替 * * preg_match 在 ...

  9. The Swift Programming Language-官方教程精译Swift(6)控制流--Control Flow

    Swift提供了类似C语言的流程控制结构,包括可以多次执行任务的for和while循环,基于特定条件选择执行不同代码分支的if和switch语句,还有控制流程跳转到其他代码的break和continu ...

  10. 在线Youtube视频下载,修改文本,剪切制作动画的最新方法

    刚刚(减去编写本文章的时间,大概20分钟前吧)在看国外最新技术资讯的时候发现有个方法可以让我们快速去下载Youtube上面的视频,不敢独享,我自己都没有怎么玩就所以立刻post上来广而告之,希望对大家 ...