用指针实现链表的创建及部分功能

版权声明:个人笔记,仅供复习 https://blog.csdn.net/weixin_42373330/article/details/88706958

#pragma comment(linker, "/STACK:10240000,10240000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
using namespace std;
#define ll long long
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
#define N 110
typedef struct node
{
    int data;
    struct node *next;
}LinkList;
LinkList * p;
void creat(LinkList * &L, int n)    //建立带头节点的链表//每次在头结点的后面一位插入
//比如输入 2 3 4 5 6,链表对应的是:头-6-5-4-3-2-尾
{
    L = (LinkList *)malloc(sizeof(node));
    L->next = NULL;
    for (int i = n; i > 0; i--){
        LinkList *p = (LinkList *)malloc(sizeof(node));
        cin >> p->data;
        p->next = L->next;
        L->next = p;
    }
}
/*若想顺序输入到链表中,则必须再定义一个指针q来放链表的尾部
void creat(LinkList * &L, int n)    //建立带头节点的链表//每次在头结点的后面一位插入
//比如输入 2 3 4 5 6,链表对应的是:头-2-3-4-5-6-尾
{
    L = (LinkList *)malloc(sizeof(node));
    L->next = NULL;
    LinkList *q=L;
    for (int i = n; i > 0; i--){
        LinkList *p = (LinkList *)malloc(sizeof(node));
        cin >> p->data;
        p->next = q->next;
        q->next = p;
        q=p;
    }
}
*/

LinkList * findx(LinkList *L, int x)      //找到第一个指向x的指针
{
    LinkList *p = L->next;
    while(p){
        if(p->data == x)
            return p;
        p = p->next;
    }
    return NULL;
}

int count(LinkList *head, int x)    //返回链表中x元素的个数
{
    LinkList *p = head->next;
    int ans = 0;
    while(p){
        if(p->data == x)
            ans++;
        p = p->next;
    }
    return ans;
}


int main()
{
    LinkList *L;
    creat(L, 5);
    LinkList *q = findx(L, 1);
   cout << q->data << endl;
   cout << count(L, 3) << endl;
    return 0;
}





creat(LinkList *&L,int x)(在上面的代码,必须加&)函数中的L = (LinkList *)malloc(sizeof(node)); L->next = NULL;若是写在在main()函数中时,则不能加&,就是下面的这样:

#pragma comment(linker, "/STACK:10240000,10240000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
using namespace std;
#define ll long long
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
#define N 110
typedef struct node
{
    int data;
    struct node *next;
}LinkList;
LinkList * p;
void creat(LinkList * L, int n)    //建立带头节点的链表//每次在L的尾部插入数据
//比如说输入2 3 4 5 6,对应的链表是:头-2-3-4-5-6-尾
{

    for (int i = n; i > 0; i--){
        LinkList *p = (LinkList *)malloc(sizeof(node));
        cin >> p->data;
        p->next = L->next;
        L->next = p;
        L=p;

    }
}

void print(LinkList *L, int x)      //找到第一个指向x的指针
{
    LinkList *p = L->next;
    while(p){
        cout<<p->data<<" ";
        p=p->next;
    }

}

int count(LinkList *head, int x)    //返回链表中x元素的个数
{
    LinkList *p = head->next;
    int ans = 0;
    while(p){
        if(p->data == x)
            ans++;
        p = p->next;
    }
    return ans;
}


int main()
{
    LinkList *L;
    L = (LinkList *)malloc(sizeof(node));
    L->next = NULL;
    creat(L, 5);
    print(L, 1);
   //cout << q->data << endl;
   //cout << count(L, 3) << endl;
    return 0;
}

总结一下就是,要注意头指针不能被覆盖掉,然后慢慢体会一下就O了

猜你喜欢

转载自blog.csdn.net/weixin_42373330/article/details/88706958