#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head, *tail, *p, *q, *r;
void SequenceCreate () /*顺序建立链表*/
{
/*建立一个空结点*/
head = ( struct node *) malloc ( sizeof ( struct node ));
head->next = NULL;
tail = head;
int n;
scanf("%d", &n);
while( n-- )
{
p = ( struct node *) malloc ( sizeof ( struct node ));
scanf("%d", &p->data);
/*把数据节点挂到尾部*/
tail->next = p;
tail = p;
}
}
void InvertedSCreate () /*逆序建立链表*/
{
/*建立一个空结点*/
head = ( struct node *) malloc ( sizeof ( struct node ));
head->next = NULL;
int n;
scanf("%d", &n);
while ( n-- )
{
p = ( struct node *) malloc ( sizeof ( struct node ));
scanf("%d", &p->data);
/*把数据节点插到头节点后作链第一个节点*/
p->next = head->next;
head->next = p;
}
}
void Insert ( struct node *p, int key ) /*插入key*/
{
/*插入前最重要的是找到 p 节点,即插入位置的前驱节点*/
q = ( struct node * ) malloc ( sizeof( struct node ));
q->data = key;
q->next = NULL;
q->next = p->next;
p->next = q;
//逆序建立链表到懂了的话这段很容易理解
}
void del ( struct node *head, int key ) /*删除key*/
{
int flag = 0;
p = head;
while(p->next)
{
/*最重要的依然是找到前驱结点*/
if(p->next->data == key)
{
q = p->next;
p->next = q->next;
free(q);
break;
}
else p = p->next;
}
}
void LinkListprint ( struct node *head ) /*输出链表*/
{
p = head->next;
while(p->next)
{
printf("%d ", p->data);
p = p->next;
}
printf("%d\n", p->data);
}
int main()
{
return 0;
}