建立正序链表

Monrother 2013-06-07 03:38:40
请按照输入整数的顺序建立一个正序的带表头节点的链表。已知程序的基本结构如下,请你编写 ins_list 函数。
链表问题很不懂。
求看 ins_list 函数有什么问题,别的都是题目已经给出来的代码。
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */

#include "stdio.h"
struct node
{
int data;
struct node * next;
} ;

typedef struct node NODE;
typedef struct node * PNODE;

int main ( )
{
int num=1;
PNODE head;

head = (PNODE)malloc( sizeof(NODE) );
head->next = NULL;
head->data = -1;

while ( num!=0 )
{
scanf("%d", &num);
if ( num!=0 )
ins_list( head, num);
}
outlist( head );
return 0;
}

outlist( PNODE head )
{
PNODE p;
p = head->next;
while ( p != NULL )
{
printf("%d\n", p->data);
p = p->next;
}
}
ins_list( PNODE h, int num )
{
NODE *q,*p;
int n=0;
p->next=NULL;
p->data=num;
for(q=h; q->next!=NULL; n++)
q=q->next;
q->next=p;
}


...全文
2226 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bangbang1234 2013-06-07
  • 打赏
  • 举报
回复
帮你修改了一下
#include "stdio.h"
#include <stdlib.h>
struct node
{
    int data;
    struct node * next;
} ;
 
typedef struct node NODE;
typedef struct node * PNODE;
 

 
int outlist( PNODE head )
{
    PNODE p;
    p = head->next;
    while ( p != NULL )
    {
        printf("%d\n", p->data);
        p = p->next;
    }
	return 1;
}
 void ins_list( PNODE h, int num )
{
	
    NODE  * r,* p;
	r=h;
	while(r->next!=NULL)
	{
		r=r->next;
	}
    p=(PNODE)malloc(sizeof(NODE));
    p->data=num;
	p->next=NULL;//p为待插结点
	if(h->next==NULL)
	{
		
		h->next=p;
		r=p;
	}
	else
	{
		r->next=p;
	    r=r->next;
	}

}
int main ( )
{
    int num=1;
    PNODE head;
 
    head = (PNODE)malloc( sizeof(NODE) );
    head->next = NULL;
    head->data = -1;//建立头结点
 
    while ( num!=0 )
    {
		printf("please input a number");
        scanf("%d", & num);
        if ( num!=0 )
            ins_list( head, num);//插入算法
    }
    outlist( head );
    return 1;
}
赵4老师 2013-06-07
  • 打赏
  • 举报
回复
仅供参考
//假设带表头结点的单向链表头指针为head,试编写一个算法将值为5的结点插入到连接表的第k个结点前,删除第k个节点,并对该链表进行排序。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
    int          data;
    struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t;
int main() {
    srand(time(NULL));

    //填写头节点数据
    H.data=-1;
    H.next=NULL;
    head=&H;

    //创建10个节点的单链表
    p=head;
    for (i=0;i<10;i++) {
        q=(struct NODE *)malloc(sizeof(struct NODE));
        if (NULL==q) return 1;
        q->data=rand()%100;//填写0..99的随机值
        q->next=NULL;
        p->next=q;
        p=q;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将值为5的结点插入到单链表的第k个结点前
    k=3;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=(struct NODE *)malloc(sizeof(struct NODE));
            if (NULL==q) return 1;
            q->data=5;
            q->next=p->next;
            p->next=q;
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //删除第k个节点
    k=5;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=p->next;
            if (q) {
                p->next=q->next;
                free(q);
            }
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //从小到大排序
    for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
        for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
            if (p->next->data > q->next->data) {

                //交换data
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
//              t=p->next->data;p->next->data=q->next->data;q->next->data=t;

                //或者

                //交换next
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
                s1=p->next;
                s2=p->next->next;
                s3=q->next;
                s4=q->next->next;

                if (s2!=s3) {
                     p->next=s3;
                    s3->next=s2;
                     q->next=s1;
                    s1->next=s4;
                } else {
                     p->next=s3;
                    s3->next=s1;
                           q=s3;
                    s1->next=s4;
                }

                //输出整个单链表
//              s=head->next;
//              while (1) {
//                  if (NULL==s) {
//                      printf("\n");
//                      break;
//                  }
//                  printf("%02d->",s->data);
//                  s=s->next;
//              }
//              getchar();
            }
        }
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //释放所有节点
    p=head->next;
    while (1) {
        if (NULL==p) {
            break;
        }
        q=p->next;
        free(p);
        p=q;
    }

    return 0;
}
//29->82->10->22->71->05->13->60->59->39->
//29->82->05->10->22->71->05->13->60->59->39->
//29->82->05->10->71->05->13->60->59->39->
//05->05->10->13->29->39->59->60->71->82->
//
be_yourself_fan 2013-06-07
  • 打赏
  • 举报
回复
单是NODE *p;的话只是定义指针,没有分配内存,
be_yourself_fan 2013-06-07
  • 打赏
  • 举报
回复
改了下
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */

//#include "stdio.h"
#include <stdio.h>
#include <malloc.h>


struct node
{
    int data;
    struct node * next;
} ;

typedef struct node NODE;
typedef struct node * PNODE;

void ins_list( PNODE h, int num );
void outlist( PNODE head );
int main ( )
{
    int num=1;
    PNODE head;

    head = (PNODE)malloc( sizeof(NODE) );
    head->next = NULL;
    head->data = -1;
	
    while ( num!=0 )
    {
        scanf("%d", &num);
        if ( num!=0 )
            ins_list( head, num);
    }
    outlist( head );
    return 0;
}

void outlist( PNODE head )
{
    PNODE p;
    p = head->next;
    while ( p != NULL )
    {
        printf("%d\n", p->data);
        p = p->next;
    }
}
void ins_list( PNODE h, int num )
{
    NODE *q,*p;
	p = (PNODE)malloc( sizeof(NODE) );	//添加
    p->next=NULL;
    p->data=num;
    for(q=h; q->next!=NULL; )
        q=q->next;
    q->next=p;
}


starytx 2013-06-07
  • 打赏
  • 举报
回复
那两个函数都没有返回类型,没有返回类型的,可以写void
Monrother 2013-06-07
  • 打赏
  • 举报
回复
main.c: In function `main': main.c:16: warning: implicit declaration of function `malloc' main.c:23: warning: implicit declaration of function `ins_list' main.c:26: warning: implicit declaration of function `outlist' main.c: At top level: main.c:31: warning: return type defaults to `int' main.c: In function `outlist': main.c:37: warning: control reaches end of non-void function main.c: At top level: main.c:51: warning: return type defaults to `int' main.c: In function `ins_list': main.c:60: warning: control reaches end of non-void function 有一大堆这个warning是什么意思
Monrother 2013-06-07
  • 打赏
  • 举报
回复
靠 下一道题的源代码给出来了。。。白送分了!

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