单向链表的增删查改练习

#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 | 留下评论

对象指针练习

#include <iostream>
using namespace std;

class Time
{
public:
    Time()
    {
        hour=0;
        minute=0;
        sec=0;
    }
    void set_time();
    void show_time();

    int hour;
    int minute;
    int sec;
};

void Time::set_time()
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time()
{
    cout << hour << ":" << minute << ":" << sec << endl;
}

int main()
{
    Time t1;
    Time *pt;
    pt=&t1;
    t1.show_time();
    pt -> set_time();
    t1.show_time();

    int *p1;
    p1=&t1.hour;
    *p1=11;

    //pt -> show_time();
    void(Time::*p2)();
    p2=&Time::show_time;
    (t1.*p2)(); 

    return 0;
}
发表在 C++ | 留下评论

C和C++动态内存的使用

//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
    char *p = (char *)malloc(sizeof(char)*100);
    if(p==NULL)
    {
        printf("malloc error\n");
        return;
    }
    strcpy(p,"hello");
    printf("str=%s\n",p);
    free(p);
    p=NULL;
}

//C++
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char *p = new char[100];
    strcpy(p,"hello");
    cout << "str=" << p << endl;
    delete []p;
    return 0;
}
发表在 C, C++ | 留下评论

堆栈小测试

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

//栈方向
void func()
{
    printf("func:\n");
    int a,b,c,d;
    printf("%p\n",&a);
    printf("%p\n",&b);
    printf("%p\n",&c);
    printf("%p\n",&d);

    return;
}


int main()
{
    printf("main:\n");
    int a,b,c,d;
    printf("%p\n",&a);
    printf("%p\n",&b);
    printf("%p\n",&c);
    printf("%p\n",&d);

    func();

    return 0;
}

//栈溢出

void func(int n)
{
    if(n == 0)
        return;
    else
    func(n-1);
}



int main()
{
    //char arr[1024*1024*10];
    //strcpy(arr,"china");
    func(1000000000); //栈溢出,导致段错误
    printf("over\n");
    return 0;
}

//堆方向
void func()
{
    printf("in func:\n");
    char *pa,*pb,*pc,*pd;
    pa=malloc(100);
    pb=malloc(100);
    pc=malloc(100);
    pd=malloc(100);
    printf("%p\n",pa);
    printf("%p\n",pb);
    printf("%p\n",pc);
    printf("%p\n",pd);
}


int main()
{
    printf("in main:\n");
    char *pa,*pb,*pc,*pd;
    pa=malloc(100);
    pb=malloc(100);
    pc=malloc(100);
    pd=malloc(100);
    printf("%p\n",pa);
    printf("%p\n",pb);
    printf("%p\n",pc);
    printf("%p\n",pd);
    func();
    return 0;
}

//堆的简单使用
int main (int argc, char *argv[])
{
    char * p = (char*)malloc(1024*1024*1024);
    if(p == NULL)
        printf("malloc error\n");
    strcpy (p,"abcd");
    printf("over\n");
    free(p);
        p=NULL;
    return 0;
}
发表在 C | 留下评论

动态链表(头插法和尾插法)

#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;
}
#endif

//头插法

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;
}


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

int main(int argc, char *argv[])
{
    Node *head = createList();
    traverseList(head);
    return 0;
}
发表在 C | 留下评论

静态链表练习

#include <stdio.h>

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

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

int main (int argc, char *argv[]){
    Node a,b,c,d,e,f;

    Node *head;
    head= &a;

    a.data=1;
    b.data=2;
    c.data=3;
    d.data=4;
    e.data=5;

    a.next=&b;
    b.next=&c;
    c.next=&d;
    d.next=&e;

    e.next=NULL;

    traverseList(head);
    return 0;
}
发表在 C | 留下评论