OJ《程序设计基础II》实验2——链表

2-1 A - 数据结构实验之链表一:顺序建立链表

#include<stdio.h>
#include<string.h>
typedef struct Node//typedef使用后,下面的结构体可不用struct
{
    int data;
    struct Node *next;
}node;
node *creat(int n)
{
    node *head,*p,*tail;
    head=(node*)malloc(sizeof(node));//malloc函数,建立一个头节点;
    head->next=NULL;//头节点指向空;
    tail=head;//尾节点指向空;
    for(int i=0;i<n;i++)
    {
        p=(node*)malloc(sizeof(node));//建立一个新的节点p;
        scanf("%d",&p->data);//给节点p赋值;
        p->next=NULL;//p指向空;
        tail->next=p;//让tail指向p;
        tail=p;//将p代替tail的位置进行循环,可以实现将新的p顺序加到链表中;
    }//顺序建链表;
    return head;
}
int main()
{
    node *head,*p;
    int n;
    scanf("%d",&n);
    head=creat(n);//顺序建链表;
    p=head->next;//遍历链表;
    while(p)//也可以写while(p!=NULL)
    {
        if(p->next==NULL)
            printf("%d",p->data);
        else printf("%d ",p->data);
        p=p->next;
    }//输出;
    return 0;
}

2-2 B - 数据结构实验之链表二:逆序建立链表

#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}node;
node *creat(int n)
{
    node *head,*p;
    head=(node*)malloc(sizeof(node));
    head->next = NULL;//和顺序一样;
    for(int i=0;i<n;i++)
    {
        p=(node*)malloc(sizeof(node));
        scanf("%d",&p->data);
        p->next=head->next;//让p指向head所指的;
        head->next=p;//让head指向p,实现将p逆序建立链表。
    }
    return head;
}
 
int main()
{
    int n;
    scanf("%d",&n);
    node *head,*p;
    head=creat(n);
    p=head->next;
    while(p)
    {
        if(p->next==NULL)
        printf("%d",p->data);
        else printf("%d ",p->data);
        p=p->next;
    }
    return 0;
}
 

2-3 C - 师--链表的结点插入

#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}node;
void Insert(node *head,int m,int x)//执行插入操作的函数
{
    node *q=(node *)malloc(sizeof(node)),*p=head;
    q->data=x;//准备好新节点并赋值
    for(int i=0;i<m&&p->next!=NULL;i++)//查找第m个,但如果p->next==NULL也要结束循环
    {
        p=p->next;//建立链表往后走
    }
    q->next=p->next;//将q插入找到的位置
    p->next=q;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        node *head,*p;
        head=(node*)malloc(sizeof(node));
        head->next&#
### 关于山东理工大学PTA平台上的《程序设计基础2》课程中的实验27-2 虽然当前提供的引用并未直接提及山东理工大学PTA平台上《程序设计基础2》课程中实验27-2的具体内容,但从上下文中可以推测该实验可能涉及链表的实现与应用。以下是基于链表基础知识以及常见编程实践的内容解析。 #### 链表基础概念 链表是一种常见的数据结构,由一系列节点组成,每个节点包含两部分:存储的数据和指向下一个节点的指针。单向链表仅能从前向后遍历,而双向链表则允许前后两个方向的访问[^4]。 #### 单链表的基本操作 对于单链表而言,其基本操作通常包括以下几个方面: 1. **创建链表** 创建一个空链表或者通过一组初始值构建链表2. **插入节点** 插入操作可以在头部、尾部或指定位置完成。例如,在头节点前插入新节点的操作如下所示: ```cpp void insertAtHead(Node*& head, int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = head; head = newNode; } ``` 3. **删除节点** 删除特定值的节点需要找到目标节点及其前置节点并调整指针关系。 ```cpp void deleteNode(Node*& head, int key) { if (!head) return; if (head->data == key) { Node* temp = head; head = head->next; delete temp; return; } Node* current = head; while (current && current->next && current->next->data != key) { current = current->next; } if (current && current->next) { Node* temp = current->next; current->next = current->next->next; delete temp; } } ``` 4. **查找节点** 查找某个值是否存在可以通过线性扫描来完成。 ```cpp bool search(Node* head, int key) { Node* current = head; while (current) { if (current->data == key) return true; current = current->next; } return false; } ``` 5. **反转链表** 反转链表是一个经典问题,其实现逻辑较为复杂但也非常重要。 ```cpp Node* reverseList(Node* head) { Node* prev = nullptr; Node* curr = head; while (curr) { Node* nextTemp = curr->next; curr->next = prev; prev = curr; curr = nextTemp; } return prev; } ``` 以上代码片段展示了如何在C++中处理一些典型的单链表操作[^5]。 #### 实验27-2可能考察的知识点 根据上述分析,实验27-2可能会围绕以下主题展开: - 链表的初始化与销毁; - 节点的增删改查; - 特殊功能实现(如链表逆置、合并有序链表等)。 如果具体题目未提供,则可以根据这些核心知识点自行练习巩固。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CRAEN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值