链表的基础操作1

摘要:
=NULL){printf;p=p-˃next;}return0;}示例2:根据示例1_数据{node*p=head-˃next;intcounter=0;while(p!=NULL){ifcounter++;p=p-˃next;}查找元素intsearch返回计数器;}intmain(){inta[5]={1,4,4,5};节点*头部;head=创建链接(a,5);输出函数return0;}示例3:插入元素voidinsert_Node{Node*p=head;int i;forp=p-˃next;//注意,头节点没有数据,因此需要next Node*q=newnode;q-˃data=x;q-˃next=p-˃next;p-˃next=q;}int main(){nta[5]={1,4,7,8,5};节点*head,*p;head=创建链接(a,5);insert_node;//注意,3表示第三个位置。插入后,第三个位置是xp=head-˃next;而(p!=NULL){printf;p=p-˃next;}return0;}以下是来自http://codeup.hustoj.com/problem.php?

1.链表的重要操作

我们知道,链表的基础单位是一个个节点,那么第一步便是创建节点。

struct node{
   typename data;  //typename data 这里是数据域
   node* next ;  //指针域
};

有一点要注意的是在C++中,是可以直接使用node的,而在C语言中,则需要使用struct node 不然会显示 unknown typename "node"

第二步,内存分配,关于内存的分配,主要有C语言中的malloc 函数和 c++ 中的new运算符

node* p = (node*) malloc(sizeof(node));//malloc,头文件是stdlib.h

 但是老师更加推荐new,因为更加简洁方便

node*p = new node;

与内存分配对应的就是内存的释放

free(p); //c
delete(p) ; //c++

注意它们的效果是释放了指针变量p所指的内存空间和将指针变量p指向空地址NULL,而p本身并没有消失

 

例1:求数组中n个元素,建立其线性链表结构

#include <iostream>
#include <stdio.h>
using namespace std;
struct node{
   int data;
   node* next ;
};

struct node* Create_link(int a[],int n)
{
    int i ;
    node *p,*pre,*head;  //我们一般说知道一个节点的位置,那么这个指针一定是指向这个节点的前一个节点,故pre超级重要
    head = new node;
    head->next = NULL;
    pre = head;
    for(i=0;i<n;i++)
    {
        p = new node;
        p->data = a[i];
        p->next =NULL;
        pre->next =p;
        pre=p;
    }
    return head;
}
int main()
{
    int a[5]={1,2,3,4,5};
    node *p,*head ;
    head=Create_link(a,5);
    p=head->next;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
    return 0;
}

 例2:在例1的基础上查找元素

int search_data(node* head,int x)
{
    node* p =head->next ;
    int counter = 0 ;
    while(p!=NULL)
    {
        if(p->data == x) counter++;
        p=p->next ;
    }
    return counter ;
}
int main()
{
    int a[5]={1,4,4,4,5};
    node *head ;
    head=Create_link(a,5);
    printf("the number of x is %d",search_data(head,4));
    return 0;
}

例3:插入元素

void insert_node(node* head ,int pos,int x)
{
    node*p =head ;
    int i ;
    for(i=0;i<pos-1;i++)
        p=p->next ;         //注意头节点没有数据,所以需要多next一次
    node *q = new node ;
    q->data = x;
    q->next =p->next ;
    p->next = q;
}
int main()
{
    int a[5]={1,4,7,8,5};
    node *head,*p;

    head=Create_link(a,5);
    insert_node(head,3,100); //注意这里3是指第三个位置,那么插入之后第三个位置就是x
    p=head->next ;
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
    return 0;
}

例5:删除元素

void delect_data(node* head ,int x)
{
    node* p =head->next ;
    node *pre =head ;
    while(p!=NULL)
        {
            if(p->data ==x)
            {
                pre->next =p->next ;
                delete(p);
                p = pre->next ;
            }
            else{
            p=p->next ;
            pre=pre->next ;
            }
        }
}
int main()
{
    int a[5]={1,4,7,8,5};
    node *head,*p;

    head=Create_link(a,5);
    insert_node(head,3,100);
    p=head->next ;
    delect_data(head,100);
    while(p!=NULL)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
    return 0;
}

下面有一道较为完整的题目,来自

http://codeup.hustoj.com/problem.php?cid=100000607&pid=0

题目是:

#include <iostream>
#include <stdio.h>
#include<string>
using namespace std;
struct node{
   int data;
   node* next ;
};

struct node* Create_link(int a[],int n)
{
    int i ;
    node *p,*pre,*head;
    head = new node;
    head->next = NULL;
    pre = head;
    for(i=0;i<n;i++)
    {
        p = new node;
        p->data = a[i];
        p->next =NULL;
        pre->next =p;
        pre=p;
    }
    return head;
}
int search_data(node* head,int x)
{
    node* p =head->next ;
    int counter = 0 ;
    while(p!=NULL)
    {
        if(p->data == x) counter++;
        p=p->next ;
    }
    return counter ;
}
void insert_node(node* head ,int pos,int x)
{
    node*p =head ;
    int i ;
    if(pos-1>0)
    {
    for(i=0;i<pos-1;i++)
        p=p->next ;
    node *q = new node ;
    q->data = x;
    q->next =p->next ;
    p->next = q;
    cout<<"insert OK"<<endl;
    }
    else cout<<"insert fail"<<endl;
}
void delect_data(node* head ,int x)
{
    node* p =head->next ;
    node *pre =head ;
    int flag = 0 ;
    if(head ==NULL){
            cout<<"Link list is empty"<<endl;
    }
    while(p!=NULL)
        {
            if(p->data ==x)
            {
                pre->next =p->next ;
                delete(p);
                p = pre->next ;
                flag = 1;
            }
            else{
            p=p->next ;
            pre=pre->next ;
            }
        }
    if(flag ==0) cout<<"delete fail"<<endl;
    if(flag == 1) cout<<"delete OK"<<endl;
}
int main()
{
    int i,n,x,m,a,b,e;
    string act;
    node *head,*p,*q;
    head = new node ;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         scanf("%d",&a);
         q = new node ;
         q->data = a ;
         q->next = head->next ;
         head->next = q;
    }

    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        cin>>act;
        if(act=="get")
        {
            scanf("%d",&a);
            insert_node(head,a,1);
        }
        else if(act=="delect")
        {
             scanf("%d",&b);
            delect_data(head,b);
        }
        else if (act=="insert")
        {
             scanf("%d",&a);
              scanf("%d",&e);
            insert_node(head,e,a);
        }
        else if(act=="show")
        {
             p=head->next ;
             while(p!=NULL)
             {
                 printf("%d  ",p->data);
                 p=p->next;
             }
        }
    }

    return 0;
}

