您的位置:首页 > 其它

为什么需要指针,不要可以吗

2017-08-09 09:12 253 查看
#include <stdio.h>

void swap(int a ,int b)

{

        printf("swap a=%d b=%d\n",a,b);
int t = a;
a = b;
b =t;
printf("swap end a=%d b=%d\n",a,b);

}

int main()

{
int a = 1;
int b = 10;
swap(a,b);

printf("a=%d b=%d\n",a,b);  

return 0;
}

root@lqs-ubuntu:/home/lqs/work/datastruct# ./swap
swap a=1 b=10

swap end a=10 b=1

a=1 b=10

#include <stdio.h>

void swap(int *a ,int *b)

{

        printf("swap a=%d b=%d\n",*a,*b);
int t = *a;
*a = *b;
*b =t;
printf("end swap a=%d b=%d\n",*a,*b);

}

int main()

{
int a = 1;
int b = 10;
swap(&a,&b);

printf("a=%d b=%d\n",a,b);  

return 0;

}

root@lqs-ubuntu:/home/lqs/work/datastruct# ./swap_p 
swap a=1 b=10

end swap a=10 b=1

a=10 b=1

root@lqs-ubuntu:/home/lqs/work/datastruct#

#include <stdio.h>

#include <stdlib.h>

typedef int DataType;

typedef struct node 

{

    DataType data;

    struct node *next;

}LinkNode;

LinkNode *create_empty_linklist()

{

    LinkNode *head = NULL;

    head = (LinkNode *)malloc(sizeof(LinkNode));

    head->next = NULL;

    return head;

}

 

int is_empty_linklist(LinkNode *head)

{

    return head->next == NULL;

}

 int insert_head_linklist(LinkNode *head,DataType data)

{

    LinkNode *temp = NULL;

    temp = (LinkNode *)malloc(sizeof(LinkNode));

    temp->data = data;

    temp->next = head->next;

    head->next = temp;

    return 0;

}

 int insert_tail_linklist(LinkNode *head,DataType data)

{

    LinkNode *temp = NULL;

    LinkNode *p = head;

    temp = (LinkNode *)malloc(sizeof(LinkNode));

    temp->data = data;

    //找到尾结点

    while(p->next)

    {

        p = p->next;

    }

    //temp->next = NULL;

    temp->next = p->next;

    p->next = temp;

       return 0;

}

 

int insert_order_linklist(LinkNode *head,DataType data)

{

    LinkNode *temp = NULL;

    LinkNode *p = head;

    temp = (LinkNode *)malloc(sizeof(LinkNode));

    temp->data = data;

#if 0

    while(p->next)

    {

        if(p->next->data > data)

        {

            break;

        }

        p = p->next;

    }

#endif

    /*while(p->next != NULL && p->next->data < data)*/

    while(p->next && p->next->data < data)

    {

        p = p->next;

    }

    temp->next = p->next;

    p->next = temp;

    return 0;

}

int delete_assign_node(LinkNode *head,DataType data)

{

    LinkNode *temp = NULL;

    LinkNode *p = head;

    while(p->next && p->next->data != data)

    {

        p = p->next;

    }

     if(p->next == NULL)

    {

        return -1;

    }

    temp = p->next;

    p->next = p->next->next;

    free(temp);

    return 0;

}

 int print_linklist(LinkNode *head)

{

    LinkNode *p = head->next;

    while(p)

    {

        printf("%d ",p->data);

        p = p->next;

    }

    putchar('\n');

    return 0;

}

int order_data(LinkNode *head)

{

    LinkNode *sa;

    LinkNode *sb;

    int  t ;

    for(sa = head->next; sa != NULL ;sa = sa->next)

    {

        for(sb=sa->next;sb != NULL;sb = sb->next)

        {

            if(sa->data > sb->data)

            {

                t = sa->data;

                sa->data= sb->data;

                sb->data = t;

            }

        }

    }

    return 0;

}

 int reverse_linklist(LinkNode *head)

{

#if 1

// printf("fun 00\n");
LinkNode *pPrv  = head;
LinkNode *Cur = head->next;
LinkNode *pNext = NULL;

head->next = NULL;

while( Cur )
{
pNext = Cur->next;
Cur->next = pPrv->next;

pPrv->next = Cur;
Cur = pNext;

}

return 0;

#endif

#if 0

    LinkNode *p = head,*q = NULL;

    if(p->next == NULL || p->next->next == NULL)

    {

        return -1;

    }

    q = p->next->next;

    p->next->next = NULL;

    while(q)

    {

        p = q->next;

        q->next = head->next ;

        head->next = q;

        q = p;

    }

    return 0;

#endif

}   

 int main()

{

    LinkNode *head = NULL;

    int a[] = {1,5,3,4,7,9};

    int i = 0;

    int ret = 0;

    head = create_empty_linklist();

    for(i = 0;i < sizeof(a) / sizeof(a[0]);i++)

    {

        /*insert_head_linklist(head,a[i]);*/

        /*insert_tail_linklist(head,a[i]);*/

        insert_order_linklist(head,a[i]);

    }

    print_linklist(head);

#if 0

    ret = delete_assign_node(head,5); 

    if(ret < 0)

    {

        printf("Assign data %d is not exist.\n",5);

    }

    print_linklist(head);

#endif

    reverse_linklist(head);

    #if 0

    for(i  = 0 ; i < 6 ; i ++)

    {

        printf("%d ",a[i]);

    }

    #endif

    print_linklist(head);

    return 0;

}

root@lqs-ubuntu:/home/lqs/work/datastruct# ./reverse 
1 3 4 5 7 9 

hello world

9 7 5 4 3 1 

root@lqs-ubuntu:/home/lqs/work/datastruct# 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