有关单链表的基本操作

来源:互联网 发布:如何写数据库 编辑:程序博客网 时间:2024/06/05 06:33

/*Á´±íʵÏÖ¶ÔѧÉú³É¼¨¹ÜÀíµÄ»ù±¾²Ù×÷*/
/*Ö÷ҪѧϰÁ´±íµÄ»ù±¾²Ù×÷ºÍ½á¹¹ÌåµÄÊìϤʹÓÃ*/

#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>

/*¶¨Òå³£Á¿*/
#define OK 1
#define ERROR 0
#define OVERFLOW (-1)

typedef int Status;

typedef struct     /*¶¨ÒåѧÉúµÄ½á¹¹Ìå*/
{
 int sn;
 char name[32];
 int score;

}data_t;

const char *help[] =

{
     "---function---"
     "1. ?,h,H     -- this help/n",

     "2. a,A,i,I   -- add a node, ex: a 10/n",

     "3. d,D       -- del a node, ex: d 10/n",

     "4. p,P       -- print this link/n",

     "5. g,G       -- get link num/n",

     "6. r,R       -- reverse the link/n",

     "7. x,X,q,Q   -- exit this program/n",

};

typedef struct LNODE{ /*¶¨Òåµ¥Á´±í*/

    data_t data;
    struct LNODE *next;
}node_t,*link_t;

link_t creat_link()  /*create linkbiao*/
{
    link_t head;
    head=(link_t)malloc(sizeof(node_t));
    head->next=NULL;
    head->data.sn=0;
    return head;
 }

void destroy_link(link_t head)
{
    node_t *p=head;
    while(head!=NULL){         /*zhuyi*/
        p=head->next;
        free(head);
        head=p;
    }
 }

void   print_link(link_t head)
{
     node_t *p = head->next; /*Ìø¹ýÍ·½Úµã*/
     int cnt = 0;
    printf("pos :  SN %20s score/n", "name      ");
     while (p != NULL) {
          printf("%04d: %4d%20s%4d/n",
          cnt++, p->data.sn, p->data.name, p->data.score);
          p = p->next;

     }

}

Status   empty_link(link_t head)
{
     if (head==NULL || head->next == NULL)

         return OK;
     else
         return ERROR;

}

int  length_link(link_t head)
{
     int len = 0;
     node_t *p = head->next;
     for(; p!=NULL; p = p->next)
     len++;
     return len;
}

node_t* locate_link(link_t head, int idx)
{
     int pos = -1;
     node_t *p = head;
     while (p!=NULL && pos < idx) {
         p = p->next; pos ++;
     }
     return p;
}

Status insert_node(link_t head, int idx, data_t e)

{

     node_t *add = NULL; /* ´ý¼ÓÈëµÄ½ÚµãÖ¸Õë */
     node_t *p = locate_link(head, idx-1); /*×¢Òâ²éѯidx-1*/
     if (p == NULL) return ERROR;
     add = (node_t *)malloc(sizeof(node_t));
     add->data = e;
     add->next = p->next;
     p->next = add;
     return OK;
}

Status delete_node(link_t head, int idx, data_t *e)
{

     node_t *p = locate_link(head, idx-1); /*×¢Òâ²éѯidx-1*/
     node_t *q = NULL; /* pΪ´ýɾ³ýµÄ½ÚµãÖ¸Õë */
     if (p == NULL || p->next== NULL) return ERROR;
     q = p->next;
     *e = q->data;
     p->next = q->next;
     free (q);
     return OK;
}

void reverse_link(link_t head)
{
     node_t *p1, *p2;
     p1 = head->next;
     head->next = NULL;
     while (p1 != NULL)    {
         p2 = p1->next;
         p1->next = head->next;
         head->next = p1;
         p1 = p2;
     }
}


main()
{
   char cmd;
   int i,idx;
   data_t tmp;

   link_t head=creat_link();


   for(i=0;i<10;i++){

      tmp.sn=i;
      tmp.score=100-i;
      strcpy(tmp.name,"alen");

      insert_node(head,i,tmp);
   }

   for(i=0;i<sizeof(help)/sizeof(help[0]);i++)
   printf("/t%s",help[i]);
   printf("/n");
   printf("input command:");

   while(cmd=getch())
   {
    printf("%c/n",cmd);
    switch(cmd){

    case '?':case 'h':case 'H':
       for(i=0;i<sizeof(help)/sizeof(help[0]);i++)
          printf("/t%s",help[i]);
       printf("/n");
    break;

    case 'a': case'A': case'i': case'I':
       printf("please input where do you want to insert:");
       scanf("%d",&idx);
       if(idx<0||idx>length_link(head)){
          printf("insert pos error!/n");
          break;
       }

       printf("please input the SN, name and score:");
       scanf("%d%s%d", &tmp.sn, tmp.name, &tmp.score);
       if (insert_node(head, idx, tmp) == ERROR){
           printf("insert error!/n");
           break;
           }
       printf("insert ok!/n");
       print_link(head);
       break;

     case 'd':case'D':
       printf("please input which do you want to delete:");
       scanf("%d",&idx);
       if(idx<0||idx>length_link(head)){
          printf("delete pos error!/n");
          break;
       }
       if(delete_node(head, idx, &tmp) == NULL) {
          printf("input error!/n");
          break;
       }

       printf("delete ok!/n");
       print_link(head);
       break;

      case'p': case 'P':
        print_link(head);
        break;

      case 'x':case'X': case'q': case'Q':
        destroy_link(head);
        return 0;

      case'r': case'R':
        reverse_link(head);
        printf("this link have been reversed!/n");
        print_link(head);
        break;

      default:
        printf("input error!,for help,please input ?/n");

    }

       printf("/n/n input command:");
   }
 } 

原创粉丝点击