| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 537 人关注过本帖
标题:数据结构
只看楼主 加入收藏
fanbt50
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-4-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
数据结构
# include<stdio.h>
# include<stdlib.h>
typedef struct linknode
{int data;
struct linknode *next;
}node, *linklist;
void Createlist(linklist L)
{node *head,*p,*s;
int n=0,z=1;
int x;
head=malloc(sizeof(node));
p=head;
printf("\n\t\t 建立一个线性表");
printf("\n\t\t 请逐个输入结点,以"x"未结束标记! \n");
while(z)
{printf("\t\t 输入:");
scanf("%c",&x);
getchar();
if(x!='x')
{s=NULL;
n++;
s->data=x;
p->next=s;
s->next=NULL;
p=s;
}
else z=0;
}
}
int Lenlist(linklist L)
{node *p=L;
int n=0;
while(p->next)
{p=p->next; n++ }.....................................................(34)
return n;
}
node *Searchlist(linklist L,int i)
{node *p=L;
int j=0;
while(p->next!=NULL&&j<1)
{p=p->next;
    j++;
}
if(j==1)
return p;
else
return NULL;
}
void Inslist(linklist L,int i,int x)
{node *head,*s,*p;
int n,j=0;
p=head;
while(p!=NULL&&j<1)
{j++;
p=p->next;
}
if(p!=NULL)
{s=NULL;
    s->data=x;
    s->next=p->next;
    p->next=s;
    n++;
}
else
printf("\n\t\t线性表为空或插入位置超出! \n");.............................................(14)
}
void Dellist(linklist L,int x)
{int n;
node *head,*p,*q;
if(head==NULL)
{printf("\t\t链表下溢! \n");
return;
}
if(head->next==NULL)
{printf("\t\t线性表已经为空!\n");
return;
}
q=head;
p=head->next;
while(p!=NULL&&p->data!=x)
{q=p;
p=p->next;
}
if(p!=NULL)
{q->next=p->next;
delete (P);..................................................................(86)
n--;
printf("\t\t %c已经被删除!",x);
}
else
printf("\t\t未找到!\n");
}
void main()
{linklist L;
int n=0,i;
int x;
Createlist(L);
Lenlist(L,n);.............................................................(98)
Searchlist(L,i);
Inslist(L,i,x);
Dellist(L,x);
}
请帮看看,我运行了搞不出来啊。。。(14) (34) (86) (98)有错!!!!!!!!!!!!!
搜索更多相关主题的帖子: 数据结构 
2010-04-26 20:48
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:5 

linklist Searchlist(linklist L,int i)//这个因该是要实现查找吧?
{
    node *p=L;
    int j=0;

    while(p->next!=NULL&&j<1)
    {
        p=p->next;
        j++;
    }
    if(j==1)
        return p;
    else
        return NULL;
}

void Inslist(linklist L,int i,int x)//因该是插入
{
    node *head,*s,*p;
    int n,j=0;
    p=head;
    while(p!=NULL&&j<1)
    {
        j++;
        p=p->next;
    }
    if(p!=NULL)
    {
        s=NULL;
        s->data=x;
        s->next=p->next;
        p->next=s;
        n++;
    }
    else
        printf("\n\t\t线性表为空或插入位置超出!\n");//.............................................(14)
}
2010-04-27 16:16
beyond_one
Rank: 4
等 级:业余侠客
帖 子:61
专家分:206
注 册:2009-6-26
收藏
得分:10 
# include<stdio.h>
# include<stdlib.h>
typedef struct linknode
{int data;
struct linknode *next;
}node, *linklist;
void Createlist(linklist L)
{node *head,*p,*s;
int n=0,z=1;
int x;
head=malloc(sizeof(node));
p=head;
printf("\n\t\t 建立一个线性表");
printf("\n\t\t 请逐个输入结点,以\"x\"未结束标记! \n");   /* "x"改成\"x\" */
while(z)
{printf("\t\t 输入:");
scanf("%c",&x);
getchar();
if(x!='x')
{s=NULL;
n++;
s->data=x;
p->next=s;
s->next=NULL;
p=s;
}
else z=0;
}
}
int Lenlist(linklist L)
{node *p=L;
int n=0;
while(p->next)
{p=p->next; n++; }     /*n++后没有分号*/
return n;
}
node *Searchlist(linklist L,int i)
{node *p=L;
int j=0;
while(p->next!=NULL&&j<1)
{p=p->next;
    j++;
}
if(j==1)
return p;
else
return NULL;
}
void Inslist(linklist L,int i,int x)
{node *head,*s,*p;
int n,j=0;
p=head;
while(p!=NULL&&j<1)
{j++;
p=p->next;
}
if(p!=NULL)
{s=NULL;
    s->data=x;
    s->next=p->next;
    p->next=s;
    n++;
}
else
printf("\n\t\t线性表为空或插入位置超出! \n");
}
void Dellist(linklist L,int x)
{int n;
node *head,*p,*q;
if(head==NULL)
{printf("\t\t链表下溢! \n");
return;
}
if(head->next==NULL)
{printf("\t\t线性表已经为空!\n");
return;
}
q=head;
p=head->next;
while(p!=NULL&&p->data!=x)
{q=p;
p=p->next;
}
if(p!=NULL)
{q->next=p->next;
free (p);                      /*改成小写的p,我用的是Turboc,所以改成free()*/
n--;
printf("\t\t %c已经被删除!",x);
}
else
printf("\t\t未找到!\n");
}
void main()
{linklist L;
int n=0,i;
int x;
Createlist(L);
Lenlist(L);                 /* 多了一个参数,改成Lenlist(L) */
Searchlist(L,i);
Inslist(L,i,x);
Dellist(L,x);
}

