标签:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
struct Node{
int date;
struct Node *next;
};
Node *Creatlist(Node *head,int n){
head->next=NULL;
for(int i=0;i<n;i++){
Node *p;
p=(Node*)malloc(sizeof(Node));
scanf("%d",&p->date);
p->next=head->next;
head->next=p;
}
return head;
}
Node *Insertlist(Node *head,int pos,int val){
Node *p,*q;
p=head;
for(int i=1;i<pos;i++){
p=p->next;
}
q=(Node *)malloc(sizeof(Node));
q->date=val;
q->next=p->next;
p->next=q;
return head;
}
Node *Dellist(Node *head,int pos){
Node *p,*pl;
p=head;
for(int i=1;i<pos;i++)p=p->next;
pl=p->next;
p->next=pl->next;
free(pl);
return head;
}
void Display(Node *head){
Node *p;
p=head->next;
while(p!=NULL){
printf("%d ",p->date);
p=p->next;
}
puts("");
}
int main(){
int n;
Node *head;
while(~scanf("%d",&n)){
head=(Node*)malloc(sizeof(head));
head->next=NULL;
head=Creatlist(head,n);
Display(head);
int pos,val;
puts("添加元素");
scanf("%d%d",&pos,&val);
head=Insertlist(head,pos,val);
Display(head);
puts("删除元素");
scanf("%d",&pos);
head=Dellist(head,pos);
Display(head);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4975894.html