单链表的各项操作

#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}Node;
typedef Node* node;
//从表尾到表头逆向建立单链表,头插法
node createlist(int n)
{
    node R,p;
    R=(Node *)malloc(sizeof(Node));
    R->next=NULL;
    for(int i=0;i<n;i++)
    {
        p=(Node *)malloc(sizeof(Node));
        int d;
        scanf("%d",&d);
        p->data=d;
        p->next=R->next;
        R->next=p;
    }
    return R;
}
//建立单链表尾插法
node createlist2(int n)
{
    node head,R,p;
    head=(Node *)malloc(sizeof(Node));
    head->next=NULL;
    R=head;
    for(int i=0;i<n;i++)
    {
        p=(Node *)malloc(sizeof(Node));
        scanf("%d",&p->data);
        R->next=p;
        R=p;
    }
    p->next=NULL;
    return head;
}
//单链表的插入
node insert(node R,int num)
{
    node head,p;
    head=R;
    p=R;
    R=R->next;
    while(R->data<num&&R->next!=NULL)
    {
        p=R;
        R=R->next;
    }
    node g;
    g=(Node *)malloc(sizeof(Node));
    g->data=num;
    if(R->next==NULL)
    {
        R->next=g;
        g->next=NULL;
    }
    else
    {
        p->next=g;
        g->next=R;
    }
    return head;
}
//单链表元素的个数
int ccount(node R)
{
    node p;
    R=R->next;
    int c=0;
    while(R)
    {
        c++;
        R=R->next;
    }
    return c;
}
//单链表的遍历
void display(node R)
{
    R=R->next;//头结点没有数
    printf("线性表中含有的元素如下:\n");
    while(R)
    {
        printf("%d ",R->data);
        R=R->next;
    }
    puts("");
}
//单链表的排序冒泡法从小到大
node sort(node R,int n)
{
    node head,p,pre;
    head=R;
    for(int i=0;i<n-1;i++)
    {
        pre=head;
        R=head->next;
        for(int j=0;j<n-1-i;j++)
       {
        p=R->next;
        node t;
        if((R->data) > (p->data))
        {
            t=R;
            t->next=p->next;
            pre->next=p;
            p->next=t;
            pre=p;//注意R和P交换了
            R=p->next;
        }
     //   printf("****%d   %d****\n",R->data,R->next->data);
     //   display(head);
        else
        {
         pre=R;
         R=R->next;
        }
      }
    }
    return head;
}
//单链表的逆置
node reverse(node R)
{
    node head,p,bg;
    R=R->next;
    bg=NULL;
    while(R)
    {
        p=R->next;
        R->next=bg;
        bg=R;
        R=p;
    }
    head=(Node *)malloc(sizeof(Node));//注意
    head->next=bg;
    return head;
}
//删除某个结点
node del(node R,int num)
{
    node head,p;
    head=R;
    p=R;
    R=R->next;
    while(num!=R->data&&R->next!=NULL)
    {p=R;R=R->next;}
    if(num==R->data)
    {
        p->next=R->next;
        free(R);
    }
    return head;
}
//释放
void delet(node R)
{
    node p;
    while(R)
    {
        p=R->next;
        free(R);
        R=p;
    }
}
int main()
{
    int n;
    printf("Please enter how many numbers you want to insert into list when create the list\n");
    while(scanf("%d",&n)!=EOF)
    {
        node R;
        printf("Please enter n numbers\n");
        R=createlist2(n);
        display(R);
        int g;
        printf("Please enter the number you want to delete end with -1\n");
        while(1)
        {
        scanf("%d",&g);
        if(g==-1) break;
        R=del(R,g);
        display(R);
        }
        printf("Please enter the number you want to insert  end with -1\n");
        while(1)
        {
            scanf("%d",&g);
            if(g==-1) break;
            R=insert(R,g);
            display(R);
        }
        int cc=ccount(R);
        printf("there are %d numbers in list\n",ccount(R));
        printf("sorting...\n");
        R=sort(R,cc);
        printf("after sort....\n");
        display(R);
        printf("reversing.....\n");
        R=reverse(R);
        printf("after reversing\n");
        display(R);
        delet(R);
    }
}
/*
Please enter how many numbers you want to insert into list when create the list
4
Please enter n numbers
2 4 3 1
线性表中含有的元素如下:
2 4 3 1
Please enter the number you want to delete end with -1
1
线性表中含有的元素如下:
2 4 3
2
线性表中含有的元素如下:
4 3
3
线性表中含有的元素如下:
4
-1
Please enter the number you want to insert  end with -1
2
线性表中含有的元素如下:
4 2
1
线性表中含有的元素如下:
1 4 2
3
线性表中含有的元素如下:
1 3 4 2
-1
there are 4 numbers in list
sorting...
after sort....
线性表中含有的元素如下:
1 2 3 4
reversing.....
after reversing
线性表中含有的元素如下:
4 3 2 1


*/


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值