单向链表的增删查改练习

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node * next;
}Node;


#if 0
Node * createList()
{
    Node * head = (Node *)malloc(sizeof(Node));
    if (NULL == head)
        exit(-1);
    head->next = NULL;

    Node *t = head,*cur;
    int nodeData;
    scanf("%d",&nodeData);
    while(nodeData)
    {
        cur = (Node *)malloc(sizeof(Node));
        if(NULL == head)
            exit(-1);
        cur ->data = nodeData;  
        t -> next = cur;
        t = cur;
        scanf("%d",&nodeData);
    }

    t->next = NULL;
    return head;
}


Node * createList()
{
    Node * head = (Node *)malloc(sizeof(Node));
    if (NULL == head)
        exit(-1);
    head->next = NULL;
    Node *cur;

    int nodeData;
    scanf("%d",&nodeData);
    while(nodeData)
    {
        cur=(Node *)malloc(sizeof(Node));
        if(NULL==head)
            exit(-1);
        cur -> data = nodeData;

        cur -> next = head -> next;
        head -> next = cur;

        scanf("%d",&nodeData);
    }
    return head;
}

#endif

Node *createList()
{
    Node * head =(Node *)malloc(sizeof(Node));
    if (NULL == head)
        exit(-1);
    head -> next = NULL;
    return head;
}


void insertList(Node *head,int nodeData)
{
    Node *cur = (Node *)malloc(sizeof(Node));
    if(NULL == head)
        exit(-1);
    cur -> data=nodeData;
    cur -> next = head -> next;
    head -> next = cur;

}

void traverseList(Node *head)
{
    head = head -> next;
    while(head)
    {
        printf("%d\n",head -> data);
        head = head -> next;
    }
}

int lenList(Node *head)
{
    int len=0;
    head =head -> next;
    while(head)
    {
        len++;
        head=head -> next;
    }
    return len;
}

Node *searchList(Node *head,int findData)
{
    head = head -> next; 
    while(head)
    {
        if(head -> data == findData)
        {   
            break;
        }
        head = head -> next;
    }
    return head;
}

void deleteNodeOfList(Node *head, Node *pfind)
{
    if(pfind ->next ==NULL)
    {
        while(head -> next != pfind)
            head=head ->next;
        head -> next= pfind -> next;
        free(pfind);
        pfind = NULL;
    }
    else
    {
        Node *t = pfind -> next;
        pfind -> data = pfind -> next -> data;
        pfind -> next = pfind -> next -> next;
        free(t);
    }

}

#if 0
void popSortList(Node *head)
{
    int len = lenList(head);
    head = head -> next;
    Node *p,*q;
    int i,j;
    for (i=0;i<len-1;i++)
    {
        p=head;
        q=p -> next;
        for (j=0;j<len-1-i;j++)
        {
            if(p -> data > q -> data)
            {
                p -> data ^= q -> data;
                q -> data ^= p -> data;
                p -> data ^= q -> data;
            }
            p = p-> next;
            q = p -> next;
        }
    }
}
#endif

void popSortList(Node *head)
{
    int len =lenList(head);
    int i,j;
    Node *prep,*p,*q,*t;
    for (i=0;i<len-1;i++)
    {
        prep=head;
        p=head -> next;
        q=p -> next;
        for(j=0;j<len-1-i;j++)
        {
            if(p -> data > q -> data)
            {
                prep -> next = q;
                p -> next = q -> next;
                q -> next = p;

                t=p;
                p=q;
                q=t;

            }
            prep = prep -> next;
            p = p -> next;
            q = q -> next;
        }
    }   
}

void reverseList(Node *head)
{
    Node * cur = head -> next;
    head -> next = NULL;
    Node *t;
    while(cur)
    {
        t = cur;
        cur = cur -> next;

        t -> next = head -> next;
        head -> next =t;
    }   
}


void destroyList(Node *head)
{
    Node *t;
    while(head)
    {
        t=head;
        head=head -> next;
        free(t);
    }
}


int main(int argc, char *argv[])
{
    Node *head = createList();

    srand(time(NULL));

    int i;
    for (i=0;i<10;i++)
    {
        //insertList(head,i);
        insertList(head,rand()%100);
    }
    traverseList(head);

    printf("len of list = %d\n",lenList(head));

    Node *pfind = searchList(head,9);

    if (pfind == NULL)
    {
        printf("find none\n");
    }
    else
    {
        pfind ->data=1000;
        traverseList(head);

        printf("your find in list\n");
        deleteNodeOfList(head,pfind);
    }

    traverseList(head);

    popSortList(head);
    printf("after popsort\n");
    traverseList(head);

    reverseList(head);  
    printf("after reverse\n");
    traverseList(head);

    destroyList(head);
    return 0;
}
此条目发表在C分类目录。将固定链接加入收藏夹。

发表评论

邮箱地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Protected with IP Blacklist CloudIP Blacklist Cloud