• 单链表


    链表是面试中一个重要的内容:

    1 实现一个单链表的建立測长,打印 

    1. #include "stdafx.h"  
    2. #include <iostream>  
    3. #include <stdio.h>  
    4. #include <string.h>  
    5. #include <conio.h>  //控制台输入输出
    6.   
    7. typedef struct student  
    8. {  
    9.     int data;  
    10.     struct student *next;  
    11. }node;  
    12.   
    13. //建立单链表  
    14. node* create()  
    15. {  
    16.     node *head,*p,*s;  
    17.     int x, cycle = 1;  
    18.     head = (node *)malloc(sizeof(node));  
    19.     p = head;  
    20.     while(cycle)  
    21.     {  
    22.         std::cout<<"please input the data: ";  
    23.         std::cin>>x;  
    24.         std::cout<<std::endl;  
    25.         if(x != 0)  
    26.         {  
    27.             s = (node *)malloc(sizeof(node));  
    28.             s->data = x;  
    29.             p->next = s;  
    30.             p = s;  
    31.         }  
    32.         else  
    33.             cycle = 0;  
    34.     }  
    35.     head = head->next;  
    36.     p->next = NULL;  
    37.     return head;  
    38. }  
    39. //单链表測长  
    40. int length(node *head)  
    41. {  
    42.     int n = 0;  
    43.     node *p;  
    44.     p = head;  
    45.     while(p != NULL)  
    46.     {  
    47.         p = p->next;  
    48.         n++;  
    49.     }  
    50.     return n;  
    51. }  
    52. //单链表打印  
    53. void print(node *head)  
    54. {  
    55.     node *p;  
    56.     int n;  
    57.     n = length(head);  
    58.     std::cout<<"Now, These "<<n<<" records are: "<<std::endl;  
    59.     p = head;  
    60.     if(p != NULL)  
    61.     {  
    62.         while(p != NULL)  
    63.         {  
    64.             std::cout<<p->data<<" -> ";  
    65.             p = p->next;  
    66.         }  
    67.         std::cout<<std::endl;  
    68.     }  
    69. }  
    70.   
    71.   
    72. int _tmain(int argc, _TCHAR* argv[])  
    73. {  
    74.     node *head;  
    75.     head = create();  
    76.     print(head);  
    77.     return 0;  
    2 单链表删除结点

    1. int delete(struct Node* x,struct Node* head){  
    2.     struct Node* p = head->next;  
    3.     struct Node* pre = head;  
    4.    if(pre->value == x->value)//推断是否和头部同样
    5.    {
    6.         head=x;
    7.         head->next=p;
    8.         return 0;
    9.    }   
    10.     while(p!=NULL)
    11.    {  
    12.         if(p->value == x->value){  
    13.             pre->next = p->next;  
    14.             free(p);  
    15.             return 1;  
    16.         }else{  
    17.             pre = p;  
    18.         }  
    19.           
    20.         p = p->next;  
    21.     }  
    22.       
    23.     return 0;  
    24. }  

    3 单链表的插入

    1. int ListInsert(node *head,int num,node *new)  
    2. {  
    3.     node *pre=head;
    4.     node *p=head->next;
    5.     int i=0;
    6.     if(num==0) //开头
    7.     {
    8.       head=new;
    9.       head->next=pre;
    10.       return 0;
    11.     }
    12.     while(p!=NULL) //中间
    13.     {
    14.        if(i=num)
    15.        {
    16.          pre->next=new;
    17.          new->next=p;
    18.          return 0;
    19.        }
    20.        pre=p;
    21.        p=p->next;
    22.        ++i;
    23.     }
    24.    if(p==NULL) //结尾
    25.    {
    26.       p=new;
    27.       new->next=NULL;
    28.       return 0;
    29.    } 
    30. }  

    4链表排序

    <span style="font-size:18px;">void sort(node *head)
    {
       node *p1,*p2;
       p1=head;
       if(p1->next==NULL)
       {
         return;
       }
       while(p1)
       {
          p2=p1->next;
          while(p2)
          {
             if(p2->data < p1->data)
             {
                int temp=p1->data;
                p1->data=p2->data;
                p2->data=temp;
             }
             p2=p2->next;
          }
         p1=p1->next;
       }
    }
    </span>
    5 对于链表我们能够仅仅遍历一次就能找到中间节点,方法。建立两个指针一个每次加1,还有一个每次加2,当当中一个到达末尾,还有一个则处于中间节点位置。


  • 相关阅读:
    python库安装
    Reversing Linked List(根据输入序列对其做部分或全部反转)
    简单的一元二项(使用的是指针形式,方便调试)
    最大子序列问题
    centos6安装mysql5.5.53
    android中常用的drawable
    android四大组件之ContentProvider
    android使用shape来绘制控件
    android布局理解
    android命令行管理avd以及sqlite3命令
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7352829.html
Copyright © 2020-2023  润新知