#include
<
stdio.h
>
#include < malloc.h >
#include < iostream.h >
#define SUCCESS 1
#define FAIL -1;
typedef struct Node ... {
int Data;
struct Node *Next;
} LNode,*LinkList;
LNode * Head = (LNode * )malloc( sizeof (LNode));
void ListCreate()
... {
Head->Next=NULL;
}
void ListInit()
... {
int i;
LNode *Temp=Head;
for (i=0;i<20;i++)
...{
LNode *p=(LNode *)malloc(sizeof(LNode));
p->Data=i;
p->Next=NULL;
Temp->Next=p;
Temp=Temp->Next;
}
}
int ListInsert(Head , int i, int e) //在头结点为Head的单链表第i个元素之前插入e元素
... {
int j=0;
LNode *q=L;
LNode *p=(LNode *)malloc(sizeof(LNode));
p->Data=e;
if(0==i)
...{
L=p;
L->Next=NULL;
return SUCCESS;
}
while(q&&(j<i))
...{
j++;
q=q->Next;
}
if((q==NULL)||(q->Next==NULL))
return FAIL;
p->Next=q->Next;
q->Next=p;
return SUCCESS;
}
void ListDel(LNode * L, int i)
... {
int j=0;
LNode *p=L;
while((p!=NULL)&&(j<i))
...{
j++;
p=p->Next;
}
if((p==NULL)&&(p->Next==NULL))
return ;
p->Next=p->Next->Next;
}
void main()
... {
int i;
LNode *p=Head;
ListCreate();
ListInit();
if (-1==ListInsert(Head,5,119))
return;
printf("删除前链表: ");
for (i=0;i<10;i++)
...{
p=p->Next;
cout<<p->Data<<endl;
}
printf("删除后链表: ");
ListDel(Head,5);
p=Head;
for (i=0;i<10;i++)
...{
p=p->Next;
cout<<p->Data<<endl;
}
printf("全部删除单链表:");
for(i=0;i<20;i++)
ListDel(Head,i);
//delete [] Head;
}
#include < malloc.h >
#include < iostream.h >
#define SUCCESS 1
#define FAIL -1;
typedef struct Node ... {
int Data;
struct Node *Next;
} LNode,*LinkList;
LNode * Head = (LNode * )malloc( sizeof (LNode));
void ListCreate()
... {
Head->Next=NULL;
}
void ListInit()
... {
int i;
LNode *Temp=Head;
for (i=0;i<20;i++)
...{
LNode *p=(LNode *)malloc(sizeof(LNode));
p->Data=i;
p->Next=NULL;
Temp->Next=p;
Temp=Temp->Next;
}
}
int ListInsert(Head , int i, int e) //在头结点为Head的单链表第i个元素之前插入e元素
... {
int j=0;
LNode *q=L;
LNode *p=(LNode *)malloc(sizeof(LNode));
p->Data=e;
if(0==i)
...{
L=p;
L->Next=NULL;
return SUCCESS;
}
while(q&&(j<i))
...{
j++;
q=q->Next;
}
if((q==NULL)||(q->Next==NULL))
return FAIL;
p->Next=q->Next;
q->Next=p;
return SUCCESS;
}
void ListDel(LNode * L, int i)
... {
int j=0;
LNode *p=L;
while((p!=NULL)&&(j<i))
...{
j++;
p=p->Next;
}
if((p==NULL)&&(p->Next==NULL))
return ;
p->Next=p->Next->Next;
}
void main()
... {
int i;
LNode *p=Head;
ListCreate();
ListInit();
if (-1==ListInsert(Head,5,119))
return;
printf("删除前链表: ");
for (i=0;i<10;i++)
...{
p=p->Next;
cout<<p->Data<<endl;
}
printf("删除后链表: ");
ListDel(Head,5);
p=Head;
for (i=0;i<10;i++)
...{
p=p->Next;
cout<<p->Data<<endl;
}
printf("全部删除单链表:");
for(i=0;i<20;i++)
ListDel(Head,i);
//delete [] Head;
}
程序运行效果图:
华为面试官真的就那么牛吗?华为面试官是个猥亵小人,看我长的帅,居然不改我的题目,说我有没有理解单链表,当初他爸为什么不把他射到墙上!!!