Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5206146
  • 博文数量: 553
  • 博客积分: 13864
  • 博客等级: 上将
  • 技术积分: 11041
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-28 21:25
个人简介

个人Blog: hhktony.com

文章分类

全部博文(553)

文章存档

2015年(1)

2014年(2)

2013年(12)

2012年(384)

2011年(154)

分类: LINUX

2012-03-08 04:15:19

#include
#include

typedef struct student
{
    int number;
    struct student *next;
    char name[10];        
}student;

typedef struct student *stud;
void CreateList();
stud head;

void detele();
void CreateList();
void insertList();
void print();
void destorylist();
void reverse();

int main(int argc, char *argv[])
{
   int c=0;
   while(c!=-1)
   {
       printf("/***********************************/\n");
       printf("1.创建链表\n2.插入元素\n3.删除单个元素\n4.销毁元素\n5.翻转链表\n-1.退出");
       printf("/***********************************/\n");
       printf("请选择操作:");
       scanf("%d",&c);
       switch(c)
       {
          case 1:
             CreateList();
             break;
          case 2:                
              insertList();
              break;
          case 3:
              detele();
              break;
          case 4:
             destorylist();
             break;
          case 5:
             reverse();
             break;
          default:
               exit(0);
               break ;          
       }
   }
   system("PAUSE");
   return 0;
}

/*创建链表*/
void CreateList()
{
     stud pointer;
     stud New;
     head=(stud)malloc(sizeof(student));
     if(head==NULL)
     {
        printf("Memory malloc failture!");
        exit(-1);
     }
     pointer=head;
     int i=0;
     //循环读入
     while(i++<3)
     {
       New=(stud)malloc(sizeof(student));
       if(New==NULL)
       {
         printf("Memory malloc failture!");
         exit(-1);
       }
       printf("please input the number:");
       scanf("%d",&New->number);
       printf("please input the name:");
       scanf("%s",New->name);
       New->next=NULL;
       pointer->next=New;
       pointer=New;
       printf("\n");
     }
     print();     
}

/*插入链表*/
void insertList()
{
    int tag;
    int pos;
    printf("请选择插入方式:0.头部   1.尾部   2.中间:");
    scanf("%d",&pos);
    if(pos==2)
    {
       printf("\n请选择欲插入的位置:");
       scanf("%d",&tag);
    }
    stud pointer;
    stud New;
    New=(stud)malloc(sizeof(student));
    if(New==NULL)
    {
       printf("Memory malloc failture!");
       exit(-1);
    }
    printf("please input the number:");
    scanf("%d",&New->number);
    printf("please input the name:");
    scanf("%s",New->name);
    //插入头部
    if(pos==0)
    {
       pointer=head;
       pointer=pointer->next;
       New->next=pointer;
       head->next=New;
    }
    //插入尾部
    else if(pos==1)
    {
       pointer=head;
       while(pointer->next!=NULL)
        pointer=pointer->next;
       pointer->next=New;
       New->next=NULL;
    }
    //插入中间某个位置
    else
    {
      int index=1;
      pointer=head;
      stud back;
      pointer=pointer->next;
      while(indexnext!=NULL)
      {
         index++;
         back=pointer;
         pointer=pointer->next;
      }
      if(index!=tag)
      {
        printf("\nNo suce student\n");
        exit(-1);
      }
      back->next=New;
      New->next=pointer;
    }
    print();
}

/*打印链表*/
void print()
{
   stud pointer;
   pointer=head;
   while(pointer->next!=NULL)
   {
      pointer=pointer->next;
      printf("Number:%d\t%s\n",pointer->number,pointer->name);
   }
   printf("\n\n");      
}

/*销毁链表*/
void destorylist()
{
   stud pointer;
   stud back;
   pointer=head;
   pointer=pointer->next;
   free(head);
   while(pointer!=NULL)
   {
      back=pointer;
      pointer=pointer->next;
      printf("delete %d\n",back->number);
      free(back);
   }    
}

/*删除单个链表元素*/
void detele()
{
   int number;
   printf("请选择要删除的元素:");
   scanf("%d",&number);
   stud pointer;
   stud backs;
   pointer=head;
   backs=pointer;
   pointer=pointer->next;
   while(pointer!=NULL)
   {
      if(pointer->number==number)
      {
        backs->next=pointer->next;
        free(pointer);
        printf("\ndelete successful!\n");
        break;
      }
      backs=pointer;
      pointer=pointer->next;
   }
   print();        
}
/*链表翻转*/
void reverse()
{
    stud pointer;
    stud back;
    stud temp;
    if(head->next==NULL)
      exit(0);
    pointer=back=head->next;
    int tag=0;
    while(pointer->next!=NULL)
    {
       pointer=pointer->next;
       temp=pointer->next;
       pointer->next=back;
       if(tag==0)
       {
         back->next=NULL;
         tag++;
       }
       back=pointer;
       pointer=temp;
    }
    pointer->next=back;
    head->next=pointer;
    print();    
}

阅读(1555) | 评论(0) | 转发(0) |
0

上一篇:C语言指针总结

下一篇:链表逆序

给主人留下些什么吧!~~