求助 C语言的单向链表
2008-10-10 18:14
411 查看
编写一个程序将一个头结点指针为pa的单链表A分解成两个单链表A和B,其头结点指针分别为pa和pb,使得A链表中含有原链表A中序号为奇数的元素,而链表B中含有原链表A中序号为偶数的元素,且保持原来的相对顺序。
我的程序:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct node * list(struct node *head);
void display(struct node *head);
void main()
{
struct node * head=NULL,* p1,*p2,*pb;
//int data;
printf("建立单向链表/n");
head=list(head);
display(head);
p1=head;
p2=p1->next;
pb=p2;
while(p1!=NULL)
{
p1->next=p2->next;
p1=p1->next;
p2->next=p1->next;
p2=p2->next;
}
printf("输出单向链表a/n");
display(head);
printf("输出单向链表b/n");
display(pb);
}
struct node * list(struct node *head)//链表输入//
{
int n;
struct node * p,* p1;
printf("读入结点数据(以0结束)");
scanf("%d",&n);
p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
p1=p;
head=p;
scanf("%d",&n);
while(n!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
p1->next=p;
p1=p1->next;
scanf("%d",&n);
}
return head;
}
void display(struct node * head)//链表输出//
{
struct node *p;
p=head;
while(p!=NULL)
{
printf("[%d]-->",p->data);
p=p->next;
}
printf("NULL/n");
}
在执行主函数的while循环时出错,希望高手指点。
谢谢
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
我的程序:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct node * list(struct node *head);
void display(struct node *head);
void main()
{
struct node * head=NULL,* p1,*p2,*pb;
//int data;
printf("建立单向链表/n");
head=list(head);
display(head);
p1=head;
p2=p1->next;
pb=p2;
while(p1!=NULL)
{
p1->next=p2->next;
p1=p1->next;
p2->next=p1->next;
p2=p2->next;
}
printf("输出单向链表a/n");
display(head);
printf("输出单向链表b/n");
display(pb);
}
struct node * list(struct node *head)//链表输入//
{
int n;
struct node * p,* p1;
printf("读入结点数据(以0结束)");
scanf("%d",&n);
p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
p1=p;
head=p;
scanf("%d",&n);
while(n!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
p1->next=p;
p1=p1->next;
scanf("%d",&n);
}
return head;
}
void display(struct node * head)//链表输出//
{
struct node *p;
p=head;
while(p!=NULL)
{
printf("[%d]-->",p->data);
p=p->next;
}
printf("NULL/n");
}
在执行主函数的while循环时出错,希望高手指点。
谢谢
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
相关文章推荐
- c语言:单向循环链表的实现
- C语言实现简单的单向链表
- 单向链表基本操作(C语言实现)
- 单向链表的实现(c语言)
- C语言实现单向链表
- C语言 数据结构与算法 单向链表
- C语言实现单向链表
- C语言单向链表的建立
- C语言单向循环链表 源码
- 【C语言】单向循环链表
- 求助 向升序单向链表中插入一个节点 求教程序中哪里错了?
- 单向链表及其应用(C语言)
- 尾插法建立单向链表 C语言实现
- C语言之字符单向链表
- C语言数据结构之单向链表(已经调试可以实现相应的功能了,可是还是有几个问题现在还是不大理解,希望大家能够一起探讨)
- iOS开发之C语言单向链表的建立
- C语言写的简单的单向链表
- C语言实现链表之单向链表(九)在任意位置插入结点
- C语言单向链表
- C语言创建单向链表之逆向建链