Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119362
  • 博文数量: 41
  • 博客积分: 1695
  • 博客等级: 上尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-21 22:50
文章分类

全部博文(41)

文章存档

2010年(1)

2007年(23)

2006年(17)

我的朋友

分类: C/C++

2006-12-20 22:14:38

//作者:Guo R.H
//日期:06.12.22
//      USTC

#include
#include

#define LEN sizeof ( struct List )

struct List
{
    int num;
    struct List *next;
};

struct List *creat ( ) ;
struct List *delete ( struct List *head, int i) ;
void print ( struct List *head ) ;
struct List *merge(struct List *head1, struct List *head2) ;

int main( )
{
    struct List *head1, *head2, *head;
    head1 = creat ( );
    head2 = creat ( );
   head = merge(head1, head2);
   print( head );
   head=delete(head, 1);
    print(head);
    return 0;
}

struct List *creat ( )
{
    struct List *head, *new, *now;
    head = NULL ;    
    now = new = ( struct List * ) malloc( LEN ) ;
    printf("Please input a number : ") ;
    scanf("%d", &new->num) ;
    while ( new->num != 0 )
    {
        if (head == NULL)
            head = new ;
        else
            now->next = new ;
        now = new ;
        new = ( struct List * )malloc( LEN ) ;    
        printf("Please input a number : ") ;
        scanf("%d", &new->num) ;
    }
    now->next = NULL ;
    free (new);
    return(head);
}

void print ( struct List *head )
{
    struct List *p ;
    p = head ;
    if (head == NULL)
    {
        printf("It's a empty list.\n%d", head) ;
        return ;
    }
    else
    {
        printf("The list is :\n") ;
        do
        {
            printf("%d\n", p->num) ;
            p = p->next ;
        }while (p != NULL);
    }
}

struct List *merge(struct List *head1, struct List *head2)
{
    struct List *head, *p;
    head = p = head1;
    while (p->next)
        p = p->next;
    p->next = head2;
    return head;
}

struct List *delete ( struct List *head, int i)
{
    struct List *p1, *p2;
    p1 = head;
    while( p1->next != NULL)
    {
        if (p1->num == i)
        {
            if(p1 = head)
                head = p1->next;
            else
                p2->next=p1->next;
        }
        p2 = p1;
        p1 = p1->next;
    }
   
    return head;
   
}
阅读(1473) | 评论(1) | 转发(0) |
0

上一篇:常见排序方法

下一篇:栈创建和操作

给主人留下些什么吧!~~