[Data structure]----sequential list 1

This is a discussion on [Data structure]----sequential list 1 within the C Programming forums, part of the General Programming Boards category; i'm studying data structure,so i want to show my exercises from the book,hope everyone help me improve my skill and ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23

    [Data structure]----sequential list 1

    i'm studying data structure,so i want to show my exercises from the book,hope everyone help me improve my skill and optimize it.Thanks

    Code:
    /************************************************************************/
    /*  after adding a new element in a non decreasing sequential list and  */
    /*  don't change the sequence                                           */
    /************************************************************************/
    
    
    #include <stdio.h>
    
    #define    DATA_NUM    15
    
    void inputData(int *dataArray, int *len)
    {
        int i = 0;
        int data = 0;
        int length = 0;
    
        printf("please input integers(0-15),-1 is end\n");
    
        for (i=0; i<DATA_NUM; ++i)
        {
            scanf("%d", &data);
    
            if (-1 == data)
            {
                break;
            }
    
            *(dataArray + i) = data;
            ++length;
        }
        *len = length;
    }
    
    void outputData(int *data, int len)
    {
        int i = 0;
    
        printf("the data list is : ");
    
        for (i=0; i<len; ++i)
        {
            printf("%d ", *(data + i));
        }
        printf("\n");
    }
    
    void insertData(int *dataArray, int *len, int data)
    {
        int i = 0;
        int j = 0;
    
        for (i=0; i<*len; ++i)
        {
            if (*(dataArray + i) >= data)
            {
                break;
            }
        }
    
        for (j=0; j<=*len-i; ++j)
        {
            *(dataArray + *len - j + 1) = *(dataArray + *len - j);
        }
    
        *(dataArray + i) = data;
        *len += 1;
    }
    
    
    int main()
    {
        int dataArray[DATA_NUM] = {0};
        int len = 0;
        int data = 0;
    
        inputData(dataArray, &len);
        outputData(dataArray, len);
        printf("input new data: ");
        scanf("%d", &data);
        insertData(dataArray, &len, data);
        outputData(dataArray, len);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23

    [Data structure]----sequential list 2

    Code:
    /************************************************************************/
    /*          delete all the value is x in the sequential list            */
    /************************************************************************/
    
    
    #include <stdio.h>
    
    #define    DATA_NUM    15
    
    void inputData(int *dataArray, int *len)
    {
        int i = 0;
        int data = 0;
        int length = 0;
    
        printf("please input integers(0-15),-1 is end\n");
    
        for (i=0; i<DATA_NUM; ++i)
        {
            scanf("%d", &data);
    
            if (-1 == data)
            {
                break;
            }
    
            *(dataArray + i) = data;
            ++length;
        }
        *len = length;
    }
    
    void outputData(int *data, int len)
    {
        int i = 0;
    
        printf("the data list is : ");
        for (i=0; i<len; ++i)
        {
            printf("%d ", *(data + i));
        }
        printf("\n");
    }
    
    void delSameData(int *dataArray, int *len, int data)
    {
        int i = 0;
        int j = 0;
        int index = 0;
    
        for (i=0; i<*len; ++i)
        {
            if (*(dataArray + i) != data)
            {
                continue;
            }
    
            ++index;
    
            for (j=i; j<*len; ++j)
            {
                *(dataArray + j) = *(dataArray + j + 1);
            }
            --i;
        }
    
        *len -= index;
    }
    
    
    int main()
    {
        int dataArray[DATA_NUM] = {0};
        int len = 0;
        int delData = 0;
    
        inputData(dataArray, &len);
        outputData(dataArray, len);
        printf("input delete data: ");
        scanf("%d", &delData);
        delSameData(dataArray, &len, delData);
        outputData(dataArray, len);
    
        return 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,614
    Instead of writing *(dataArray + i) write dataArray[i]. Likewise for *(dataArray + *len - j + 1).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23

    [Data structure]----sequential list 3

    Code:
    /************************************************************************/
    /*     delete all the value between x and y in the sequential list      */
    /************************************************************************/
    
    
    #include <stdio.h>
    
    #define    DATA_NUM    15
    
    void inputData(int *dataArray, int *len)
    {
        int i = 0;
        int data = 0;
        int length = 0;
    
        printf("please input integers(0-15),-1 is end\n");
    
        for (i=0; i<DATA_NUM; ++i)
        {
            scanf("%d", &data);
    
            if (-1 == data)
            {
                break;
            }
    
            *(dataArray + i) = data;
            ++length;
        }
        *len = length;
    }
    
    void outputData(int *data, int len)
    {
        int i = 0;
    
        printf("the data list is : ");
        for (i=0; i<len; ++i)
        {
            printf("%d ", *(data + i));
        }
        printf("\n");
    }
    
    void delDataAToB(int *dataArray, int *len, int a, int b)
    {
        int i = 0;
        int j = 0;
        int index = 0;
    
        for (i=0; i<*len; ++i)
        {
            j = i;
            while (*(dataArray + j) >= a && *(dataArray + j) <= b)
            {
                ++index;
                ++j;
                continue;
            }
    
            if (index > 0)
            {
                for (; j<*len; ++j)
                {
                    *(dataArray + j - index) = *(dataArray + j);
                }
    
                *len -= index;
                --i;
                index = 0;
            }
        }
    }
    
    
    int main()
    {
        int dataArray[DATA_NUM] = {0};
        int len = 0;
        int aData = 0, bData;
    
        inputData(dataArray, &len);
        outputData(dataArray, len);
        printf("input the scope of deleting data: ");
        scanf("%d%d", &aData, &bData);
        delDataAToB(dataArray, &len, aData, bData);
        outputData(dataArray, len);
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23

    [Data structure]----sequential list 4

    Code:
    /************************************************************************/
    /*  put the data of less than 0 in the front of the sequential          */
    /*  list,more than 0 is rear                                            */
    /************************************************************************/
    
    
    #include <stdio.h>
    
    #define    DATA_NUM    15
    
    void inputData(int *dataArray, int *len)
    {
        int i = 0;
        int data = 0;
        int length = 0;
    
        printf("please input integers(0-15),-1 is end\n");
    
        for (i=0; i<DATA_NUM; ++i)
        {
            scanf("%d", &data);
    
            if (-1 == data)
            {
                break;
            }
    
            *(dataArray + i) = data;
            ++length;
        }
        *len = length;
    }
    
    void outputData(int *data, int len)
    {
        int i = 0;
    
        printf("the data list is : ");
        for (i=0; i<len; ++i)
        {
            printf("%d ", *(data + i));
        }
        printf("\n");
    }
    
    void putDataByZero(int *dataArray, int len)
    {
        int i = 0;
        int temp = 0;
        int *p = dataArray;
        int *q = p;
    
        while (p < &dataArray[len-1])
        {
            while ((*p >= 0) && (p < &dataArray[len-1]))
            {
                ++p;
            }
    
            temp = *p;
            *p = *q;
            *q = temp;
            ++q;
        }
    }
    
    int main()
    {
        int dataArray[DATA_NUM] = {0};
        int len = 0;
    
        inputData(dataArray, &len);
        outputData(dataArray, len);
        putDataByZero(dataArray, len);
        outputData(dataArray, len);
    
        return 0;
    }

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Four posts merged into one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between data object and data structure?
    By c_lady in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2011, 11:30 AM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. List Tree - New data structure
    By sincoder in forum C++ Programming
    Replies: 5
    Last Post: 08-12-2008, 01:33 PM
  5. C++...sequential access data files
    By locolobo in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2002, 09:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21