[問題] 請問雙向鏈結的排序
請問有強者可以幫我改個程式嗎??
問題在於函式dlink createdlist2(dlink head,int value)
內容的部分該怎麼改,我才可以在主程式輸入Data的時候
鏈結自動會找到適當的位置
這樣只我走鏈結的時候直接顯示大到小或小到大???
#include <stdio.h>
#include <stdlib.h>
struct dlist /* 雙向串列結構宣告 */
{
int data; /* 節點資料 */
struct dlist *front; /* 指向下一節點的指標 */
struct dlist *back; /* 指向前一節點的指標 */
};
typedef struct dlist dnode; /* 雙向串列新型態 */
typedef dnode *dlink; /* 雙向串列指標新型態 */
/* 使用陣列值建立雙向鏈結串列 */
/* 雙向鏈結串列的顯示 */
dlink createdlist2(dlink head,int value)
{
dlink new_node;
dlink current;
dlink prev;
prev=head;
if(head==NULL)
{
new_node = ( dlink ) malloc(sizeof(dnode));
new_node->data=value;
new_node->front=new_node;
new_node->back=new_node;
}
else
{
new_node = ( dlink ) malloc(sizeof(dnode));
new_node->data=value;
current=head->front;
while((current!=head) && (current->data>= new_node->data))
{
prev=current;
current=current->front;
}
new_node->front=current;
new_node->back=prev;
prev->front=new_node;
current->back=new_node;
}
return new_node;
}
int main(int argc, char **argv)
{
dlink head=NULL; /* 雙向鏈結串列指標 */
dlink now = NULL;
dlink pre=NULL;
dlink temp=NULL;
dlink now1=NULL;
/* 目前節點指標 */
//int list[10] = {75,98,77,10,57,50,40,81,91,99}; /* 陣列內容 */
//int select; /* 選擇項1,2或3 */
//head = createdlist(list,10); /* 建立雙向鏈結串列 */
head=createdlist2(head,50);
now1 = head;
head=createdlist2(head,60);
head=createdlist2(head,40);
head=createdlist2(head,30);
head=createdlist2(head,90);
if ( head == NULL )
{
printf("記憶體配置失敗! \n");
return 0 ;
}
while(now!=NULL)
{
printf("(%d)",now->data);
now=now->front;
}
return 0
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.54.135
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1530183619.A.517.html
→
06/28 20:02,
7年前
, 1F
06/28 20:02, 1F
→
06/28 20:03,
7年前
, 2F
06/28 20:03, 2F
推
06/29 00:31,
7年前
, 3F
06/29 00:31, 3F
→
06/29 00:31,
7年前
, 4F
06/29 00:31, 4F
→
06/29 00:31,
7年前
, 5F
06/29 00:31, 5F
→
06/29 00:31,
7年前
, 6F
06/29 00:31, 6F