#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef struct Node
{
int data;
struct Node *next;
} LNode, *Linklist;
void InitList(Linklist *L);
void HeadInsertList(Linklist *L, int e);
void TailInsertList(Linklist *L, int e);
Linklist FindTail(Linklist *L);
Linklist FindElemByPos(Linklist *L, int n);
Linklist FindElemByNum(Linklist *L, int e);
int DeleteElem(Linklist*L,int n);
int GetElem(Linklist *L, int n);
int TellListLength(Linklist *L);
void PrintList(Linklist *L);
int main()
{
Linklist L;
InitList(&L);
for (int i = 1; i < 10; i++)
HeadInsertList(&L,i);
PrintList(&L);
DeleteElem(&L,3);
PrintList(&L);
system("pause");
return 0;
}
void InitList(Linklist *L)
{
(*L) = (Linklist)malloc(sizeof(LNode));
(*L)->next = NULL;
}
void HeadInsertList(Linklist *L, int e)
{
LNode *p = (LNode *)malloc(sizeof(LNode));
if (p)
{
p->data = e;
p->next = (*L)->next;
(*L)->next = p;
}
}
void TailInsertList(Linklist *L, int e)
{
LNode *p = FindTail(L);
LNode *temp = (LNode *)malloc(sizeof(LNode));
if (temp)
{
temp->data = e;
temp->next = p->next;
p->next = temp;
}
}
Linklist FindTail(Linklist *L)
{
LNode *p = (*L);
while (p->next != NULL)
{
p = p->next;
}
return p;
}
Linklist FindElemByPos(Linklist *L, int n)
{
LNode *p = (*L)->next;
int j=1;
while ((p != NULL)&&j<n)
{
p = p->next;
j++;
}
return p;
}
Linklist FindElemByNum(Linklist *L, int e)
{
LNode *p = (*L)->next;
while ((p != NULL) && (p->data != e))
{
p = p->next;
}
return p;
}
int DeleteElem(Linklist *L, int n)
{ int data=-1;
LNode *p=FindElemByPos(L,n-1);
LNode *q=p->next;
data=q->data;
p->next=q->next;
free(q);
return data;
}
Linklist FindElem(Linklist *L, int n)
{
int j = 1;
LNode *p = (*L)->next;
while (p != NULL && j < n)
{
p = p->next;
j++;
}
return p;
}
int GetElem(Linklist *L, int n)
{
if (n < 1)
return -1;
LNode *p = FindElem(L, n);
return p->data;
}
int TellListLength(Linklist *L)
{
int cnt=0;
LNode*p=(*L)->next;
while(p!=NULL)
{
p=p->next;
cnt++;
}
return cnt;
}
void PrintList(Linklist *L)
{
LNode*p=(*L)->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
06-17
285
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-30