免责声明:文章转载自《链表的基础操作1》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇创建 rdlc报表SQL Server跨服务器查询下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python中和生成器协程相关的yield之最详最强解释,一看就懂(一)

yield是python中一个非常重要的关键词,所有迭代器都是yield实现的,学习python,如果不把这个yield的意思和用法彻底搞清楚,学习python的生成器,协程和异步io的时候,就会彻底懵逼。所以写一篇总结讲讲yield的东西。 分成四块来讲, 这篇先说yield基本用法,后面会重点将yield from的牛逼之处 一, 生成器中使用yiel...

使用NODEJS+REDIS开发一个消息队列以及定时任务处理

作者:RobanLee 原创文章,转载请注明: 萝卜李 http://www.robanlee.com 源码在这里: https://github.com/robanlee123/RobCron时间有限,就不详细注释,有问题或者意见欢迎@我,也欢迎大家批评指正. 本文所必须的一些资料如下: 1. NODEJS ==> 可以去NODEJS.ORG下载...

通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传

建立.NET Core Web Api项目建立请求模型 public class UserInfo { public int Age { get; set; } public string Name { get; set; } public bool Sex { get; set; }...

vue JSON数据导出为 多个sheet表的excel文件

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />...

还原数据库备份文件时,关于“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个。必须提供所有成员”的处理方式

好久没写博客了,最近在做毕设的权限管理模块,今天在还原数据库文件时,遇到了“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个。必须提供所有成员”这个错误,百度了一下,找到了原因和解决方式。 原因分析:1。备份的时候,旧路径没有删除,我添加了一个新路径,就无意中设置成了两个备份路径,SQL就会根据两个备份路径把...

vue,基于element的tree组件封装

封装组件代码 // 组件:树 /* 参数说明-属性: 1.treeData:展示数据(array) 2.treeEmptyText:内容为空的时候展示的文本(String) 3.treeNodeKey:每个树节点用来作为唯一标识的属性,整棵树应该是唯一的(String) 4.treeRenderAfterExpand:是否在第一次展开某个树节点后才渲染其子...