对链表进行创建、结点的删除和插入操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yyzsir/article/details/79116608

C:

/*链表注意的问题:每个函数是独立的,而且完成一个函数需要返回头指针,说明已完成此功能,以便下一个功能使用*/
#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int num;
    struct Node*next;
};
int n;
int main()
{
    struct Node *creat();                                         //创建链表
    struct Node *del(struct Node * ,int);                         //删除结点
    struct Node *insert(struct Node *, struct Node *);            //插入节点
    void print(struct Node *);                                    //输出
    struct Node*head,stu;
    int del_num;
    printf("Input datas:\n");
    head=creat();             
    printf("Input the data that you want to delete:");
    scanf("%d",&del_num);                                         //输入想要删除的数据
    head=del(head,del_num);
    print(head);                                                  //输出删除之后的数据               
    printf("Input the data you want to insert:");     
    scanf("%d",&stu.num);                                         //输入想要插入的数据
    head=insert(head,&stu);
    print(head);                                                  //输出插入之后的数据
    return 0;
}
struct Node*creat()
{
    struct Node*head;
    struct Node *p1,*p2;
    n=0;                                                           //计算数据个数
    p1=p2=(struct Node*)malloc(sizeof(struct Node));               //先给p1和p2一个空间
    scanf("%d",&p1->num);                                          //先向第一个结点输入数据,让它进入循环
    head=NULL;
    while(p1->num!=0)                                              //输入0结束数据输入
    {
        n=n+1;
        if(n==1)head=p1;                                           //为第一个结点时,将头指针指向第一个结点
        else p2->next=p1;                                          //不是第一个结点时,前一个结点的next指向下一个结点
        p2=p1;                                                     //p2指向新结点
        p1=(struct Node*)malloc(sizeof(struct Node));              //p1建立新结点
        scanf("%d",&p1->num);                                      //向新结点里加入数据
    }
    p2->next=NULL;                                                 //循环结束时,将最后的一个结点的next赋为空
    return(head);                                                  //返回头指针,链表创建结束
}
struct Node *del(struct Node *head,int num)
{
    struct Node*p1,*p2;
    if(head==NULL)                                                //如果头结点为空
    {
        printf("\nlist NULL!\n");                                 //这是一个空表
        return(head);                                             //返回头指针
    }
    p1=head;                                                      //p1指向头指针指向的内容
    while(num!=p1->num&&p1->next!=NULL)                           //循环条件是p1指向的数据不是要删除的而且p1的next不是空 ,遍历
    {
        p2=p1;                                                  //p2指向p1指向的内容
        p1=p1->next;                                              //p1指向p1的next,也就是下一个结点
    }
    if(num==p1->num)                                            //如果找到了想要删除的数据
    {
        if(p1==head)head=p1->next;                             //如果删除的是头,那么头指向下一个结点
        else p2->next=p1->next;                                 //否则删除此节点,让p2指向被删除结点的next的指向
        printf("delete:%d\n",num);
        n=n-1;
    }
    else printf("%d Not been found!\n",num);                  //遍历结束没找到   
    return(head);                                             //返回头指针,删除结束
}
struct Node*insert(struct Node *head, struct Node *stud)
{
    struct Node *p0,*p1,*p2;
    p1=head;                                                     //p1指向头结点
    p0=stud;                                                      //p0指向传递过来的指针               
    if(head==NULL)                                               //如果是空表 ,此时插入完只有一个结点
    {
        head=p0;                                                  //头指针指向p0
        p0->next=NULL;                                           //唯一结点的next为空
    } 
    else
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))              //遍历,如果p0的数据大于p1的数据而且p1的next不是空
        {
            p2=p1;                                               //p2指向p1   
            p1=p1->next;                                        //p1指向p1的next
        }         
        if(p0->num<=p1->num)                                 //比较两个数,找到一个第一次小于的数插入 ,这个功能只会向头或尾插入
        {
            if(head==p1) head=p0;                             //如果是头结点,head指向p0
            else p2->next=p0;                                  //否则p2的next指向p0,连接过程
            p0->next=p1;                                       //p0的next指向p1
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }
    }
     n=n+1;                 
     return(head);                                            //返回头指针,插入结束
}
void print(struct Node *head)
{
    struct Node *p;
    printf("\nThese %d data are:\n",n); 
    p=head;
    if(head!=NULL)
    do
    {
        printf("%d\n",p->num);
        p=p->next;
    }while(p!=NULL);
}

