网易c++------课时51---链表
2020-11-04 本文已影响0人
heiqimingren



struct node
{
int data;
node *next; //节点本身数据类型,指针
};
int main()
{
node *head;
node *p1,*p2; //实例化一个指针
p1 = (node*)malloc(sizeof(node));
(*p1).data = 1;
p2 = (node*)malloc(sizeof(node));
(*p2).data = 2;
p1->next = p2; //实现了两个节点的链接
p2->next = NULL;
head = p1;
return 0;
}
===============================================================
struct node
{
int data;
node *next; //节点本身数据类型,指针
};
int main()
{
node *head;
node *p1,*p2; //实例化一个指针
int i;
head = 0;
for (i = 1; i <= 5;i++)
{
p1 = (node*)malloc(sizeof(node));
(*p1).data = 1;
if (head==0 ) //链表为空,则将该节点设置为表头
{
head = p1;
p2 = p1;
}
else //链表非空,则将该结点加入到链表的末尾。
{
p2->next = p1;
p2 = p1;
}
}
p2->next = 0;
return 0;
}



/*
1,单个节点数据结构的定义
2,两个节点的串联
3,多个节点的循环初始化
4,链表输出
5,删除节点
*/
struct node
{
int data;
node *next; //节点本身数据类型,指针
};
int main()
{
node *head;
node *p1,*p2; //实例化一个指针
int i;
head = 0;
for (i = 1; i <= 5;i++)
{
p1 = (node*)malloc(sizeof(node));
(*p1).data = i;
if (head==0 ) //链表为空,则将该节点设置为表头
{
head = p1;
p2 = p1;
}
else //链表非空,则将该结点加入到链表的末尾。
{
p2->next = p1;
p2 = p1;
}
}
p2->next = 0;
//输出链表数据
node *p;
p = head;
printf("链表上各结点的数据为: \n");
while(p!=0)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
//删除数据为2的链表节点
p1 = head;
while (p1->data!=2)
{
p2 = p1;
p1 = p1->next;
}
p2->next = p1->next;
delete p1;
return 0;
}