分類: c基礎
2004-10-29 20:17
9500人閱讀收藏
舉報 單鏈表很全的例子,增加,刪除,排序,都有了
view plain
copy to clipboard
print?
#include
#include
typedef
struct node
node;
//鍊錶輸出
void output(node *head)
printf("\r\n");
} //鍊錶建立
node* creat()
head->pstnext = null;
p = head;
while(cycle)
s->ndate = date;
p->pstnext = s;
p = s;
} else
} p->pstnext = null;
return(head);
} //單鏈表測長
void length(node *head)
printf("%d\r\n", j);
} //鍊錶按值查詢
void research_date(node *head, int date)
if(null == p)
else
if(date == p->ndate)
return;
} //按序號查詢
void research_number(node *head, int num)
if(p == null)
else
if(i == 0)
else
if(i == num)
} //在指定元素之前插入新結點
void insert_1(node *head, int i, int newdate)
if(null == pre || j > i-1)
else
new->ndate = newdate;
new->pstnext = pre->pstnext;
pre->pstnext = new;
} }
//在指定元素之後插入新結點
void insert_2(node *head, int i, int newdate)
if(j == i)
new->ndate = newdate;
new->pstnext = pre->pstnext;
pre->pstnext = new;
}else
} //刪除指定結點
void delete_1(node *head, int i3)
if(null == p)
else
} //指定刪除單鏈表中某個資料,並統計刪除此資料的個數
int delete_2(node *head, int delete_date)
else
} return count;
} //鍊錶逆置
void reverse_list(node *head)
q = head->pstnext->pstnext;
head->pstnext->pstnext = null;
while(null != q)
} //單鏈表的連線
void connect_list(node *head, node *head_new)
p->pstnext = head_new->pstnext;
} //單鏈表銷毀
void destroy_list(node* head)
} main()
下面是輸出結果:
vs2010下除錯結果
單鏈表的插入,查詢,刪除
鍊錶是一種重要的資料結構,相比於陣列,陣列更像是乙個順序表,陣列只要第乙個元素固定,那麼在他後面的元素的位址一定固定,陣列在記憶體中是一塊連續的儲存區域,我們可以根據下標找到他的每個元素,這是陣列和鍊錶的乙個區別 鍊錶,見名思意,乙個鍊子連線起來的表,元素之間的聯絡靠的是這個鍊子,這也決定了鍊錶中的...
3單鏈表查詢插入刪除
include include define size sizeof struct linklist struct linklist int main void 頭節點建立成功 printf please inpput n n while 1 i n while i scanf s d p1 x 輸...
單鏈表的插入刪除
include using namespace std struct lnode void creat link lnode head head指標的引用,lnode head 傳遞的是指標,但是對於指標的原值卻發生了copy,這樣你雖然可以對指標指向的記憶體進行修改但是不能對指標進行修改。因此要傳...