记自己完完整整写出的第一个单链表程序
#include <stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int n;
struct Node* Create(int n)
{
struct Node* head=NULL;
head=(struct Node*)malloc(sizeof(struct Node));
head->next=NULL;
struct Node* P;
struct Node* End;
End=head;
for(int i=0;i<n;i++)
{
P=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&P->data);
P->next=NULL;
End->next=P;
End=P;
}
return head;
}
int Insert(struct Node*head,int num,int data)
{
struct Node *temp,p;
temp=head;
p=(struct Node)malloc(sizeof(struct Node));
if(num==0)
{
p->data=data;
p->next=head->next;
head->next=p;
}
else if(num>n)
return -1;
else {
for(int i=0;i<num;i++)
{
temp=temp->next;
}
p->data=data;
p->next=temp->next;
temp->next=p;
n++;}
return 0;
}
int Delete(struct Node* head,int num)
{
struct Node* temp,*p;
temp=head;
if((num<1)&& (head))
return -1;
for(int i=0;i<num;i++)
{
p=temp;
temp=temp->next;
if(!(p->next)||i>n)
return -1;
}
p->next=p->next->next;
n--;
free(temp);
return 0;
}
void print(struct Node* head)
{
struct Node* temp;
temp=head->next;
while(temp)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
int main()
{
struct Node* head;
scanf("%d",&n);
head=Create(n);
int judge;
int Onum;
//int Bool;
scanf("%d",&Onum);
int num;
for(int i=0;i<Onum;i++)
{
scanf("%d",&judge);
scanf("%d",&num);
switch (judge)
{
case 0:
{
int Pdata;
scanf("%d",&Pdata);
Insert(head,num,Pdata);
break;
}
case 1:
{
if(num==0)
break;
Delete(head,num);
}
}
}
print(head);
return 0;
}