记自己写出的第一个完完整整的程序,有关单链表基本操作(无注释,耗时大),加油加油!

记自己完完整整写出的第一个单链表程序
#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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值