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

    技术2022-05-19  22

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

    #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;} 

     

     


    最新回复(0)