Linked List C implement (1)

Linked List C implement  (1)

////////////////////////////////////////////////////////////////////////////////////////////////////

#include<stdio.h>
#include<malloc.h> 


typedef struct List_Node{
    
int info;
    struct List_Node 
*next;
  }node;
//结点结构体

/******************************/
/* 尾插法建立带头结点的单链表 */
/******************************/
node
* Creat_Node()
{
    node 
*head,*pre,*p;
    
int x;
    head
=(node*)malloc(sizeof(node));;
    head
->next=NULL;
    pre
=head;
    
printf("输入各结点的值,以0结束:");
    
while(EOF!=(scanf("%d",&x))&&x!=0)
    {
        p
=(node*)malloc(sizeof(node));
        p
->info=x;
        p
->next=pre->next;
        pre
->next=p;
        pre
=pre->next;
    }
    
return head;


/******************************/
/* 头插法建立带头结点的单链表 */
/******************************/
node
* Build_Node()
{
    node 
*head,*p;
    
int x;
    head
=(node*)malloc(sizeof(node));;
    head
->next=NULL;
    
printf("输入各结点的值,以0结束:");
    
while(EOF!=(scanf("%d",&x))&&x!=0)
    {
        p
=(node*)malloc(sizeof(node));
        p
->info=x;
        p
->next=head->next;
        head
->next=p;
    }
    
return head;



/******************************/
/*         打印单链表         */
/******************************/ 

void Print_Node(node 
*head)
{
    node 
*p=head->next;
    
printf("输出该链表:");
    
while(p)
    {
        
printf("%-5d--->",p->info);
        p
=p->next;
    }
    
if(p==NULL)
    {
        
printf("^\n\n\n");
    }


  

#include"Head_Node.h" 

int Count_Node(node *head)
{
    node 
*p=head->next;
    
int num=0;
    
while(p!=NULL)
    {
        num
++;
        p
=p->next;
    }
    
return num;


int main()
{
    node 
*head;
    head
=Creat_Node();
    Print_Node(head);
    
printf("结点个数为:%d\n",Count_Node(head));
    
return 0;


  

#include"head_node.h" 

/**********************************/
/*         删除重复               */
/**********************************/ 

void Delete_Repeat_Node(node 
*head)
{
    node 
*p,*pre,*s;
    pre
=head->next;
    p
=pre->next;
    
while(p)
    {
        s
=p->next;
        
while(s&&s->info!=p->info)
        {
            s
=s->next;
        }
        
if(s)
        {
            pre
->next=p->next;
            free(p);
            p
=pre->next;
        }
        
else
        {
            pre
=p;
            p
=p->next;
        }
    }


int main()
{
    node 
*head;
    head
=Creat_Node();
    Print_Node(head);
    Delete_Repeat_Node(head);
    Print_Node(head);
    
return 0;
}


  

#include"Head_Node.h"
/************************************/
/*           在Y前插入X             */
/************************************/
void Before_y_Insert_x(node
* head,int y,int x)
{
    node 
*pre,*p,*s;
    pre
=head;
    p
=pre->next;
    
while(p&&p->info!=y)
    {
        pre
=p;
        p
=p->next;
    }
    
if(p==NULL)
    {
        
printf("error!%d不在该链表中\n",y);
    }
    
else
    {
        s
=(node*)malloc(sizeof(node));
        s
->info=x;
        s
->next=p;
        pre
->next=s;
    }


int main()
{
    node 
*head;
    
int x,y;
    head
=Creat_Node();
    
printf("在y前插入x,输入y,x:");
    scanf(
"%d%d",&y,&x);
    Print_Node(head);
    Before_y_Insert_x(head
,y,x);
    Print_Node(head);
    
return 0;
}

posted @ 2007-08-07 11:05  RayG  阅读(332)  评论(0编辑  收藏  举报