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


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. linux_ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机

    使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机. ssh-keygen 创建公钥和密钥. ssh-copy-id 把本地主 ...

  2. [CLR via C#]1.6 Framework类库~1.9与非托管代码的互操作性

    原文:[CLR via C#]1.6 Framework类库~1.9与非托管代码的互操作性 1.6 Framework类库 1. .NET Framework中包含了Framework类库(Frame ...

  3. SQL2005性能分析一些细节功能你是否有用到?

    原文:SQL2005性能分析一些细节功能你是否有用到? 我相信很多朋友对现在越来越大的数据量而感到苦恼,可是总要面对现实啊,包括本人在内的数据库菜鸟们在开发B/S程序时,往往只会关心自己的数据是否正确 ...

  4. .net cookie

    HttpCookie HttpRequest HttpResponse 这3个  类 . 之间的关系 . 请求 ,相应 都有一个   public HttpCookieCollection Cooki ...

  5. HHVM Installation and Configuration(HHVM 安装及配置)

    Installation and Configuration¶ General Installation Considerations Installation on Linux systems Ub ...

  6. 20款最好的免费的IDES和编辑器

    程序员编程利器:20款最好的免费的IDES和编辑器 Posted on 2015-02-17 21:52 尛鱼 阅读(170) 评论(1) 编辑 收藏 程序员编程利器:20款最好的免费的IDEs和编辑 ...

  7. MVC验证05-自定义验证规则、验证2个属性值不等

    原文:MVC验证05-自定义验证规则.验证2个属性值不等 本文体验2个属性值不等.即当一个属性输入值,另外一个属性输入的值不能和第一个属性值相等.相关文章包括: MVC验证01-基础.远程验证   M ...

  8. addEventListener

    addEventListener addEventListener-开始 前面零散地写了些关于 addEventListener 的内容,觉得比较散,有些地方可能也说得不够清楚明白,所以决定以连载的形 ...

  9. Java清理临时目录文件Demo(一)

    /** * 删除单个文件 * * @param sPath * 被删除文件的文件名 * @return 单个文件删除成功返回true,否则返回false */ public static boolea ...

  10. protobuf-net-data

    protobuf-net http://www.codeproject.com/Articles/642677/Protobuf-net-the-unofficial-manual https://g ...