Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1498680
  • 博文数量: 329
  • 博客积分: 2773
  • 博客等级: 少校
  • 技术积分: 4219
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:17
个人简介

淡定从容,宁静致远

文章分类

全部博文(329)

文章存档

2016年(4)

2015年(50)

2014年(68)

2013年(45)

2012年(162)

分类: C/C++

2012-12-16 23:39:04

#include
#include
typedef struct linknode
{
 char data;
 struct linknode *next;
 
}linnode;
linnode *head;
int n;
void CreateList()
{
 n=0;
 linnode *p,*s;
 char x;
 int z=1;
 head = malloc(sizeof(linnode));
 p=head;
 printf("\n\t\t请逐个输入节点,以“x”为结束标记!\n");
 printf("\n");
 while(z)
 {
  printf("请输入一个字符数据:\n");
  scanf("%c",&x);
  getchar();
  if(x!='x')
  {
   s=malloc(sizeof(linnode));
   n++;
   s->data=x;
   p->next = s;
   s->next = NULL;
   p=s;
  }
  else
   z=0;
 }
 
}
void InsList(int i,char x)
{
 linnode *s,*p;
 p = head;
 int j = 0;
 while(p!=NULL&&j {
  j++;
  p=p->next;
 }
 if(p!=NULL)
 {
  s=malloc(sizeof(linnode));
  s->data=x;
  s->next = p->next;
  p->next = s;
  n++;
 }
 else 
  printf("\n\t\t线性表为空或者插入位置超出\n");
}
void DelList(char x)
{
 linnode *p,*q;
 if(head = NULL)
 {
  printf("\n\t\t链表下溢!");
  return;
 }
 if(p->next = NULL)
 {
  printf("链表已经为空!"); 
  return;
 }
 q = head;
 p = head->next;
 while(p->next !=NULL&&p->data!=x)
 {
  q=p;
  p = p->next;
 }
 if(p!=NULL)
 {
  q->next = p->next;
  free(p);
  n--;
  printf("\n\t\t节点%此已经被删除!",x);
 }
 else
  printf("\n\t\t抱歉,没有找到你要删除的数据!");
}
void ShowList()
{
 linnode *p= head;
 printf("\n\t\t显示线性表的所有元素:");
 if(head->next==NULL&&p==NULL)
  printf("\n\t\t链表为空!");
 else
 {
  printf("\n\t\t");
  while(p->next!=NULL)
  {
   printf("%5c",p->next->data); 
   p=p->next;
   
  }
  
 }
 
}
void SearchList(char x)
{
 linnode *p;
 int i=1;
 if(head ==NULL)
 {
  printf("\n\t\t链表下溢!");
  return;
 }
 if(head->next ==NULL)
 {
  printf("\n\t\t链表为空,没有任何节点!");
  return;
 }
 p = head->next;
 i++;
 if(p!=NULL)
 {
  printf("\n\t\t在表的第%d位置上找到%c节点!",i,x);
 }
 else
  printf("\n\t\t抱歉没有找到数值为%c的节点",x);
}
int main()
{
 head = NULL;
 int choice,i,j=1;
 char x;
 while(j)
 {
  printf("\n\t\t    线性表子系统      "); 
  printf("\n\t\t***********************************"); 
  printf("\n\t\t**        1-------建  表        **"); 
  printf("\n\t\t**        2-------插  入        **"); 
  printf("\n\t\t**        3-------删  除        **"); 
  printf("\n\t\t**        4-------显  示        **"); 
  printf("\n\t\t**        5-------查  找        **"); 
  printf("\n\t\t**        6-------求表长        **"); 
  printf("\n\t\t**        0-------返  回        **"); 
  printf("\n\t\t***********************************"); 
  printf("\n\t\t请选择菜单号(1-6):"); 
  scanf("%d",&choice);
  getchar();
  if(choice==1)
   CreateList();
  else if(choice==2)
  {
   printf("\n\t\t请输入插入的位置i和插入的数据(输入格式i,x):");
   scanf("%d,%c",&i,&x);
   InsList(i,x);
    
  }
  else if(choice == 3)
  {
   printf("\n\t\t请输入要删除的数据:"); 
   scanf("%c",&x);
   DelList(x);
  }
  else if(choice ==4)
   if(head == NULL) 
   printf("请先建立线性表!");
   else
    ShowList();
   else if(choice==5)
   {
    printf("\n\t\t请输入要查找的元素:");
    scanf("%c",&x);
    SearchList(x);
    
   }
   else if(choice==6)
   {
    printf("\n\t\t线性表长度为:%d",n); 
    
   }
   else if(choice == 0)
    j=0;
   else
    printf("\n\t\t输入错误!请重新输入!");
   
  
 }
 
}
阅读(1342) | 评论(0) | 转发(1) |
0

上一篇:graphics

下一篇:约瑟夫

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