您的位置:首页 > 其它

单向链表的建立、打印、 删除

2015-09-22 15:55 363 查看
#include <stdlib.h> /*含ma l l o c ( ) 的头文件*/  
#include <stdio.h>  
typedef struct Student
{
 int data;
 struct Student *next;
 }node;

node* del(node *head, int num)//删除链表中的一个数(最早出现)
{
    node *point,*temp;
    point=head;
    while((num!=point->data)&&(point->next!=NULL))
    {
        temp=point;  
        printf("want to find %d\n",point->data);
        point=point->next;
      
    } 
    if(num==point->data)
    {
    <span style="white-space:pre">	</span>printf("have found %d\n",point->data);
        if (point==head)
        {
            point =head->next;
            free(point);
        }
        else
        {
            temp->next=point->next;
           
        }
    }
    else
    {
        printf("\n%d 找不到\n",num);
    }
    return(head);
}  
node* creat(){    node *head , *temp , *s;    int x,cycle=1;    head =(node*)malloc(sizeof(node));//申请空间 head存储在    temp=head;    printf(" temp=%d\n temp->next=%d\n head=%d\n  head->next=%d\n",temp,temp->next,head,head->next );    while(cycle)    {        printf("请输入数据:\n");        scanf("%d",&x);        if (x!=0)        {            s=(node*)malloc(sizeof(node));            temp->next=s;            temp=s;     //建立链接,注意顺序            s->data=x;            printf("%d is saved in %d \n",s->data,s);            printf(" temp=%d\n temp->next=%d\n head=%d\n  head->next=%d\n",temp,temp->next,head,head->next );        }        else        {        cycle=0;        }    }     head=head->next;  //赋值头链接地址    temp->next=NULL;    printf("head is in %d\n",head );    return(head);}void print(node *head ){    node *p;    p=head;    if (head!=NULL)    {        while(p!=NULL)        {            printf("uuu %d\n",p->data);            p=p->next;        }    }    }main( )  {         struct node *head;      head=NULL;    //②建一个空表      print(creat(head));/*打印单链表*/  }  

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: