关于c语言链表的操作

来源:互联网 发布:mac 连接云服务器 编辑:程序博客网 时间:2024/05/16 13:48

这几天又讲到链表了,但是又忘记了,所以重新把关于链表的建链表,对链表进行排序,然后是删除,插入,以及遍历等功能。。但是最近要考试了,所以没有写成菜单的形式。。等考试完了,在进行补充吧。。

代码如下。。。

#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{    int data;    struct node *next;};int main(){    /*建立链表操作*/    int n,x,pos,t,number;    int i,j,temp;    struct node *head,*p,*tail,*last,*current;    struct node *p1,*p2;    printf("input numbers end of 0:\n");    head=NULL;    scanf("%d",&n);    p=(struct node *)malloc(sizeof(struct node));    p->data=n;    p->next=head;    head=p;    tail=p;    scanf("%d",&n);    number=1;    while(n)    {       number++;       p=(struct node *)malloc(sizeof(struct node));       p->data=n;       p->next=NULL;       tail->next=p;       tail=p;       scanf("%d",&n);    }    p=head;    while(p!=NULL)    {        printf("%d ",p->data);        p=p->next;    }    printf("\n");    /*建立链表操作*/     /*链表的排序操作*/     /*选择排序法*/    for(p1=head,i=0;i<number-1;i++,p1=p1->next)        for(p2=p1->next,j=i+1;j<number;j++,p2=p2->next)    {        if(p1->data>p2->data)        {            temp=p1->data;            p1->data=p2->data;            p2->data=temp;        }    }    /*冒泡排序*/    printf("排序后的链表\n");    p=head;    while(p!=NULL)    {        printf("%d ",p->data);        p=p->next;    }    printf("\n");    for(i=0;i<number-1;i++)        for(p1=head,p2=p1->next,j=0;j<number-i-1;j++,p1=p2,p2=p2->next)    {        if(p1->data>p2->data)        {            temp=p1->data;            p1->data=p2->data;            p2->data=temp;        }    }    printf("排序后的链表\n");    p=head;    while(p!=NULL)    {        printf("%d ",p->data);        p=p->next;    }    printf("\n");    /*对于删除操作要用到两个指针,一个是遍历指针,一个是last指针。*/    /*链表的插入操作*/    /*实行删除操作*/    printf("请输入命令:1代表执行 0表退出\n");    scanf("%d",&pos);    while(pos)    {    printf("请输入您想删除的数字\n");    scanf("%d",&x);    p=head;    while(p->data!=x&&p->next!=NULL)    {       last=p;       p=p->next;    }    if(head->data==x)//要删除的元素在头部     head=p->next;    else if(p->data==x)//要删除的元素在中间或则尾部     last->next=p->next;    else//错误的输入     printf("fuck 错误输入\n");    p=head;    while(p!=NULL)    {        printf("%d ",p->data);        p=p->next;    }    printf("\n");    printf("请输入命令:1代表执行 0代表退出\n");    scanf("%d",&pos);    }    /*实行删除操作*/    /*插入操作要用到3个指针,一个current指针,一个last指针,还有一个是插入的那一个*/    printf("将执行插入操作\n");    printf("请输入命令:1代表执行 0代表退出\n");    scanf("%d",&pos);    while(pos)    {        printf("请输入您想插入的数\n");        scanf("%d",&t);        p=(struct node *)malloc(sizeof(struct node));        p->data=t;        current=head;        while(current->data<t&¤t->next!=NULL)        {            last=current;            current=current->next;        }//查找t在有序数列中的位置        if(t<head->data)//如果插入的数字在头部        {            p->next=head;            head=p;        }        else if(current->next==NULL)//如果插入的数在尾部        {           p->next=NULL;           current->next=p;        }        else//插入的数在中间        {            p->next=current;            last->next=p;        }        p=head;        while(p!=NULL)        {              printf("%d ",p->data);              p=p->next;        }    printf("\n");    printf("请输入命令:1代表执行 0代表退出\n");    scanf("%d",&pos);    }    /*链表的插入操作*/    return 0;}/*测试用例1 2 15 4 10 100 78 0*/



0 0