“头结点是头指针指向的结点,

首结点是头结点指向的下一个结点。头结点无数据域,首结点有数据域。
头结点(通常无数据域

**

学习链表首先会创建吧:

**

有头结点
List Read()
{
   
	struct Node *p=(struct Node*)malloc(sizeof(struct Node)),*head=p;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
   
		p->Next=(struct Node*)malloc(sizeof(struct Node));
		p=p->Next;
		scanf("%d",&p->Data);
	}
	p->Next=NULL;
	return head;
}
无头结点
struct ListNode *readlist()
{
      struct ListNode  *head=NULL, *p=NULL ,*temp =NULL;
    int x;
    scanf("%d",&x);
    while(x != -1){
   
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = x;
        p->next = NULL;
        if(temp==NULL)
            head = temp = p;
        else{
   
            temp->next = p;
            temp = p;
        }
        scanf("%d",&x);
    }
    return head;
}

遍历链表:

无头结点
void Print(List L)
{
   
    while (L) {
    printf("%d ", L->Data);
    L= L->Next;    }
    printf("\n");
}
有头结点
void Print(List L)
{
   
    while (L= L->Next) {
    printf("%d ", L->Data);    }
    printf("\n");
}

插入链表:

tmp->Next = p->Next;
p->Next = tmp;

List Insert(List L,ElementType X)
{
   
	struct Node *tmp=(struct Node*)malloc(sizeof(struct Node));
	tmp->Data=x;
	struct Node *p=L;
	while(p->Next&&p->Next->Data<X){
   p=p->Next;}
	tmp->Next=p->Next;
	p->Next=tmp;
	return L;
}

删除链表:

删除链表的元素也就是把前节点的指针域越过要删除的节点指向下下个节点。
即:p->next = q->next;然后放出q节点的空间,
即free(q);

带头结点的删除(P=L )
不带头结点(P=L->next)

struct ListNode *deletem( struct ListNode *L, int m )
{
   
	struct ListNode *p=NULL,*temp=NULL;
	if(L==NULL)
	{
   
		return NULL;
	}
	p=L;
	temp=p->next ;
	while(temp)
	{
   
		if( temp->data ==m )
		{
   
			p->next =temp->next ;
			free(temp);
			temp=p->next ;
		}
		else 
		{
   
			p=p->next ;
			temp=p->next ;
		}
	}
	if(L->data ==m )
	{
   
		L=L->next ;
	}
	return L;
}