C++:

/*链表注意的问题:每个函数是独立的,而且完成一个函数需要返回头指针,说明已完成此功能,以便下一个功能使用*/
#include<iostream>
#include<cstdlib>
using namespace std;
struct Node
{
    int num;
    Node *next;
};
int main()
{
    int data;
    Node stu1;
    Node *head,stu;
    Node *create();                        //创建链表 
    Node *Delete(Node *head,int data);     //删除结点 
    Node *insert(Node *head,Node *p);      //插入结点 
    void print(Node *head);                //输出链表 
    void release(Node *head);              //释放链表 

    head=create();
    cout<<"Input the data that you want to delete:";
    cin>>data;
    head=Delete(head,data);
    print(head);

    cout<<"Input the data you want to insert:";
    cin>>stu.num;
    head=insert(head,&stu);
    print(head);

    release(head);
    return 0;
}

Node *create()
{
    Node *head;
    Node *p1,*p2;
    int i;
    cout<<"Input datas:\n";          //输入数据,数据要求不重复,以0为结束标志 
    cin>>i;
    head=NULL; 
    while(i!=0)
    {
        p1=new Node;
        p1->num=i;
        if(head==NULL)              //这是一个空表 
        {
            head=p1;
            p2=p1;
        }
        else
        {
            p2->next=p1;
            p2=p1;
        }
        cin>>i;
    }
    if(head) p2->next=0;
    return head;
} 

Node *Delete(Node *head,int data)
{
    Node *p1,*p2;
    if(head==NULL)            //这是一个空表 
    {
        cout<<"list NULL!"<<endl;
        return NULL;
    } 
    if(head->num==data)     //删除的结点是首结点 
    {
        p2=head;
        head=head->next;
        delete p2;
    }
    else 
    {
        p2=p1=head;
        while(p2->num!=data&&p2->next!=NULL)     //查找要删除的结点 
        {
            p1=p2;
            p2=p2->next;
        }
        if(p2->num==data)                       //删除的中间结点或尾结点 
        {
            p1->next=p2->next;
            delete p2;
        }
        else cout<<"Data not been found!";      //找不到要删除的结点 
    }
    return head;
}

Node *insert(Node *head,Node *p)
{
    Node *p1,*p2;
    if(head==NULL)                     //这是一个空表 
    {
        head=p;
        p->next=0;
        return head;
    }
    if(head->num>=p->num)            //插入的结点作为新链表的首结点 
    {
        p->next=head;
        head=p;
        return head;
    }
    p2=p1=head;             //查找插入位置,使p1和p2指向首结点 
    while(p2->next&&p2->num<p->num)
    {
        p1=p2;
        p2=p2->next;
    }
    if(p2->num<p->num)            //在链表尾部插入结点 
    {
        p2->next=p;
        p->next=NULL;
    }
    else                //在链表中间插入 
    {
        p->next=p2;
        p1->next=p;
    }
    return head;
}

void print(Node *head)
{
    Node *p=head;
    if(p==NULL)
    {
        cout<<"list NULL!"<<endl;
        return ;
    }
    cout<<"These data in list are:"<<endl;
    while(p!=NULL)
    {
        cout<<p->num<<'\t';
        p=p->next;
    }
    cout<<endl;
}

void release(Node *head)
{
    if(head==NULL)
    {
        cout<<"list NULL!"<<endl;
        return ;
    }
    Node *p;
    while(head)
    {
        p=head;
        head=head->next;
        delete p;
    }
    cout<<"Node has been released!"<<endl;
} 

猜你喜欢

转载自blog.csdn.net/yyzsir/article/details/79116608
今日推荐