单链表增删改查

一、单链表增删改查代码实现

#include<iostream>
using namespace std;
typedef struct Node {
    struct Node* pNext;
    int data;
}NODE, * pNODE;
/*创建单链表,头结点不存放任何数据*/
pNODE CreateSingleList(void) {
    int length, element;
    pNODE pHead = (pNODE)malloc(sizeof(NODE));
    if (NULL == pHead) {
        cout << "内存分配失败!" << endl;
        exit(EXIT_FAILURE);
    }
    pHead->data = 0;
    pHead->pNext = NULL;
    pNODE pCurrent = pHead;
    cin >> length;
    cout << "所创建的单链表的长度:" << length << endl;
    for (int i = 1; i <= length; i++) {
        pNODE pNew = (pNODE)malloc(sizeof(NODE));
        cin >> element;
        pNew->data = element;
        pNew->pNext = NULL;
        pCurrent->pNext = pNew;
        pCurrent = pCurrent->pNext;
    }
    return pHead;
}
/*单链表反转*/
pNODE InvertSingleList(pNODE pHead) {

    pNODE pInverseHead = (pNODE)malloc(sizeof(NODE));
    if (NULL == pInverseHead) {
        cout << "内存分配失败!" << endl;
        exit(EXIT_FAILURE);
    }
    pInverseHead->data = 0;
    //pInverseHead->pNext = NULL;
    pNODE pCurrent = pHead->pNext;
    pNODE pPre = NULL;
    while (pCurrent) {
        pNODE pnode = pCurrent->pNext;
        pCurrent->pNext = pPre;
        pPre = pCurrent;
        pCurrent = pnode;
    }
    pInverseHead->pNext = pPre;
    return pInverseHead;
}
/*单链表打印*/
void PrintSingleList(pNODE phead) {
    pNODE pCurrent = phead->pNext;
    cout << "链表元素如下:" << endl;
    while (pCurrent != NULL) {
        cout << pCurrent->data << " ";
        pCurrent = pCurrent->pNext;
        if (!pCurrent) cout << " NULL " << endl;
    }
}
/*单链表长度计算*/
int CalaulateSingleListLength(pNODE phead) {
    int length = 0;
    pNODE pCurrent = phead->pNext;
    while (pCurrent != NULL) {
        pCurrent = pCurrent->pNext;
        length++;
    }
    return length;
}
/*插入单链表元素*/
int InsertSingleList(pNODE phead, int pos, int data) {
    pNODE pInsertNode = pNODE(malloc(sizeof(NODE)));
    if (NULL == pInsertNode) {
        cout << "pInsertNode 内存分配失败!" << endl;
        exit(EXIT_FAILURE);
    }
    pNODE pCurrent = phead->pNext;
    pInsertNode->data = data;
    for (int i = 1; i < pos - 1; i++) {
        pCurrent = pCurrent->pNext;
    }
    pInsertNode->pNext = pCurrent->pNext;
    pCurrent->pNext = pInsertNode;
    return 0;
}
/*删除单链表元素*/
int DeleteSingleList(pNODE phead, int pos) {
    pNODE pCurrent = phead->pNext;
    if (1 == pos) {
        phead->pNext = phead->pNext->pNext;
    }
    else if (2 == pos) {
        phead->pNext->pNext = phead->pNext->pNext->pNext;
    }
    else {
        for (int i = 1; i < pos - 1; i++) {
            pCurrent = pCurrent->pNext;
        }
        pCurrent->pNext = pCurrent->pNext->pNext;
    }
    return 0;
}
/*判断是否存在环形链表*/
bool IsLoopExist(pNODE phead) {
    pNODE pCurrent = phead->pNext;
    pNODE pFastNode = phead->pNext;
    pNODE pSlowNode = phead->pNext;
    if (pCurrent->pNext == NULL || pCurrent == NULL) {
        return false;
    }
    while (pFastNode != NULL && pFastNode->pNext != NULL) {
        pFastNode = pFastNode->pNext->pNext;
        pSlowNode = pSlowNode->pNext;
        if (pFastNode == pSlowNode)
            return true;
    }
    return false;
}
/*寻找链表中的中间元素*/
pNODE SearchMiddleNode(pNODE phead) {
    int length = 0 ;
    pNODE pCurrent = phead->pNext;
    length = CalaulateSingleListLength(phead);
    cout << "链表长度:" << length << endl;
    if (length % 2 == 0) {
        length = length / 2;
    }
    else 
        length = length/2 + 1;
    while (--length)
        pCurrent = pCurrent->pNext;
    return pCurrent;
}
int main(int argc, char* argv[]) {
    pNODE head = NULL;
    head = CreateSingleList();
    PrintSingleList(head);
    pNODE InverseHead = InvertSingleList(head);
    PrintSingleList(InverseHead);
    InsertSingleList(InverseHead, 4, 100);
    PrintSingleList(InverseHead);
    DeleteSingleList(InverseHead, 4);
    PrintSingleList(InverseHead);
}

