#include <stdio.h>
#include <stdlib.h>
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
struct link * DeleteNode(struct link *head,int data);
struct link *InsertNode(struct link *head, int nodeData);
int main()
{
char c;
int data = 0;
struct link *head = NULL; /* 鏈表頭指標 */
while (1)
{
scanf("%d",&data);
if (data=https://bbs.csdn.net/topics/=-1)
break;
head = AppendNode(head,data);/* 向head為頭指標的鏈表末尾添加節點 */
}
DisplyNode(head); /* 顯示當前鏈表中的各節點資訊 */
scanf("%d",&data);
head = InsertNode(head,data);
DisplyNode(head); /* 顯示當前鏈表中的各節點資訊 */
DeleteMemory(head); /* 釋放所有動態分配的記憶體 */
return 0;
}
struct link *AppendNode(struct link *head,int data)
{
struct link *p=(struct link *)malloc(sizeof(struct link));
p->data=https://bbs.csdn.net/topics/data;
p->next= NULL;
struct link *r=head;
if(r)
{
while(r->next)
r=r->next;
r->next=p;
}
else
head=p;
return head;
}
void DisplyNode(struct link *head)
{
struct link *p;
p=head;
if(p)
{
printf("%d",p->data);
p=p->next;
}
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
printf("\n");
}
struct link * DeleteNode(struct link *head,int data)
{
struct link *p,*q,*r;
p=q=head;
while(p!=NULL)
{
if(p->data=https://bbs.csdn.net/topics/=data)
{
if(p==head)
{
head=p->next;
r=p;
p=q=head;
}
else
{
q->next=p->next;
r=p;
p=q->next;
}
r->next=NULL;
free(r);
}
else
{
q=p;
p=p->next;
}
}
return head;
}
void DeleteMemory(struct link *head)
{
struct link *p,*q;
for(p=head;p;p=q)
{
q=p->next;
free(p);
}
}
struct link *InsertNode(struct link *head, int data)
{
struct link *p,*q;
int i=0,t;
q=head;
p=(struct link *)malloc(sizeof(struct link));
p->data=https://bbs.csdn.net/topics/data;
p->next=NULL;
while(q)
{
if(p->data=https://bbs.csdn.net/topics/=0)
{
p->next=q->next;
q->next=p;
t=p->data;
p->data=https://bbs.csdn.net/topics/q->data;
q->data=https://bbs.csdn.net/topics/t;
i++;
return head;
}
if(p->data <= q->data)
{
p->next=q->next;
q->next=p;
t=p->data;
p->data=https://bbs.csdn.net/topics/q->data;
q->data=https://bbs.csdn.net/topics/t;
i++;
break;
}
else
{
q=q->next;
}
}
if(i==0)
{
while(q->next)
q=q->next;
q->next=p;
p->next=NULL;
}
return head;
}
經過除錯問題就出在最后一個函式上
并且是while陳述句出錯了
但我怎么想都想不到是怎么回事
uj5u.com熱心網友回復:
最后一個函式是在鏈表中添加一個新節點uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
struct link * DeleteNode(struct link *head,int data);
struct link *InsertNode(struct link *head, int nodeData);
int main()
{
char c;
int data = 0;
struct link *head = NULL; /* 鏈表頭指標 */
while (1)
{
scanf("%d",&data);
if (data=https://bbs.csdn.net/topics/=-1)
break;
head = AppendNode(head,data);/* 向head為頭指標的鏈表末尾添加節點 */
}
DisplyNode(head); /* 顯示當前鏈表中的各節點資訊 */
scanf("%d",&data);
head = InsertNode(head,data);
DisplyNode(head); /* 顯示當前鏈表中的各節點資訊 */
DeleteMemory(head); /* 釋放所有動態分配的記憶體 */
return 0;
}
struct link *AppendNode(struct link *head,int data)
{
struct link *p=(struct link *)malloc(sizeof(struct link));
p->data=https://bbs.csdn.net/topics/data;
p->next= NULL;
struct link *r=head;
#if 0 //頭重腳輕,改成如下形式
if(r)
{
while(r->next)
r=r->next;
r->next=p;
}
else
head=p;
#else
if (!r) {
head = p;
return head;
}
while (r->next)
r = r->next;
r->next = p;
#endif
return head;
}
void DisplyNode(struct link *head)
{
struct link *p;
p=head;
while (p) {
if (p == head) {
printf("%d",p->data);
p = p->next;
continue;
}
printf("->%d",p->data);
p=p->next;
}
/*
if(p)
{
printf("%d",p->data);
p = p->next;
}
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
*/
printf("\n");
}
struct link * DeleteNode(struct link *head,int data)
{
struct link *p,*q,*r;
p=q=head;
while(p!=NULL)
{
if(p->data=https://bbs.csdn.net/topics/=data)
{
if(p==head)
{
head=p->next;
r=p;
p=q=head;
}
else
{
q->next=p->next;
r=p;
p=q->next;
}
r->next=NULL;
free(r);
}
else
{
q=p;
p=p->next;
}
}
return head;
}
void DeleteMemory(struct link *head)
{
struct link *p,*q;
for(p=head;p;p=q)
{
q=p->next;
free(p);
}
}
struct link *InsertNode(struct link *head, int data)
{
struct link *p,*q;
int i=0,t;
q=head;
p=(struct link *)malloc(sizeof(struct link));
p->data=https://bbs.csdn.net/topics/data;
p->next=NULL;
while(q)
{
if(p->data=https://bbs.csdn.net/topics/=0)
{
p->next=q->next;
q->next=p;
t=p->data;
p->data=https://bbs.csdn.net/topics/q->data;
q->data=https://bbs.csdn.net/topics/t;
i++;
return head;
}
if(p->data <= q->data)
{
p->next=q->next;
q->next=p;
t=p->data;
p->data=https://bbs.csdn.net/topics/q->data;
q->data=https://bbs.csdn.net/topics/t;
i++;
break;
}
else
{
q=q->next;
}
}
//if(i==0)
if(q==NULL)
{
q = head;
if (q == NULL) {
head = p;
return head;
}
while(q->next)
q=q->next;
q->next=p;
p->next=NULL;
}
return head;
}
供參考~
InsertNode函式有問題,當要插入的data是鏈表中最大的時候,需要注意的是q需要初始化一下,否則在上面的while回圈的時候q已經是NULL了,
另外,head也可能是NULL。因此需要特殊處理~
建議用上面的代碼測驗多種情況,比如中間插入,頭插,尾插都測驗一下
其他問題代碼里都做了修改~
uj5u.com熱心網友回復:
資料結構對單鏈表進行資料排序 http://bbs.csdn.net/topics/392201633轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/256023.html
標籤:C語言
