链表的建立,插入,删除,查找(1)

磨磨蹭蹭弄完了,效果还行@!

#include<stdio.h>
#include<stdlib.h>
typedef struct No{
 int date;
 No* next;
} Node;
//链表的建立///
Node* creat(int n)
{
 Node *head=NULL,*p,*tail;
 int i;
 for(i=0;i<n;i++){
  p=(Node*)malloc(sizeof(Node));
  scanf("%d",&p->date);
  p->next=NULL;
  if(head==NULL)
   tail=head=p;
  else{
   tail->next=p;
   tail=p;
  }
 }
 return head;
}
/链表的输出
void print(Node* head)
{
 Node* p=head;
 while(p!=NULL){
  printf(p->next==NULL?"%d/n":"%d ",p->date);
  p=p->next;
 }
}
链表的查找/
Node* lookup(Node* head,int a)
{
 Node *p=head;
 while(p!=NULL){
  if(p->date==a)
   return p;
  p=p->next;
 }
 return NULL;
}
链表的插入//
Node* insert(Node *head,int a,int j,int &n)
{
 Node *p,*q=head,*tail;
    p=(Node*)malloc(sizeof(Node));
 p->date=a;
 p->next=NULL;
 int i=0;
 if(j==0){
  p->next=q;
  head=p;
  n++;
  return head;
 }
 else{
  i++;
  while(i!=j){
   q=q->next;
   i++;
  }
  if(i==n)
   q->next=p;
  else {
   p->next=q->next;
   q->next=p;
  }
  n++;
  return head;
 }
}
/链表的删除///
void delet(Node *head)
{
 Node *p=head,*q;
 while(p!=NULL){
  q=p;
  p=p->next;
  free(q);
 }
}
main()
{
 int n,x,j;
 Node *head,*p;
 printf("*******************/n建立链表/nEnter n:");
 scanf("%d",&n);
 putchar('/n');
 printf("Enter n integers:/n");
 head=creat(n);
 putchar('/n');
 print(head);
 printf("********************/n链表的插入/nEnter j and x:/n");
 scanf("%d%d",&j,&x);
 head=insert(head,x,j,n);
 print(head);
 putchar('/n');
 printf("*********************/n链表的查找/nEnter the integer you want to look up:");
 scanf("%d",&x);
 
 p=lookup(head,x);
 if(p==NULL)
  printf("Threr is no integer you need!/n");
 else
    printf("p->date is %d/n",p->date);
 delet(head);
 return 0;
}
 


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值