• 链表的基本操作(创建链表,查询元素,删除元素,插入元素等)


    复习一下链表,参考书目为:算法笔记

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 struct node 
     4 {
     5     int data;
     6     node* next;
     7 };
     8 //创建链表 
     9 node* create(int Array[])
    10 {
    11     node *p,*pre,*head;//pre保存当前结点的前驱结点,head为头结点 
    12     head = new node;//头结点的创建 
    13     head->next=NULL;//头结点不需要数据域,指针域初始为NULL 
    14     pre=head;//记录pre为head 
    15     for(int i=0;i<10;i++){
    16         p=new node;//新建结点 
    17         p->data=Array[i];//赋值 
    18         p->next=NULL;//新结点指针域设置为NULL 
    19         pre->next=p;//前驱结点的指针域指向新结点 
    20         pre=p;//把pre设为p,作为下个结点的前驱结点 
    21     }
    22     return head;//返回头结点指针 
    23 }
    24 //查找元素 
    25 //带有head头结点的链表统计数据域为x的结点个数 
    26 int search(node* head,int x)
    27 {
    28     int count=0;//计数器 
    29     node* p=head->next;//从第一个结点开始 
    30     while(p!=NULL){//循环遍历链表 
    31         if(p->data==x) count++;//值相等count加一 
    32         p=p->next;
    33     }
    34     return count;
    35 }
    36 //插入元素
    37 //将x插入以head为头结点的链表的第pos和位置上 
    38 void insert(node* head,int pos,int x)
    39 {
    40     node* p=head;
    41     for(int i=0;i<pos-1;i++){
    42         p=p->next;
    43     }
    44     node* q=new node;//创建新结点 
    45     q->data=x;
    46     q->next=p->next;
    47     p->next=q;
    48 } 
    49 //删除元素
    50 //删除以head为头结点的链表中所有数据域为x的结点
    51 void del(node* head,int x)
    52 {
    53     node* p=head;//头结点 
    54     p=p->next;//头结点后的第一个结点
    55     node* pre =head;
    56     while(p!=NULL){
    57         if(p->data==x){
    58             pre->next=p->next;
    59             delete(p);
    60             p=pre->next;
    61         }else{//数据域不是x,把pre和p都后移一位 
    62             pre=p;
    63             p=p->next;
    64         }
    65     } 
    66 } 
    67 int main()
    68 {
    69     int Array[10]={5,3,6,1,2,3,3,6,3,8};
    70     node* L=create(Array);//新建链表,返回头指针head赋值给L 
    71     node* q=L;//头结点; 
    72     L=L->next;//头结点后面接的第一个带有数据域的结点 
    73     printf("输出所建链表的元素:");
    74     while(L!=NULL){
    75         printf("%d ",L->data);//输出每个结点的数据域 
    76         L=L->next;
    77     }
    78     //统计元素3的个数
    79     printf("
    查找元素为3的个数为:%d
    ",search(q,3)); 
    80     //删除元素为6的结点
    81     del(q,6); 
    82     printf("删除素为6的结点之后的链表;"); 
    83     L=q->next;//头结点后面接的第一个带有数据域的结点 
    84     while(L!=NULL){
    85         printf("%d ",L->data);//输出每个结点的数据域 
    86         L=L->next;
    87     }
    88     //在第5个位置插入元素1000
    89     insert(q,5,1000);
    90     printf("
    在第5个位置插入元素为100的结点之后的链表;");
    91     L=q->next;//头结点后面接的第一个带有数据域的结点 
    92     while(L!=NULL){
    93         printf("%d ",L->data);//输出每个结点的数据域 
    94         L=L->next;
    95     }
    96     return 0;
    97 }

    运行结果如下图:

  • 相关阅读:
    sql-trace-10046-trcsess-and-tkprof
    教你深入理解软件包的配置、编译与安装过程
    Java RESTful 框架的性能比较
    gcc、arm-Linux-gcc和arm-elf-gcc的组成及区别
    Linux线上系统程序debug思路及方法
    使用systemtap调试Linux内核 :www.lenky.info
    SystemTap使用技巧 1
    gvfs
    Systemtap examples, Network
    .NET 大型信息化建设标准基础数据管理平台
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10759536.html
Copyright © 2020-2023  润新知