- 论坛徽章:
- 0
|
在linux下的程序一样的!!!!
这里没警告!!!
帮忙解解!!!!
//----------------------------------------------------------------------------------
listHead.h
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OVERFLOW -1
#define ERROR -1
#define OK 0
//#define NULL 0
#define LEN sizeof(truct LNode);
typedef int ElemType;
struct LNode //结点结构
{
ElemType data; //数据域
struct LNode *next; //指针域
};
typedef struct LNode linkList;
//-----------------------------------------------------------------------------
function2_1.c
#include "listHead.h"
//构建一个空的链表
void initList(struct LNode *L)
{
L=(struct LNode *)malloc(sizeof(struct LNode)); //产生头结点,并使L指向此头结点
if(!L)
{
exit(OVERFLOW);
}
L->next=NULL;//指针域为空
}
//销毁链表
void destroyList(struct LNode *L)
{
struct LNode *q;
while(L) //如果链表不为空
{
q=L->next; //将当前结点的下一个结点赋值给q
free(L); //释放地一个结点
L=q;
}
}
//向链表插入数据
int listInsert(struct LNode *L,int i,ElemType e)
{//在带头结点的单链表L中的第i个位置之前插入元素e
int j=0;
struct LNode *p=L;
struct LNode *s;
printf("i=%d ------ e=%d \n",i,e);
while(p && j<i-1)//p是否会为0?? j++直到为i-1为止
{
p=p->next;
j++;
}
if(!p||j>i-1)//i小于1或者大于表长// ??大于表长??
{
printf("insert fail !!!\n");
return ERROR;
}
s=(struct LNode *)malloc(sizeof(struct LNode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
int listTraverse(struct LNode *L)
{
struct LNode *p = L->next;
//printf("&L->next=%d\n",&L->next);
if(p ==NULL)
return -1;
// printf("scan!\n");
for(p = L->next;p!=NULL;p=p->next)
{
printf("data %d\n",p->data);
//printf("*p %d",*p);
}
#if 0
if(p!=NULL)
{
do{
printf("\n*p %d\n",*p);
printf("%d ",p->data);
tmp = p;
p=p->next;
}while(p!=tmp);
}
else
printf("List is NULL!!\n");
#endif
printf("List !!!\n");
printf("\n");
return 0;
}
//------------------------------------------------------------------------------------------------
main2_1.c
#include "listHead.h"
void print(ElemType c)
{
printf("%d ",c);
}
int main(void)
{
linkList L;
ElemType e,e0;
int i;
int j,k;
initList(&L);
// printf("NULL= %d\n",NULL);
// int a[5]={2,3,1,4,6};
for(j=1;j<=5;j++)
{
// i=listInsert(&L,1,a[j-1]);
i=listInsert(&L,1,j);
if(i==0)
printf("j=%d success Insert !!\n",j);
else
printf("error !!!\n");
}
listTraverse(&L);
printf("List !!!\n");
printf(" h3-----herer\n");
}
//--------------------------------------------------------
Makefile
AIM:=main2_1
$(AIM):main2_1.c function2_1.c listHead.h
gcc $^ -o $@
.PHONY:clean
clean:
-rm $(AIM) *~
//-------------------------------------------------------------------------
|
|