| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 398 人关注过本帖
标题:谁给我看看那错了??
只看楼主 加入收藏
向阳的水仙
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-10-17
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:3 
谁给我看看那错了??
一个线性链表分割成为三个循坏链表 问题
原题目:已知由一个线性链表表示中含有三类字符的数据元素(如:字母字符、数字字符和其他字符),试编写程序将该线性链表分割成为三个循坏链表,其中每个循坏链表表示的线性表中均只含一类字符。

#include <stdio.h>
#include <malloc.h>
#include <ctype.h>




typedef struct LNode
{
    char data;
    struct LNode * next;
}LNode,*LinkList;



void CreateList_L(LinkList &L,int n)
{
    int i;
    LinkList P;
    L=(LinkList )malloc(sizeof(LNode));
    L->data=0;
    L->next=NULL;

    for(i=0;i<n;i++)
    {
        P=(LinkList)malloc(sizeof(LNode));
        scanf("%c",&P->data);
        getchar();
        P->next=L->next;
        L->next=P;
   
    }
}


void CreateList_C(LinkList &L)
{


    L=(LinkList)malloc(sizeof(LNode));

    L->next=L;


}

void LinkList_Divide(LinkList &L,LinkList &A,LinkList &B,LinkList &C)
{
    LinkList p,q,r,s;
   
    s=L->next;
  A=(LinkList)malloc(sizeof(LNode));p=A;
  B=(LinkList)malloc(sizeof(LNode));q=B;
  C=(LinkList)malloc(sizeof(LNode));r=C;
  while (s)
    {

     
        if ( isalpha (s->data) )
        {
            p->next=s;
            p=s;

        }
        else if ( isdigit (s->data) )
        {
           q->next=s;
           q=s;
        }
        else
        {
            r->next=s;
            r=s;
        }

    }

    p->next=A;
    q->next=B;
    r->next=C;
}


void main()
{
    LinkList L,A,B,C,P,q;
    CreateList_L(L,5);


        do
    {
        P=L->next;
        
    printf("%c ",P->data);
L=L->next;
    }while(L->next!=NULL);
    printf("\n");



    CreateList_C(A);
    CreateList_C(B);
    CreateList_C(C);
    LinkList_Divide(L,A,B,C);




P=A->next;
while(P!=A)
{
    printf("%c ",P->data);
    P=P->next;

}
printf("\n");

P=B->next;
do
    {
   
        
    printf("%c ",P->data);
    P=P->next;
    }while(P->next!=B);
printf("\n");


P=C->next;
do
    {
   
    printf("%c ",P->data);
    P=P->next;
    }while(P->next!=C);
printf("\n");



}

[ 本帖最后由 向阳的水仙 于 2010-10-13 21:19 编辑 ]
搜索更多相关主题的帖子: 编写程序 include 线性表 元素 字母 
2010-10-13 21:11
遮天云
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:农村一小伙
等 级:贵宾
威 望:12
帖 子:1132
专家分:2671
注 册:2010-6-1
收藏
得分:20 
程序代码:
#include<stdio.h>
#include<malloc.h>
typedef struct linknode
{
    char data;
    struct linknode *next;
}nodetype;
nodetype *cyccreate()//创建循环链表
{
    char data;
    int i=1;
    nodetype *h=NULL,*s,*t=NULL;
    printf("建立一个循环单链表\n");
    while(1)
    {
        printf("请输入第%d个结点data阈值:",i);
        scanf("%c",&data);
        

        if(data=='0')//以零表示输入结束
            break;
        if(i==1)//建立第一个结点
        {
            h=(nodetype *)malloc(sizeof(nodetype));
            h->data=data;
            h->next=NULL;
            t=h;
        }
        else//建立其余结点
        {
            s=(nodetype *)malloc(sizeof(nodetype));
            s->data=data;
            s->next=NULL;
            t->next=s;
            t=s;//t始终指向生成的循环单链表的最后一个结点
        }
           fflush(stdin);//清除键盘缓存

        i++;
    }
    if(t!=NULL)
        t->next=h;//将最后一个不是NULL的结点的next阈置为h
    return h;
}
void cycdisp(nodetype *h)//输出由h指向的单循环的所有data阈的值
{
    nodetype *p=h;
    printf("输出一个单循环链表\n");
    if(p==NULL)
    {
        printf("链表为空\n");
        return ;
    }
    while(p->next!=h)
    {
        printf("%4c",p->data);
        p=p->next;
    }
    printf("%4c\n",p->data);
}
void split(nodetype *heada,nodetype *headb,nodetype *headc)
{
    char c;
    nodetype *ra,*rb,*rc,*p;
    p=heada->next;
    ra=heada;
    ra->next=NULL;
    rb=headb;
    rb->next=NULL;
    rc=headc;
    rc->next=NULL;
    while(p!=heada)
    {
        c=p->data;
        if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
        {
            ra->next=p;
            ra=p;
        }
        else if(c>='0'&&c<='9')
        {
            rb->next=p;
            rb=p;
        }
        else
        {
            rc->next=p;
            rc=p;
        }
        p=p->next;
    }
    ra->next=heada;
    rb->next=headb;
    rc->next=headc;
}
   
void main()
{
    nodetype *heada,*headb,*headc,*pointer,*q;//pointer为原链表的头结点
    heada=cyccreate();
    cycdisp(heada);
    q=heada;
    while(q->next!=heada)
        q=q->next;
    pointer=(nodetype *)malloc(sizeof(nodetype));//哨兵头结点
    pointer->next=heada;
    heada=pointer;
    q->next=heada;
    headb=(nodetype *)malloc(sizeof(nodetype));//添加哨兵结点
    headb->next=NULL;
    headc=(nodetype *)malloc(sizeof(nodetype));//添加哨兵结点
    headc->next=NULL;
    split(heada,headb,headc);
    //分别输出整理后的节点
    cycdisp(heada);
    cycdisp(headb);
    cycdisp(headc);
}
   
    
楼主看看我的,你的没细看
2010-10-14 10:45
向阳的水仙
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-10-17
收藏
得分:0 
回复 2楼 遮天云
谢谢,知道那错了,看了你的之后发现是链表建错了
2010-10-14 15:54
遮天云
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:农村一小伙
等 级:贵宾
威 望:12
帖 子:1132
专家分:2671
注 册:2010-6-1
收藏
得分:0 
呵呵~,以后注意就行了。一不小心就容易出错
2010-10-14 18:09
快速回复:谁给我看看那错了??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013635 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved