Today would like to write a code to practice practicing, the idea is very good results, planted a big somersault, in this error lingered for 4 hours to solve, now share out, give everyone to wake up, put the code first:
/********************************************
* File name: sqlist.h
* File Description: Linear table Sequential Storage Demo
* File Author: by Wang.j,in 2013.11.16
* File Version: 1.0
* Modify Records:
*********************************************/
#ifndef __sqlist_h__
#define __dwlist_h__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 50
#define OK 0
#define ERR-1
typedef int ELEMTYPE;
typedef struct {
Elemtype Data[maxsize];
int Len;
}sqlist;
int init_list (sqlist *l);
int destroy_list (sqlist *l);
int List_empty (sqlist L);
int List_length (sqlist L);
int disp_list (sqlist L);
int Get_elem (sqlist L, int i, elemtype *e);
int Local_elem (sqlist L, elemtype e);
int List_insert (sqlist *l, int i, elemtype e);
int List_delete (sqlist *l, int i, elemtype *e);
#endif
/**************************************************
* File name: sqlist.c
* File Description: Implementation of linear table sequential storage
* File Author: by Wang.j,in 2013.11.16
* File Version: 1.0
* Modify Records:
***************************************************/
#include "Sqlist.h"
#if 0
#define ERR_NONE_ERROR 0
#define ERR_FUNC_EXEC 1
#define Err_file_open 2
Char *error_msg[] = {
/* 0 * * "Successful implementation, no errors",
/* 1/"Function execution Error",
* * 2/"File Open Error",
};
int my_errno = 0;
#endif
int main (void)
{
int ret = 0;
int i = 0;
SqList slist;
Elemtype e;
memset (&slist, 0, sizeof (slist));
printf ("length:%d\n", Slist.len);
ret = Init_list (&slist);
if (OK!= ret)
return-1;
ret = List_empty (slist);
printf ("Length:%d\n", Slist.len);
if (OK = ret)
printf ("Sequential table is empty \ n");
if (ERR = = ret)
printf ("Sequential table is not empty \ n");
for (i = 0; i < i++) {
E = (elemtype) i;
List_insert (&slist, I, E);
}
printf ("Insert data \ n");
ret = List_empty (slist);
if (OK = ret)
printf ("Sequential table is empty \ n");
if (ERR = = ret)
printf ("Sequential table is not empty \ n");
printf ("After length%d\n", List_length (slist));
Disp_list (slist);
Destroy_list (&slist);
return 0;
}
/*=====================================================
* Function Name: init_list
* Function function: Initialize a sequential table, create an empty order table
* Function parameter: sqlist *L is responsible for returning a created order table, if creating
Returns NULL if failed
* Return value: Successfully returns 0 and returns a created empty table through the pointer.
Failed return-1 pointer returns null
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int init_list (sqlist *l)
{
L = (SqList *) malloc (sizeof (sqlist));
if (NULL = = L) {
L = NULL;
return-1;
}
L->len = 0;
return 0;
}
/*=====================================================
* Function Name: destroy_list
* Function function: Destroy the creation of a good order table, release the space of the order table
* Function Parameters: SqList *l, existing linear table
* Return Value: Success 0
Failure-1
Usually free does not fail, in fact this function can directly use void
, here just write their own handy, see the code will know not to return 0
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int destroy_list (sqlist *l)
{
Free (L);
return 0;
}
/*=====================================================
* Function Name: list_empty
* Function function: To determine whether the SqList order table is empty
* Function parameter: sqlist L, existing linear table
* Return value: Empty 0
Not empty-1
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int List_empty (sqlist L)
{
if (0 = l.len)
return 0;
return-1;
}
/*=====================================================
* Function Name: list_length
* Function function: Get the length of the linear table, return the number of elements in the order table
* Function parameter: sqlist L, the existing linear table
* Return value: Length of L
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int List_length (sqlist L)
{
return L.len;
}
/*=====================================================
* Function Name: disp_list
* Function function: Display all the elements in the order table
* Function parameter: sqlist L, the existing linear table
* Return Value: Success 0
Failure-1
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int disp_list (sqlist L)
{
int i = 0;
if (0 >= L.len)
return-1;
for (i = 0; i < L.len; i++)
printf ("%d\t", L.data[i]);
/*
* This place is my own objection, first you may not know the type of output is
*%d, and then the length is to use the List_length function or use the L.len method,
* List_length is the extra overhead of function calls with function calls, which on the PC
* Overhead is nothing, but in an embedded system you have to consider this overhead,
* This is basically a problem between good portability and code efficiency, in order to improve
* Portability can add several layers of abstraction to achieve a variety of judgments. Unless it's extremely large.
* Project or to match a variety of such devices, I think like code definition types that
* A small thing, team communication can be solved. Work is to avoid problems, learning is asking questions.
* So how to choose can only see a person.
*/
printf ("\ n");
return 0;
}
/*=====================================================
* function name: Get_elem
* function function: Gets the domain of the I position element, To facilitate the corresponding I starting from 0 with the
array subscript, return the obtained value with E
* Function parameters: SQLite l Existing order table
int i Location
Elemtype *e return range
* return value: Success 0
failed -1
* Creator: by Wang.j,in 2013.11.16
* Modify record:
======== ==============================================*/
int Get_elem (sqlist L, int i, elemtype *e)
{
if (I < 0 | | I >= l.len) {
e = NULL;
return-1;
}
*e = L.data[i];
/*
* This place should pay attention
* Look at the difference with E = & (L.data[i])
*/
return 0;
}
/*=====================================================
* Function Name: Local_elem
* Function function: Find by element value, return the first element position corresponding to E
* Function Parameters: SqList L, the existing order table
* Return value: There is a return position
Failure return-1
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int Local_elem (sqlist L, Elemtype e)
{
int i = 0;
for (i = 0; i < L.len; i++) {
if (E = = L.data[i])
return i;
}
return-1;
}
/*=====================================================
* Function Name: List_insert
* Function function: Inserting elements in the I position of SQLite
* Function parameter: sqlist *l existing order table
int I position
Elemtype e Element
* Return Value: Success 0
Failure-1
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int List_insert (sqlist *l, int i, elemtype e)
{
int j = 0;
if (I < 0 | | i > MAXSIZE-1)
return-1;
for (j = l->len; J > i; j--)
L->DATA[J] = l->data[j-1];
L->data[i] = e;
l->len++;
return 0;
}
/*=====================================================
* Function Name: List_delete
* Function function: Delete the element of the I position, the element is returned by E
* Function parameter: SQLite *l existing order table
int I position
Elemtype *e The element that deletes the location
* Return Value: Success 0
Failure-1
* Creator: by Wang.j,in 2013.11.16
* Modify Records:
======================================================*/
int List_delete (sqlist *l, int i, Elemtype *e)
{
int j = 0;
if (I < 0 | | I >=l->len)
return-1;
*e = l->data[i];
for (j = i; J < (L->len-1), J + +)
L->DATA[J] = l->data[j+1];
l->len--;
return 0;
}
The results were completely unexpected.
Believe that smart you have seen, I defined in Main in the Slist space on the stack, and I init_list in the middle of all of a sudden to allocate this stuff to the heap space, and slist is not a pointer, simply can not point, so the result of course is very wrong.
For example, stacks and heaps are two parallel worlds, and only the pointer is a wormhole that travels through two worlds, except that other things cannot be crossed.
Knowing the reason is naturally easy to solve.
Because the stack will automatically allocate space so there is no need to request space again. So init_list instead:
It's okay.
Let us be a cautionary tale.