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

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);


}
以上是用指针的方法将一个带头结点的单链表倒置的程序,
但运行时得不到想要的结果,我认为是倒置函数有问题,
但不会改,请哪位高手帮忙更改一下,谢谢!!
...全文
891 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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);

}

以前作的 , 楼主可以参考下
/*头结点头文件 hlinklist.h*/ #include typedef int datatype; typedef struct link_node { datatype data; struct link_node *next; }node; /*初始化链表*/ node *init() { node *head; head=(node *)malloc(sizeof(node)); head->next=0; return head; } /*尾插法创建一个头结点链表*/ node *creat(node *head) { node *r,*s; int x; r=head; printf("在新链表中输入数据以0结束:"); scanf("%d",&x); while(x) { s=(node*)malloc(sizeof(node)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=0; return head; } /*打印链表的结点值*/ void print(node *head) { node *p; p=head->next; if(!p) printf("链表内容为空!"); else while(p) { printf("%5d",p->data); p=p->next; } printf("\n"); } /*在单链表中查找第i个结点的地址*/ node *find(node *head,int i) { node *p=head; int j=0; if(i<0) {printf("不存在!");return 0;} if(i==0) return head; while(p&&i!=j) { p=p->next; j++; } return p; } /*在头结点单链表第i个位置后插入一个数*/ node *insert(node *head,int i,datatype x) { node *p,*q; q=find(head,i); if(!q) { printf("插入的位置不存在!\n");return head;} else { p=(node *)malloc(sizeof(node)); p->data=x; p->next=q->next; q->next=p; } return head; } /*在头结点单链表中删除一个为x的值*/ node *dele(node *head,datatype x) { node *pre=head,*p; p=head; while(p&&p->data!=x) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } return head; } /*把头结点单链表倒置(以结点形式 )*/ node *Dao_zhi(node *head) { node *p,*s; p=head->next; head->next=NULL; while(p) { s=p; p=p->next; s->next=head->next; head->next=s; } return head; } /*删除链表中重复的结点 */ node *dele1(node *head) { node *pre,*p,*q; if(head->next==0||!head->next->next) { printf("链表为空!"); return head; } //pre=head->next; q=head->next; while(q) { pre=q; p=q->next; while(p) { while(p&&q->data!=p->data) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } p=pre->next; } q=q->next; } return head; }

70,020

社区成员

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

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