Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6608
  • 博文数量: 5
  • 博客积分: 145
  • 博客等级: 入伍新兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-01 22:28
文章分类
文章存档

2012年(5)

我的朋友
最近访客

分类: C/C++

2012-12-06 21:44:21

链表是数据结构中最基础的知识,也是应用中最常用的动态分配内存的方法。
下面我以具体的例子来说明对它的操作。
#include
#include
#include
#define len sizeof(struct student)
struct student
{
char name[10];
int age;
struct student *next;
};
struct student *creat(struct student *head)                //创建一个链表
{
int length=0,yes=1;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(len);
printf("please enter one student's name and age:\n");
scanf("%s%d",p1->name,&p1->age);
while(yes)
{
length++;
if(length==1)
head->next=p1;
else
p2->next=p1;
p2=p1;
printf("continue creat list (0--no):");
scanf("%d",&yes);
if(!yes)
break;
else
  {
  p1=(struct student *)malloc(len);
  printf("please enter one student's name and age:\n");
  scanf("%s%d",p1->name,&p1->age);
  }
}
p2->next=NULL;
return head;
}
struct student *print_list(struct student *head)      //打印一个链表
{
struct student *p1;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else
{
p1=head->next;
while(p1!=NULL)
{
printf("%s %d\n",p1->name,p1->age);
p1=p1->next;
}
}
return head;
}
struct student *add_list(struct student *head)            // 增加一个节点
{
int x,n=0;
char name[10]={0};
struct student *new,*p1,*p2;
new=(struct student *)malloc(len);
printf("please enter one student's name and age:\n");
scanf("%s%d",new->name,&new->age);
p1=head->next;
while(p1!=NULL)
{
n++;
p1=p1->next;
}
printf("you have %d place to add a new note,where do you want (1 to %d)???:",n+1,n+1);
scanf("%d",&x);
if(x<=0||x>(n+1))
{
printf("enter error\n");
return head;
}
p2=head;
while(--x && (p2=p2->next));
new->next=p2->next;
p2->next=new;
return head;
}
struct student *delete_list(struct student *head)       //删除一个节点
{
char name[20];
struct student *p1,*p2;
int flag;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else
{
printf("Who you want to delete from the list:\n");
scanf("%s",name);
p1=head->next;
p2=p1->next;
if(!strcmp(p1->name,name))
     {
     head->next=p1->next;                    
     }
else
{
     while(p2!=NULL)
     {
        if(!strcmp(p2->name,name))
        {
        p1->next=p2->next;
        flag=1;
        break;
        }
        else
        {
        p1=p1->next;
        p2=p2->next;
        }
     }
        if(!flag)
        {
        printf("The list dosen't contain the name\n");
        }
}
}
return head;
}

struct student *invert(struct student *head)      //将链表反转
{
struct student *p1,*p2,*p3,*temp;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else
{
temp=p1=head->next;
p2=p3=p1->next;
while(p3!=NULL)
{
p3=p3->next;
p2->next=p1;
p1=p2;
p2=p3;
}
temp->next=NULL;
head->next=p1;
}
return head;
}
 
struct student *destroy(struct student *head)            //销毁一个链表
{
struct student *p1,*p2;
if(head->next==NULL)
{
printf("you have not creat a list!!!!!!!!!!!!!!!\n");
}
else
{
p1=p2=head->next;
while(p1!=NULL)
{
p1=p1->next;
free(p2);
p2=p1;
}
}
head->next=NULL;
return head;
}
 
int main()
{
int choose;
struct student *head;
head=(struct student *)malloc(len);
head->next=NULL;
while(1)
{
printf("1:Creat a new list\n");
printf("2:Print the list\n");
printf("3:Add a note to the list\n");
printf("4:Delete a note from the list\n");
printf("5:Invert the list \n");
printf("6:Destroy the list\n");
printf("7:Exit\n");
printf("\nPlease choose one choice:");
scanf("%d",&choose);
switch(choose)
{
case 1:
printf("\n---------------creat a new list:-------------\n");
head=creat(head);
printf("\n");
break;
case 2:
printf("\n---------------print list:-------------------\n");
head=print_list(head);
printf("\n");
break;
case 3:
printf("\n---------------add a new note:-----------------\n");
head=add_list(head);
printf("\n");
break;
case 4:
printf("\n---------------delete a note:------------------\n");
head=delete_list(head);
printf("\n");
break;
case 5:
printf("\n---------------invert the list-----------------\n");
head=invert(head);
printf("\n");
break;
case 6:
printf("\n--------------destory the list-----------------\n");
head=destroy(head);
printf("\n");
break;
case 7:
exit(0);
default:
printf("Enter error\n");
printf("\n");
}
}
return 0;
}
 
阅读(508) | 评论(0) | 转发(0) |
0

上一篇:linux exec函数族

下一篇:c语言 队列

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