当前位置: 代码迷 >> C语言 >> [求助]单链表改进!
  详细解决方案

[求助]单链表改进!

热度:459   发布时间:2006-05-21 10:39:00.0
[求助]单链表改进!

改进!!
单链表分解:

#include <stdio.h>
#include <malloc.h>
#include <conio.h>

typedef struct node
{
int data;
struct node *next;
}*LinkList, ListNode;

void CreateList(LinkList *head);
void Docompose(LinkList *head1, LinkList *head2);//单链表head1分解
void VisitList(LinkList head);
void DestroyList(LinkList *head);

int main(void)
{
LinkList newhead1 = NULL, newhead2 = NULL;

CreateList(&newhead1);
VisitList(newhead1);

Docompose(&newhead1, &newhead2);

VisitList(newhead1);
VisitList(newhead2);

DestroyList(&newhead1);
DestroyList(&newhead2);

getch();
return 0;
}

void CreateList(LinkList *head)
{
ListNode *p, *q;
int item;

q = NULL;

printf("Enter the item:\n");
scanf("%d", &item);

while (item != 0)
{
p = (LinkList)malloc(sizeof(ListNode));
if (p == NULL)
exit(1);
p -> data = item;

if (*head == NULL)
*head = p;
else
q -> next = p;
q = p;

printf("Enter the item:\n");
scanf("%d", &item);
}
if (q != NULL)
q -> next = NULL;
}

void Docompose(LinkList *head1, LinkList *head2)
{
int item;
ListNode *p, *tail1;
p = *head1;

printf("Enter docompose number:\n");
scanf("%d", &item);

while(p -> data != item)
{
p = p -> next;
}

tail1 = p;
p = p -> next;
tail1 -> next = NULL;

*head2 = p;
}

void VisitList(LinkList head)
{
LinkList p;
p = head;

while (p)
{
printf("%d ", p -> data);
p = p -> next;
}
printf("\n");
}

void DestroyList(LinkList *head)
{
LinkList p;


while (*head != NULL)
{
p = *head;
*head = (*head) -> next;
free(p);
}

}

搜索更多相关的解决方案: 单链  改进  

----------------解决方案--------------------------------------------------------

#include <stdio.h>
#include <malloc.h>
#include <conio.h>

typedef struct node
{
int data;
struct node *next;
}*LinkList, ListNode;

void CreateList(LinkList *head);
void Docompose(LinkList *head1, LinkList *head2);
void VisitList(LinkList head);
void DestroyList(LinkList *head);

int main(void)
{
LinkList newhead1 = NULL, newhead2 = NULL;

CreateList(&newhead1);
VisitList(newhead1);

Docompose(&newhead1, &newhead2);

VisitList(newhead1);
VisitList(newhead2);

DestroyList(&newhead1);
DestroyList(&newhead2);

getch();
return 0;
}

void CreateList(LinkList *head)
{
ListNode *p, *q;
int item;

q = NULL;

printf("Enter the item:\n");
scanf("%d", &item);

while (item != 0)
{
p = (LinkList)malloc(sizeof(ListNode));
if (p == NULL)
exit(1);
p -> data = item;
p->next=NULL;
if (*head == NULL)
*head = p;
else
q -> next = p;
q = p;
/*后面的就不要了
 if (q != NULL)
q -> next = NULL;

*/

printf("Enter the item:\n");
scanf("%d", &item);
}

}

void Docompose(LinkList *head1, LinkList *head2)
{
int item;
ListNode *p, *tail1;
p = *head1;

printf("Enter docompose number:\n");
scanf("%d", &item);
/*
  这个地方帮你改了一下,输入的数应该归属于下一个链
*/

while(p -> data != item)
{
tail1=p;
p = p -> next;
}


tail1 -> next = NULL;

*head2 = p;
}

void VisitList(LinkList head)
{
LinkList p;
p = head;

while (p)
{
printf("%d ", p -> data);
p = p -> next;
}
printf("\n");
}

void DestroyList(LinkList *head)
{
LinkList p;


while (*head != NULL)
{
p = *head;
*head = (*head) -> next;
free(p);
}

}




----------------解决方案--------------------------------------------------------
行了,不用改了,能写这样就行了
----------------解决方案--------------------------------------------------------

夸我呢,还是挖苦我呢?!
晕啦!
----------------解决方案--------------------------------------------------------
ListNode *p, *q;

你都定义了指针类型了你还用这种方式做什么


直接用LinkList p, q就行了,要一直性.

while (p)
{
printf("%d ", p -> data);
p = p -> next;
}

这里不需要在设置P了,直接用实参传过来的指针就行

p = (LinkList)malloc(sizeof(ListNode));
if (p == NULL)
exit(1);
这里直接测试,不用分开

p -> data = item;

这里生成新结点后加个p -> nextPtr = NULL

tail1 = p;
p = p -> next;
tail1 -> next = NULL;

这里你放个tail1做什么,直接用P,先将P赋给headPtr2,然后将P指向NULL

行了,基本上了,我忙去了,有事发信吧



----------------解决方案--------------------------------------------------------

楼主,链表就是明白它的逻辑存储结构,就可以随便写了

多写熟练就行,你发这么多关于链表的没必要,不要产生依赖


----------------解决方案--------------------------------------------------------

谢谢各位!!
我会慢慢改进的!!


----------------解决方案--------------------------------------------------------
  相关解决方案