简单链表操作

来源:互联网 发布:pc蛋蛋app源码 编辑:程序博客网 时间:2024/06/07 03:46

先是最简单的,创建,删除节点,有序链表添加节点,反转等,代码如下:

#include <iostream>using namespace std;struct Node{        int data;        Node *next;};Node *createLink(int *a, int len){        Node *head = NULL;        Node *tail = NULL;        for(int i=0; i<len; i++)        {                if(i == 0)                {                        head = new Node;                        head->next = NULL;                        head->data = a[i];                        tail = head;                }                else                {                        Node *temp = new Node;                        temp->next = NULL;                        temp->data = a[i];                        tail->next = temp;                        tail = temp;                }        }        return head;}void show(Node *head){        while(head)        {                cout<<head->data<<" ";                head = head->next;        }        cout<<endl;}void deleteNode(Node *&head, int data){        Node *preNode = NULL;        Node *tempHead = NULL;        while(head) //如果头结点是要删除的节点        {                if(head->data == data)                {                        tempHead = head;                        head = head->next;                        delete [] tempHead;                }                else                        break;        }        tempHead = head->next;        preNode = head;        while(tempHead) //不是头的情况        {                if(tempHead->data == data)                {                        preNode->next = tempHead->next;                        delete [] tempHead;                        tempHead = preNode->next;                }                else                {                        preNode = preNode->next;                        tempHead = tempHead->next;                }        }}void insertNode(Node *&head, int data){        Node *insert = new Node;        insert->data = data;        insert->next = NULL;        if(data < head->data) //头结点的情况特殊处理        {                insert->next = head;                head = insert;                return;        }        Node *pre = head;        Node *tempHead = head->next;        while(tempHead)//寻找pre节点        {                if(tempHead->data > data)                        break;                else                {                        pre = tempHead;                        tempHead = tempHead->next;                }        }        insert->next = pre->next;        pre->next = insert;}void reverseLink(Node *&head){        if(head == NULL)                return;        Node *p1 = head;        Node *p2 = head->next;        p1->next = NULL;        while(p2)        {                Node *p3 = p2->next;                p2->next = p1;                p1 = p2;                p2 = p3;        }        head = p1;}int main(){        int a[] = {1, 2, 3, 4, 5};        Node *head = createLink(a, 5);        cout<<"the link is:"<<endl;        show(head);        deleteNode(head, 1);        cout<<"after delete:"<<endl;        show(head);        insertNode(head, 0);        cout<<"after insert:"<<endl;        show(head);        reverseLink(head);        cout<<"after reverse:"<<endl;        show(head);        return 0;}