按照上面改后,在Turboc下编译通过,但运行时出错。因为你的函数调用不对,自己看看吧!
2010-04-27 16:44
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:5 
自己在改进下

#include <stdio.h>
#include <stdlib.h>

typedef struct linknode
{
    int data;
    struct linknode *next;
}node, *linklist;

void Createlist(linklist &L)
{
    node *head,*p,*s;
    int n=0,z=1;
    int x;
    //head=malloc(sizeof(node));
    head = (linklist) malloc (sizeof(node));
    head->next = NULL;


    p=head;
    printf("\n\t\t 建立一个线性表");
    printf("\n\t\t 请逐个输入结点,以\"x\"未结束标记!\n");
    while(z)
    {
        printf("\t\t 输入:");
        scanf("%c",&x);
        getchar();
        if(x!='x')
        {
            //s=NULL;
            s = (linklist) malloc (sizeof(node));
            n++;
            s->data=x;
            p->next=s;
            s->next=NULL;
            p=s;
        }
        else
            z=0;
    }
    L = head;
}

int Lenlist(linklist L)
{
    node *p=L;
    int n=0;
    while(p->next)
    {
        p=p->next;
        n++;
    }//.....................................................(34)
    return n;
}

linklist Searchlist(linklist L,int i)//i 表示要查找的第几号元素
{//具有头指针
    node *p=L;
    int j=1;

    while(p->next && j<i)
    {
        p=p->next;
        j++;
    }
    if(!p->next)//表示查询的位置超出
        return NULL;        
    else
        return p->next;
}

void Inslist(linklist L,int i,int x)//i表示要插入的位置
{
    //node *head,*s,*p;
    linklist p, s;
    //int n;
    int j=1;
    //p=head;
    p = L;
    while( p->next && j<i )
    {
        j++;
        p=p->next;
    }
    if(p!=NULL)
    {
        //s=NULL;
        s = (linklist) malloc (sizeof(node));
        s->data=x;
        s->next = p->next;
        p->next=s;
       //n++;
    }
    else
        printf("\n\t\t线性表为空或插入位置超出!\n");//.............................................(14)
}

void Dellist(linklist L,int x)
{
    //int n;
    linklist p, q;
    /*if(head==NULL)
    {
        printf("\t\t链表下溢! \n");
        return;
    }*/
    if(L->next==NULL)
    {
        printf("\t\t线性表为空!\n");
        return;
    }
    q=L;//q 表示被删除结点的前驱
    p=L->next;//指向被删除结点
    while(p!=NULL && p->data!=x)
    {
        q=p;
        p=p->next;
    }
    if(p!=NULL)
    {
        q->next=p->next;
        delete (p);//大小写..................................................................(86)
        //n--;
        printf("\t\t %c已经被删除!\n",x);
    }
    else
        printf("\t\t未找到!\n");
}

void main()
{   
    linklist L;
    int n=0,i;
    int x;
    Createlist(L);
    //Lenlist(L,n);//参数的个数
    n = Lenlist(L);

    printf("请输入要查找的位置:");
    scanf("%d", &i);
    Searchlist(L,i);

    printf("请输入要插入的位置和元素的值:");
    scanf("%d%d", &i, &x);
    Inslist(L,i,x);

    printf("请输入要删除第一次出现在链表中元素的值:");
    scanf("%d", &x);
    Dellist(L,x);
}
2010-04-27 16:45
快速回复:数据结构
数据加载中...
 
   



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

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