链表的 实现

#include <stdio.h>
#include <stdlib.h>
struct node;
typedef struct node *P;//意思是p 指向struct node的指针
typedef P list;//list 与 ptrtonode 同等意思,便于在下面的编程中理解
typedef P position;//意思是表示自引用结构中便于理解position是指向当前自身的指针
list makelist(list L);//用于创造空链表,而其类型是list原因是makelist返回的是链表,而其类型是list
int isempty(list L);//判断是否为空链表,如果为空则返回true
int islast(position P,list L);//P是指向当前结点的指针,并且它的类型是position
position find (int x,list L);//在L链表中查找元素x
void delete(int x,list L); 
void insert(int x,list L,position P);//P代表在结点,而x是代表在链表L中P结点前面插入x
position findprevious(int x,list L); 
position header(list L);//不知道有什么用
position first(list L);//同上
position advance(position P);//同上
int retrieve(position P);//retrieve 意思是检索,恢复,其他同上
void printlist(list L); 
struct node{
  int data;
  position next;
};

void main(){
  list L;
  int n;
  L = makelist(list L); 
  printf("0 represent the element you want to look for in the list\n");
  printf("1 represent the element you want to delete in the list\n");
  printf("2 represent the element you want to insert in the list\n");
 // scanf("%d",&n);
    while (n == 0 || n == 1|| n == 2){ 
    prinf("please input choose:\n");
       scanf("%d",&n);
      switch(n){
        case '0':
           printf("please input element you want to look for:\n");
                   int c;
             position P;
               scanf("%d",&c);
                P= find(c,L);
                printf("%s",P);
        case '1':
          printf("please input element you want to delete:\n");
             int c;
            scanf("%d",&c);
          delete(c,L);
       case '2':
          printf("please input element you want to insert:\n");
            int c;

            position P;
             scanf("%d,%s",&c,P);
            insert(c,L,P);
       default:
         return "choosing error";
      }
    }
}

void printlist(list L){
  position P;
  P = header(L);
  printf("the list is :\n");
  if(P != NULL)
  do {
    printf("%d--",P -> data);
    p = P -> next;
  }while(P != NULL);
}

list makelist(list L){
   position P;
   P =L;
   P -> next = NULL;
   return P;
}

int isempty(list L){//return true if empty
  return L-> next == NULL;
}

int islast(position P,list L){//ruturn true if P is the last position in list L.parametre(参数,系数) L is unused in this implementation(履行,实现)
     return P -> next == NULL;//judge if the list is last ,if the list is null ,the return's value is true(1)
}

position find (int x,list L){//look for the present node x
   position P;
   p = L -> next;
   while(P != NULL && P -> data != x)
        P = P -> next;
    return P;
}

void delete (int x,list L){
  position P,tmpcell;
     p = findprevious(x,L);
    if(!islast(P,L)){
      tmpcell = P -> next;
      p -> next = tmpcell -> next;
      free(tmpcell);

      printlist(L);
     }
    else
    printf("the list is null\n");
}

position findprevious(x,L){//look for the next node  x
  position P;
  P = L;
  while(P -> next != NULL && P -> next -> data != x)
      P = P -> next;
    return P;
}

void insert(int x,list L,position P){
  position tmpcell;
  tmpcell = malloc(sizeof(struct node));
    if(tmp == NULL)
     printf("out of space");
     tmpcell -> data = x;
     tmpcell -> next = P -> next;
     P -> next = tmpcell;
     print(L);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值