单链表的基本操作

#include<bits/stdc++.h>
#include<cstdlib>
using namespace std;
typedef int ElemType;
typedef struct Node
{
    ElemType data;
    struct Node *next;

}Node, *LinkedList;
//单链表的初始化
LinkedList LinkedListInit()
{

    Node *L;
    L = (Node *)malloc(sizeof(Node));
    if (L == NULL)
        cout << "申请内存空间失败" << endl;
    L->next = NULL;
    return L;
}
//单链表的建立,头插法
LinkedList LinkedListCreatH()
{
    Node *L;
    L = (Node *)malloc(sizeof(Node));
    L->next = NULL;
    ElemType x;
    int temp = 1;
    while (temp)
    {
        cin >> x;
        if (x != 0)
        {
            Node *p;
            p = (Node *)malloc(sizeof(Node));
            p->data = x;
            p->next = L->next;
            L->next = p;
        }
        else
            temp = 0;
    }
    return L;
}
//单链表的打印
void printList(Node *head)
{
    if (head->next == NULL)
    {
        cout << "list is empty" << endl;
        return;
    }
    Node *p = head->next;
    int index = 0;
    while (p != NULL)
    {
        cout << "第" << ++index << "个元素为:" << p->data << endl;
        p = p->next;
    }
}
//单链表的测长
int getListLength(Node *head)
{
    int length = 0;
    Node *p = head->next;
    while (p != NULL)
    {
        ++length;
        p = p->next;
    }
    return length;
}
//单链表的建立2 尾插法
LinkedList LinkedListCreatT()
{


    Node *L;
    L = (Node *)malloc(sizeof(Node));
    L->next = NULL;
    Node *r;
    r = L;
    ElemType x;
    int temp = 1;
    while (temp)
    {
        cin >> x;
        if (x != 0)
        {
            Node *p;
            p = (Node *)malloc(sizeof(Node));
            p->data = x;
            r->next = p;
            r = p;
        }
        else
            temp = 0;
    }
    r->next = NULL;
    return L;
}
//单链表的插入
Node *insertList(Node *head, int pos, int data)
{
    Node * newNode = new Node;
    newNode->data = data;
    Node *p = head;
    int index = 1;
    while (p != NULL && index<pos)
    {
        p = p->next;
        ++index;
    }
    newNode->next = p->next;
    p->next = newNode;
    return head;
}
Node *deleteNode(Node *head, int pos)
{
    Node *p = head;
    if (p->next == nullptr)
    {
        cout << "链表为空" << endl;
        return nullptr;
    }
    Node *node = nullptr;
    int index = 1;
    while (p != nullptr&&index < pos)
    {
        p = p->next;
        ++index;
    }
    if (p != nullptr&&p->next != nullptr)
    {
        node = p->next;
        p->next = node->next;
        delete node;
    }
    return head;
}

int main()
{
    /* LinkedList list,start;
    cout<<"请输入单链表的数据:";
    list=LinkedListCreatH();
    for(start=list->next;start!=NULL;start=start->next)
    cout<<" "<<start->data;
    cout<<endl;
    cout<<"请输入单链表的数据:";
    list=LinkedListCreatT();
    for(start=list->next;start!=NULL;start=start->next)
    cout<<" "<<start->data;
    */
    LinkedList list;
    list = LinkedListCreatH();
    printList(list);
    list = LinkedListCreatT();
    printList(list);
    int length = getListLength(list);
    cout << "单链表的长度为: " << length << endl;
    int pos, data;
    cin >> pos >> data;
    list = insertList(list, pos, data);
    cout << "插入后的链表为: " << endl;
    printList(list);
    int pos1;
    cin >> pos1;
    list = deleteNode(list, pos1);
    cout << "删除后的链表为: " << endl;
    printList(list);
    system("pause");
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值