将一个带头结点的单链表倒置程序(用指针的方法)

luoryan 2008-12-02 11:47:08
#include<stdio.h>
#include<malloc.h>


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


/********************链表**********************/
NODE *Create(){
NODE *p,*head;
int x;
head=(NODE *)malloc(sizeof(NODE));
head->next=NULL;
printf("Input data,-1 to End!\n");
scanf("%d",&x);
while(x!=-1){
p=(NODE *)malloc(sizeof(NODE));
p->data=x;
p->next=head->next;
head->next=p;
scanf("%d",&x);
}
return(head);
}

/******************************************/
void Output(NODE *head){
NODE *p;
p=head;
printf("Begin to dump the LinkList...\n");
while(p->next!=NULL){
printf("->%d",p->next->data);
p=p->next;
}
printf("\nThe LinkList ended!\n");
}

/*******************倒置函数***********************/
void ds(NODE *head){
NODE *p;
p=head;
printf("Begin to ds the LinkList...\n");
p=p->next;
while(p->next!=NULL){
head=p->next;
p->next=p->next->next;
p->next->next=p;
if(p->next!=NULL)
p=p->next;
}

printf("\nThe dsList ended!\n");
}
/******************************************/
void main(){
NODE *head;
head=Create();
Output(head);
ds(head);
Output(head);


}
以上是用指针的方法将一个带头结点的单链表倒置的程序,
但运行时得不到想要的结果,我认为是倒置函数有问题,
但不会改,请哪位高手帮忙更改一下,谢谢!!
...全文
944 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lann64 2008-12-03
  • 打赏
  • 举报
回复
/****************另一个倒置***************************/
void reverse(NODE * head)
{
NODE *p,*q,*r;
p=NULL;
q=head->next;

while(q)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=p;
}
lann64 2008-12-03
  • 打赏
  • 举报
回复
/*******************倒置函数***********************/
void ds(NODE *head)
{
NODE *p;
p=head;
struct stack s;
init(&s);
printf("Begin to ds the LinkList...\n");
while (p->next!=NULL)
{
push(&s,p->next->data);
p=p->next;
}
p=head;
while (!empty(&s))
{
p->next->data=pop(&s);
p=p->next;
}
printf("\nThe dsList ended!\n");
}
  • 打赏
  • 举报
回复
数据结构书上都有啊,没好好学吧?
aaajj 2008-12-03
  • 打赏
  • 举报
回复
不错
yangkunhenry 2008-12-03
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h>
typedef struct node{
int data;
struct node *next;
} NODE;
//********************链表**********************/
NODE *Create(){
NODE *p,*head;
int x;
head=(NODE *)malloc(sizeof(NODE));
head->next=NULL;
printf("Input data,-1 to End!\n");
scanf("%d",&x);
while(x!=-1){
p=(NODE *)malloc(sizeof(NODE));
p->data=x;
p->next=head->next;
head->next=p;
scanf("%d",&x);
}
return(head);
}

/******************************************/
void Output(NODE *head){
NODE *p;
p=head;
printf("Begin to dump the LinkList...\n");
while(p->next!=NULL){
printf("->%d",p->next->data);
p=p->next;
}
printf("\nThe LinkList ended!\n");
}

/*******************倒置函数***********************/
NODE* ds(NODE *head){
NODE *p,*p1,*p2=NULL;
p=head;
printf("Begin to ds the LinkList...\n");
p=p->next;
while(p!=NULL)
{
p1=p;
p=p->next;
p1->next=p2;
p2=p1;
}
head->next=p2;
printf("\nThe dsList ended!\n");
return head;
}
/******************************************/
void main(){
NODE *head;
head=Create();
Output(head);
ds(head);
Output(head);
}
你要的结果:
Input data,-1 to End!
2 5 9 1 3 -1
Begin to dump the LinkList...
->3->1->9->5->2
The LinkList ended!
Begin to ds the LinkList...

The dsList ended!
Begin to dump the LinkList...
->2->5->9->1->3
The LinkList ended!
Press any key to continue . . .
BaihowFF 2008-12-03
  • 打赏
  • 举报
回复

pNode Reverse(pNode head){
// 防止传入为空链表
if ( head == NULL) return NULL;

// 预处理
pNode p=new Node;
p = head->next;
head->next = NULL;

// 交换前后..用p表示未交换头..head反转后存储
while (p != NULL)
{
swap(p->next, head);
swap(head, p);
}

delete p;
p=NULL;

// 实现链式操作
return head;
}
RobotPassport 2008-12-03
  • 打赏
  • 举报
回复

//Invert function
void Invert(NODE *head)
{
assert(head != NULL && head->next != NULL);

NODE *ptr=head->next->next;
head->next->next=NULL;
NODE *p1=head->next;
/* while(ptr != NULL) // /*......*/跟程序下面的while(ptr!=NULL){..}等价.
{
Lnode *tmp=ptr->next;

ptr->next=head->next;
head->next=ptr;

ptr=tmp;
}
*/
while(ptr!=NULL)
{
NODE *p2=ptr->next;
ptr->next=head->next;
head->next=ptr;
ptr=p2;

}

}
xiangxuf 2008-12-03
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<malloc.h>
#include<assert.h>
typedef struct node{
int data;
struct node *next;
}NODE;

//Initiate the string.

NODE *Create()
{
NODE *p, *head;
int x;
head = (NODE*)malloc(sizeof(NODE));
head->next = NULL;
printf("Input data, -1 to End !\n");
scanf("%d",&x);
while(x != -1)
{
p = (NODE *)malloc(sizeof(NODE));
p->data = x;
p->next = head->next;
head->next = p;
getchar();
scanf("%d", &x);
}

return(head);
}

void Output(NODE *head)
{
NODE *p;
p = head;
printf("Begin to dump the LinkList..\n");
p = p->next;
while(p != NULL)
{
printf("%d",p->data);
p = p->next;
}
printf("\nThe LinkList ended! \n");

}

//Invert function
void Invert(NODE *head)
{
assert(head != NULL && head->next != NULL);

NODE *ptr=head->next->next;
head->next->next=NULL;
NODE *p1=head->next;
/* while(ptr != NULL)
{
Lnode *tmp=ptr->next;

ptr->next=head->next;
head->next=ptr;

ptr=tmp;
}
*/
while(ptr!=NULL)
{
NODE *p2=ptr->next;
ptr->next=head->next;
head->next=ptr;
ptr=p2;

}

}


void main()
{
NODE *head;
head = Create();
Output(head);
Invert(head);
Output(head);

}

以前作的 , 楼主可以参考下

70,039

社区成员

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

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