zoukankan      html  css  js  c++  java
  • 单链表的生成,插入,删除,反向

    // shuangshuang.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    #include<stdio.h>
    #include<malloc.h>
    #include<iomanip>
    //#include"myheader.h"
    using namespace std;
    
    
    
    
    
    typedef struct node
    {
        int data;
        node* next;
    }node;
    
    bool init(node*& head,int darry[],int len)//初始化一个链表,链表载入数组darry的数据,len为长度
    {
        if(darry == NULL)
            return false;
    
        head =(node*)malloc(sizeof(node));
        node *p =head;
        
    
        if(head==0)
        {
            cout<<"have not got any storage";
        }
    
        p->data=darry[0];
    
        for(int i=1;i<len;i++)
        {
    
            node *pnext =(node*)malloc(sizeof(node));
            pnext->data=darry[i];
            p->next=pnext;
            p=p->next;
        }
    
        p->next=NULL;
    }
    
    void DisplayList(node* head)
    {
        cout<<endl;
    
        while(head!=NULL)
        {
            cout<<head->data<<endl;
            head=head->next;
        }
    }
    
    
    int length(node* head)
    {
        if(head == NULL)
            return 0;
        
        int i=0;
        while(head!=NULL)
        {
            head=head->next;
            i++;
        }
        return i;
    }
    
    node* del(node*& head,int num)//删除链表中value为num的结点
    {
        node* p = head;
        node* psave = p;
        while(p->data!=num && p->next !=NULL)
        {
            psave = p;
            p=p->next;
        }
    
        if(p == head)//如果是删除头结点的话
        {
    
            head=p->next;
            free(p);
            return head;
        }
        if( p->data == num)
        {
            psave->next = p->next;
            free(p);
        }
        else
        {
            cout<<"there is no "<<num<<"in the list"<<endl;
        }
    
        return head;
    
    }
    
    bool insertList(node*& head , int num)//把num插入链表(按照data的大小)
    {
        node* p = head;
        node* inp=(node*)malloc(sizeof(node));
        inp->data = num;
    
        if(inp == NULL)
        {
            cout<<"can`t get any memory";
            return false;//没有插入返回错误
        }
    
        if(num<head->data)
        {
            inp->next=head;
            head=inp;
            return true;
        }
        while(p->next!=NULL && p->next->data < num)
        {
            p=p->next;
        }
    
    
        inp->next = p->next;
        p->next=inp;
    
        return true;
    
    
    }
    
    node* reverseRecursive(node* head)
    {
        if( head == NULL || head->next==NULL)
            return head;
    
        node* pr=reverseRecursive(head->next);
        head->next->next=head;
        head->next=NULL;
    
        return pr;
    }
    
    node* reverse(node* head) //非嵌套版反转链表
    {
        if( head == NULL || head->next==NULL)
            return head;
    
        node* p=head;
        node* pre = NULL;
        node* temp;
    
    
        while(p!=NULL)
        {
        temp=p->next;
        p->next=pre;
        pre=p;
        p=temp;
        }
    
        return pre;
    }
    
    int main()
    {
        node *head = NULL;
        int thedata[]={11,12,13,24,35};
    
        init(head,thedata,5);
    
        del(head,1312);
        DisplayList(head);
    
        insertList(head,20);
        DisplayList(head);
    
        head=reverse(head);
        DisplayList(head);
        cout<<"length:"<<length(head);
    }
  • 相关阅读:
    UVtool 工具解释1
    系统自己提供的 非常方便进行轴向的改变。
    解释脚本语言 计算两个向量的夹角度数。
    转换到正交视图的算法。
    对于 位 的判断我一般都转数组 其实这里有这个很好、
    服务器开启防火墙时千万别忘了开3389!
    j2me笔记
    成为那一小部分人!
    SEO笔记
    Java代码优化方案 J2ME内存优化
  • 原文地址:https://www.cnblogs.com/Dzhouqi/p/3630241.html
Copyright © 2011-2022 走看看