|

楼主 |
发表于 2011-8-14 23:05:43
|
显示全部楼层
- #include<stdio.h>
- #include<malloc.h>
- int listlength=0;
- struct node
- {
- int data; //数据域
- struct node *next; //指针域
- };
- //函数声明
- struct node * creat(void); //创建链表
- void print(struct node *); //输出链表
- void listinsert(struct node *); //插入元素
- void listdelete(struct node *); //删除元素
- //主函数
- int main(void)
- {
- //在主函数里创建空的头指针
- struct node * phead = NULL;
-
- phead = creat(); //从头指针开始创建链表
- printf("\n");
- char quit;
- do
- {
- char select;
- printf("输入 p:打印 i:插入 d:删除\n");
- select = getch();
- switch(select)
- {
- case 'p' :
- {
- printf("\n当前小妞总数: %d\n当前可以接客的小妞有:\n",listlength);
- print(phead);
- printf("\n");
- } break;
- case 'i' :
- {
- char exit;
- do
- {
- printf("在职的小妞:\n");
- print(phead);
- printf("你有多少小妞我全收了!\n");
- listinsert(phead);
- ++listlength;
- printf("当前小妞总数: %d",listlength);
- printf("\ne:退出\t任意键继续\n");
- exit = getch();
- }
- while(exit != 'e');
- printf("\n感谢您为我们带来新的小妞!\n");
- } break;
- case 'd' :
- {
- char exit;
- do
- {
- printf("在职的小妞:");
- print(phead);
- printf("我们这里有很多小妞,你可以随意的挑选!\n");
- listdelete(phead);
- --listlength;
- printf("当前小妞总数: %d",listlength);
- printf("\ne:退出\t任意键继续\n");
- exit = getch();
- }
- while(exit != 'e');
- printf("\n记得再来哦!\n");
- } break;
- default : exit(-1);
- }
- printf("欢迎下次光临!\n\n");
- printf("q:quit\tanykey to continue\n\n");
- quit = getch();
- }while(quit != 'q');
- return 0;
-
- }
- //创建链表函数
- struct node * creat(void)
- {
- int length; //链表的长度
- int variables; //零时变量
- int i; //循环变量
-
- struct node * phead = (struct node *)malloc(sizeof(struct node));
- //为头指针分配一个没有数据的空间
-
- if(NULL == phead) //如果头指针分配为空,说明内存没有空间了
- {
- printf("先生,客房已满,欢迎下次光临!");
- exit(-1);
- }
-
- struct node * ptail = phead; //创建一个尾指针,使头指针的值赋给尾指针
- ptail->next = NULL; //尾指针的后继赋空值
-
- printf("你想要几位小妞?\n");
- scanf("%d",&length);
- listlength = length;
-
- for(i = 0; i < length; i++)
- {
- printf("输入你想要的第%d位小妞的编号: ",i+1);
- scanf("%d",&variables);
-
- struct node * pnew = (struct node *)malloc(sizeof(struct node));
-
- if(NULL == pnew)
- {
- printf("先生,客房已满,欢迎下次光临!");
- exit(-1);
- }
-
- pnew->data = variables; //把输入的数据给新指针的数据域
- ptail->next = pnew; //把新指针的当前地址给尾指针的后继
- pnew->next = NULL; //新指针的后继赋空值
- ptail = pnew; //尾指针移动到新指针的位置,也就是尾指针向后移
- }
- return (phead);
- }//创建链表函数完成
- //链表打印函数
- void print(struct node * phead)
- {
- struct node * p = phead->next; //创建一个p指针指向第一个元素的位置
-
- while(p)
- {
- printf("%d ",p->data); //输出当前数据
- p = p->next; //p指针向后移
- }
- printf("\n\n");
- }//链表打印函数完成
- //链表插入函数
- void listinsert(struct node * phead)
- {
- struct node * p = phead; //创建查找指针
-
- int locate=0; //插入位置
- int veriables=0; //插入数据
- int i=1; //循环变量
- struct node * lover = NULL; //建一个情人
-
- printf("先生,你要把新带来的小妞放在哪?\n");
- scanf("%d",&locate);
-
- while(p && i<locate) //查找位置
- {
- p=p->next;
- ++i;
- }
-
- struct node * insert = (struct node *)malloc(sizeof(struct node)); //创建插入节点
-
- if(NULL == insert)
- {
- printf("先生,客房已满,欢迎下次光临!");
- exit(-1);
- }
-
- printf("先生,请为你新带来的这位小姐编号:\n");
- scanf("%d",&veriables);
-
- insert->data = veriables; //赋值给插入结点
- lover = p->next; //用建立的情人lover保存当前地址的后继地址
- p->next = insert; //把插入结点地址给当前指针的后继
- insert->next = lover; //把情人lover保存的地址给插入结点的后继
- print(phead);
- }//插入函数完成
- void listdelete(struct node * phead)
- {
- struct node * p = phead; //创建查找指针p
-
- int locate=0; //要删除的位置
- int i=1; //循环变量
- int veriables; //保存删除的数据
-
- struct node * deleter = NULL; //创建一个空指针,用于指向要删除的结点
-
- printf("你想要带走第几位小妞?\n");
- scanf("%d",&locate);
-
- while(p && i<locate) //查找删除位置
- {
- p = p->next;
- ++i;
- }
-
- if(!(p->next) || i>locate)
- {
- printf("先生你找的小妞不在,请另选一位!");
- exit(-1);
- }
-
- deleter = p->next; //把p的地址后继的地址给零食指针
- p->next = deleter->next ; //删除了p指针后面的一项
- veriables = deleter->data; //把删除的数据保存起来
- free(deleter);
- print(phead);
- printf("被你带走的是%d小妞: \n",veriables);
- }
复制代码 |
|