Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68384
  • 博文数量: 16
  • 博客积分: 338
  • 博客等级: 一等列兵
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-25 01:32
文章分类
文章存档

2011年(16)

我的朋友

分类: C/C++

2011-05-04 00:48:06

#include
#include
#include
/*构建结点结构体 */
typedef struct LNode{
       int data;
       struct LNode * next;
}LNode, * LinkList;
/*用于创建链表的函数 */
/*反序构建的*/
LinkList CreateList_L(LinkList L, int n)
{
       int i;
       LinkList p;
       L = (LinkList)malloc(sizeof(LNode));
       L->next = NULL;
       for(i = n; i > 0; --i)
       {
               p = (LinkList)malloc(sizeof(LNode));
               scanf("%d",&p->data);
               p->next = L->next;
               L->next = p;
        }
       return L;
}
/* 用于插入结点的函数 */
LinkList ListInsert_L(LinkList L, int i, int newnode)
{
       LinkList p = L;
       LinkList s;
       int j = 0;
       while(p&&j       {
               p = p->next;
               ++j;
        }
       if(!p||j>i-1)
       {
               printf("位置小于1或大于表长。\n");
               return L;
        }
       s = (LinkList)malloc(sizeof(LNode));
       s->data = newnode;
       s->next = p->next;
       p->next = s;
       return L;
}
/* 用于删除结点的函数 */
LinkList ListDelete_L(LinkList L, int i)
{
       LinkList p = L;
       LinkList s;
       int j=0;
       while(p->next&&j       {
               p = p->next;
               ++j;
        }
       if(!(p->next)||j>i-1)
       {
               printf("删除位置不合理。\n");
               return L;
        }
       s = p->next;
       p->next = s->next;
       printf("%s%d\n","被删除的结点是:",s->data);
       free(s);
       return L;
}

/*用于遍历链表的函数 */
void ListDisp_L(LinkList L)
{
       LinkList p;
       int i=0;
       p = L->next;
       while(p)
       {
               printf("%d:%d\n", ++i,p->data);
               p = p->next;
        }
}

/* 选择排序算法 网上找来的 我自己写的老报错误 */
LinkList ListSort_L(LinkList L)
{
       LinkList h1,p,q,r,s;
       h1=p=(LinkList)malloc(sizeof(LinkList));
       p->next=L;
       while(p->next)
       {
               q=p->next;
               r=p;
               while(q->next)
               {
                       if(q->next->data < r->next->data)
                               r=q;
                       q=q->next;
                }
               if(r!=p)
               {
                       s=r->next;
                       r->next=s->next;
                       s->next=p->next;
                       p->next=s;
                }
               p=p->next;
        }
       L=h1->next;
       free(h1);
       return L;
}
int getoptions()
{
       int opt;
       printf("1: 录入链表\n");
       printf("2: 显示链表\n");
       printf("3: 插入结点\n");
       printf("4: 删除结点\n");
       printf("5: 排序链表\n");
       printf("6: 退出\n");
       printf("输入选项并按回车确认:");
       scanf("%d",&opt);
       return opt;
}
 
int main(int argc, char* argv[])
{
       int opt;
       int where;
       int value;
       int count;
       LinkList L;
       while(1)
       {
               clrscr();
               opt = getoptions();
               if(opt == 1)
               {
                       clrscr();
                       printf("请输入链表初始结点数:");
                       scanf("%d",&count);
                       printf("请输入各个结点数值,每输入一个按回车确认:\n");
                       L = CreateList_L(L, count);
                       clrscr();
                       ListDisp_L(L);
                       system("PAUSE");
                       continue;
                }
               if(opt == 2)
               {
                       clrscr();
                       ListDisp_L(L);
                       system("PAUSE");
                       continue;
                }
               if(opt == 3)
               {
                       clrscr();
                       ListDisp_L(L);
                       printf("请输入插入位置:");
                       scanf("%d", &where);
                       printf("请输入要插入的数值:");
                       scanf("%d", &value);
                       clrscr();
                       L = ListInsert_L(L,where,value);
                       ListDisp_L(L);
                       system("PAUSE");
                       continue;
                }
               if(opt == 4)
               {
                       clrscr();
                       ListDisp_L(L);
                       printf("请输入要删除的位置:");
                       scanf("%d",&where);
                       clrscr();
                       L = ListDelete_L(L,where);
                       ListDisp_L(L);
                       system("PAUSE");
                       continue;
                }
               if(opt == 5)
               {
                       clrscr();
                       L = ListSort_L(L);
                       ListDisp_L(L);
                       system("PAUSE");
                       continue;
                }
               if(opt == 6)
               {
                       return 0;
                }
        }  
}
阅读(1640) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~