/**
链表基本操作
增,删,查,改,创建,摧毁
2016.12.7
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
using namespace std;
typedef struct node{
int data;
struct node *next;
}NODE;
///遍历链表
void showList(NODE *head){
NODE *p=head;
while(p){
if(!p->next){///细节处理链表最后一个元素后加换行,其余加空格
printf("%d\n",p->data);
}
else{
printf("%d ",p->data);
}
p=p->next;
}
}
///摧毁链表,释放每个节点的内存
void des(NODE *p){
while(p){
des(p->next);
free(p);
}
}
///在第一个5后面添加一个5
void addNode(NODE*head){///参数列表新的head(1)指向主函数的head(2),两个指针指向同一块内存。
NODE *p=head,*tmp;
while(p->data!=5)p=p->next;
tmp=(NODE*)malloc(sizeof(NODE));
tmp->data=p->data;
tmp->next=p->next;
p->next=tmp;
}
///在每一个奇数后添加100
void addList(NODE*head){
NODE*p=head;
while(p){
if(p->data%2){
NODE*q=(NODE*)malloc(sizeof(NODE));
q->data=100;
q->next=p->next;
p->next=q;
}
p=p->next;
}
}
///删除节点,测试为删除数据为5的节点
NODE* del(NODE**head){///指向指针的指针
NODE *p=*head,*q;
q=p->next;
while(q){
if(q->data==5){
p->next=q->next;
free(q);
q=p->next;
}
else{
p=p->next;
q=q->next;
}
}
if((*head)->data==5){///如果头要删除是,返回新的头指针
p=*head;
*head=(*head)->next;
free(p);
}
}
int main()
{
NODE *head,*tail,*p;
head=tail=NULL;
int a;
while(~scanf("%d",&a)){///创建链表,以EOF为结束
p=(NODE*)malloc(sizeof(NODE));
p->data=a;
p->next=NULL;
if(!head){
tail=head=p;
}
else{
tail->next=p;
tail=p;
}
}
//addNode(head);
//addList(head);
del(&head);
showList(head);
return 0;
}
转载于:https://www.cnblogs.com/Q1143316492/p/6260746.html