zoukankan      html  css  js  c++  java
  • a simple linked list

    header file:

       1:  #ifndef LINKLIST_H_INCLUDED
       2:  #define LINKLIST_H_INCLUDED
       3:   
       4:  struct node;
       5:  typedef struct node mynode;
       6:  typedef struct node *ptr_to_node;
       7:  typedef ptr_to_node list;
       8:  typedef ptr_to_node position;
       9:   
      10:  list create_empty(void);
      11:  int is_empty(list l);
      12:  int is_last(position p,list l);
      13:  position find(char x,list l);
      14:  void delete_element(char x,list l);
      15:  void delete_position(position p,list l);
      16:  position find_previous(char x,list l);
      17:  void insert_after(char x,list l,position p);
      18:  void insert_before(char x,list l,position p);
      19:  void delete_list(list l);
      20:  position get_header(list l);
      21:  position get_first(list l);
      22:  char get_value(position p);
      23:   
      24:  #endif // LINKLIST_H_INCLUDED

    implement file:

       1:  #include <stdio.h>
       2:  #include <stdlib.h>
       3:  #include "linklist.h"
       4:   
       5:  struct node
       6:  {
       7:      char value;
       8:      ptr_to_node next;
       9:  };
      10:   
      11:  int is_empty(list l)
      12:  {
      13:      return l->next==NULL;
      14:  }
      15:   
      16:  list create_empty(void)
      17:  {
      18:      return (list)malloc(sizeof(mynode));
      19:  }
      20:   
      21:  int is_last(position p,list l)
      22:  {
      23:      return p->next==NULL;
      24:  }
      25:   
      26:  position get_first(list l)
      27:  {
      28:      return l->next;
      29:  }
      30:   
      31:  position get_header(list l)
      32:  {
      33:      return l;
      34:  }
      35:   
      36:  char get_value(position p)
      37:  {
      38:      return p->value;
      39:  }
      40:   
      41:  position find(char x,list l)
      42:  {
      43:      position p=l->next;
      44:      while(p!=NULL)
      45:      {
      46:          if(p->value==x)
      47:          {
      48:              return p;
      49:          }
      50:          p=p->next;
      51:      }
      52:   
      53:      return NULL;
      54:  }
      55:   
      56:  position find_previous(char x,list l)
      57:  {
      58:      position p=l;
      59:      while(p!=NULL && p->next!=NULL)
      60:      {
      61:          if(p->next->value==x)
      62:          {
      63:              return p;
      64:          }
      65:          p=p->next;
      66:      }
      67:   
      68:      return NULL;
      69:  }
      70:   
      71:  position find_previous2(position p,list l)
      72:  {
      73:       position c=l;
      74:       while(c!=NULL && c->next!=p)
      75:       {
      76:           c=c->next;
      77:       }
      78:       return c;
      79:  }
      80:   
      81:  void insert_after(char x,list l,position p)
      82:  {
      83:      ptr_to_node t=create_empty();
      84:      t->value=x;
      85:      position oldnext=p->next;
      86:      t->next=oldnext;
      87:      p->next=t;
      88:  }
      89:   
      90:  void insert_before(char x,list l,position p)
      91:  {
      92:      position previous=find_previous2(p,l);
      93:      if(previous==NULL)
      94:      {
      95:          return;
      96:      }
      97:   
      98:      insert_after(x,l,previous);
      99:  }
     100:   
     101:  void delete_element(char x,list l)
     102:  {
     103:      position current=find(x,l);
     104:      if(current==NULL)
     105:      {
     106:          return;
     107:      }
     108:      position previous=find_previous(x,l);
     109:      previous->next=current->next;
     110:      free(current);
     111:  }
     112:   
     113:  void delete_position(position p,list l)
     114:  {
     115:      position previous=find_previous2(p,l);
     116:      previous->next=p->next;
     117:      free(p);
     118:  }
     119:   
     120:  void delete_list(list l)
     121:  {
     122:      position p=l->next;
     123:      position t;
     124:      while(p!=NULL)
     125:      {
     126:          t=p->next;
     127:          free(p);
     128:          p=t;
     129:      }
     130:      //RESET THE HEADER
     131:      l->next=NULL;
     132:  }
     133:   
     134:  void print_list(list l)
     135:  {
     136:      position p;
     137:      p=l;
     138:      while(p!=NULL)
     139:      {
     140:          printf("%c,",p->value);
     141:          p=p->next;
     142:      }
     143:      printf("\n");
     144:  }

    main:

       1:  int main()
       2:  {
       3:      int i;
       4:      position p;
       5:      list l=create_empty();
       6:      l->value='%';
       7:      l->next=NULL;
       8:      for(i=65;i<91;i++)
       9:      {
      10:          insert_after(i,l,l);
      11:      }
      12:   
      13:      print_list(l);
      14:   
      15:      p=find('B',l);
      16:      if(p!=NULL)
      17:      {
      18:          printf("B after is:%c\n",p->next->value);
      19:      }
      20:   
      21:      delete_element('C',l);
      22:      print_list(l);
      23:   
      24:      p=find('B',l);
      25:      insert_before('C',l,p);
      26:      print_list(l);
      27:   
      28:      delete_position(p,l);
      29:      print_list(l);
      30:   
      31:      p=find('X',l);
      32:      if(p!=NULL)
      33:      {
      34:          printf("found X:%c\n",p->value);
      35:      }
      36:      p=find_previous2(p,l);
      37:      if(p!=NULL)
      38:      {
      39:          printf("X before is:%c\n",p->value);
      40:      }
      41:      delete_list(l);
      42:      print_list(l);
      43:      return 0;
      44:  }
  • 相关阅读:
    Eclipse的Tomcat插件安装
    Struts1.2 struts-config.xml配置详解
    JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    dede 忘记密码在数据库中修改方法
    如何将XML文件中的数据传送并保存在关系数据库中
    lamp
    开发人员必读的11本最具影响力书籍
    org.apache.commons.lang.StringUtils类
    B. Jzzhu and Sequences
    线段树——习题、lazy解析
  • 原文地址:https://www.cnblogs.com/dancewithautomation/p/2559447.html
Copyright © 2011-2022 走看看