二、测试验证

9
所创建的单链表的长度:9
1 2 3 4 5 6 7 8 9
链表元素如下:
1 2 3 4 5 6 7 8 9  NULL 
链表元素如下:
9 8 7 6 5 4 3 2 1  NULL 
链表元素如下:
9 8 7 100 6 5 4 3 2 1  NULL 
链表元素如下:
9 8 7 6 5 4 3 2 1  NULL 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以帮你解答这个问题。 首先,我们可以定义一个火车售票的节点类,包含以下属性: - 车次号 - 出发站点 - 到达站点 - 出发时间 - 到达时间 - 座位类型 - 票价 - 下一节点指针 接下来,我们可以定义一个火车售票单链表类,实现以下方法: 1. 增加节点:在链表末尾添加节点 2. 删除节点:根据车次号删除指定节点 3. 修改节点:根据车次号修改指定节点 4. 查询节点:根据车次号查询指定节点信息 代码实现如下: ```python class TrainTicketNode: def __init__(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): self.train_num = train_num self.start_station = start_station self.end_station = end_station self.start_time = start_time self.end_time = end_time self.seat_type = seat_type self.ticket_price = ticket_price self.next = None class TrainTicketLinkedList: def __init__(self): self.head = None # 添加节点 def add_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): new_node = TrainTicketNode(train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price) if self.head is None: self.head = new_node else: current_node = self.head while current_node.next is not None: current_node = current_node.next current_node.next = new_node # 删除节点 def delete_node(self, train_num): if self.head is None: print("链表为空") else: if self.head.train_num == train_num: self.head = self.head.next else: current_node = self.head while current_node.next is not None and current_node.next.train_num != train_num: current_node = current_node.next if current_node.next is None: print("未找到该车次信息") else: current_node.next = current_node.next.next # 修改节点 def modify_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): if self.head is None: print("链表为空") else: current_node = self.head while current_node is not None and current_node.train_num != train_num: current_node = current_node.next if current_node is None: print("未找到该车次信息") else: current_node.start_station = start_station current_node.end_station = end_station current_node.start_time = start_time current_node.end_time = end_time current_node.seat_type = seat_type current_node.ticket_price = ticket_price # 查询节点 def search_node(self, train_num): if self.head is None: print("链表为空") else: current_node = self.head while current_node is not None and current_node.train_num != train_num: current_node = current_node.next if current_node is None: print("未找到该车次信息") else: print("车次号:{}\n出发站点:{}\n到达站点:{}\n出发时间:{}\n到达时间:{}\n座位类型:{}\n票价:{}".format( current_node.train_num, current_node.start_station, current_node.end_station, current_node.start_time, current_node.end_time, current_node.seat_type, current_node.ticket_price)) ``` 这样,我们就实现了火车售票单链表增删改查功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无色界神力

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

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

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

打赏作者

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

抵扣说明:

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

余额充值