日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

c单链表

發(fā)布時(shí)間:2023/12/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c单链表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <stdio.h>
#include <stdlib.h> #define T 1
#define F -1 typedef int Type; struct Node
{
??? Type value;
??? struct Node *next;
}; int init(struct Node **head); ? ? ? ? ?? //初始化
int insert_head(struct Node *head, Type value); ? ? ? ? ? ? // 頭插法
int insert_tail(struct Node *head, Type value); ? ? ? ? ? ? ?? // 尾插法
int insert(struct Node *head, Type index, Type x); ? ? ? ?? // 在中間插入
int delete(struct Node *head, Type index); ? ? ? ? ? ? ? ? ? ?? // 按位刪除
int delete_value(struct Node *head, Type x); ? ? ? ? ? ? ? ?? // 按值刪除
int change_index(struct Node *head, Type index, Type x); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? //按位改變
int change_value(struct Node *head, Type old_value, Type new_value); ? ? ? ? ? ?? //按值改變
void search_index(struct Node *head, Type index); ? ? ? // 按位查值
void search_value(struct Node *head, Type value); ? ? ? //按值查位
int length(struct Node *head); ? ? ? ? ? ? ? // 輸出長(zhǎng)度
int print(struct Node *head); ? ? ? ? ? ? ? ?? // 輸出函數(shù)
int main()
{???
??? int i;
??? int ret;
??? struct Node *head;
??? ret = init(&head);
????
??? for (i = 0; i <= 5; i++)
??? {
??????? insert_head(head, i);????
??? }
??? print(head); for (i = 0; i <= 5; i++)
??? {
??????? insert_tail(head, i);????
??? }
??? print(head); printf("%d\n", length(head));
??? delete(head,2);
??? delete(head,length(head) - 1);
??? delete(head,0);
??? print(head); insert(head, 3,99);
??? insert(head, 0,99);
??? insert(head, length(head),99);
??? print(head);
?
??? delete_value(head, 0);
??? print(head); change_index(head, 4, 101);
??? print(head);?? change_value(head, 99, 100);
??? print(head);
?
??? search_index(head, 7);
????
??? search_value(head, 100); return 0;
} int init(struct Node **head)
{
??? struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
??? if (NULL == newnode)
??? {
??????? return F;
??? }
??? newnode->value = 0;
??? newnode->next = NULL;
??? (*head) = newnode;
??? return T;
} int insert_head(struct Node *head, Type value)
{
??? struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
??? if (NULL == newnode)
??? {
??????? return F;
??? }
????
??? newnode->value = value;
??? newnode->next = head->next;
??? head->next = newnode;
??
??? return T;
} int insert(struct Node *head, Type index, Type x)
{
??? if (index < 0 || index > length(head))
??? {
??????? printf("out of range\n");
??????? return F;
??? }
??? int i;
??? for (i = 0; i < index; i++)
??? {
??????? head = head->next;
??? }
????
??? struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
??? if (NULL == newnode)
??? {
??????? return F;
??? }
????
??? newnode->value = x;
??? newnode->next = head->next;
??? head->next = newnode;
??
??? return T;
}?? int insert_tail(struct Node *head, Type value)
{
??? struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
??? if (NULL == newnode)
??? {
??????? return F;
??? }
????
??? newnode->value = value;
??? newnode->next = NULL;
????
??? while (NULL != head->next)
??? {
??????? head = head->next;
??? }
????
??? head->next = newnode; return T;
} int delete(struct Node *head, Type index)
{
??? int i;
??? struct Node *temp;
??? if (index < 0 || index >= length(head))
??? {
??????? printf("out of range\n");
??????? return F;
??? }
??? for (i = 0; i < index; i++)
??? {
??????? head = head->next;
??? }
??? temp = head->next->next;
??? free(head->next);
??? head->next = temp;
????
??? return T;
} int delete_value(struct Node *head, Type x)
{
??? int i;
??? struct Node *temp;
??? while (NULL != head->next)
??? {
??????? if(head->next->value == x)
??????? {
??????????? temp = head->next->next;
??????????? free(head->next);
??????????? head->next = temp;
??????? }
??????? else
??????? {
??????????? head = head->next;
??????? }
??? }
} int change_index(struct Node *head, Type index, Type x)
{
??? int i;
??? if (index < 0 || index >= length(head))
??? {?
??????? printf("out of range\n");
??????? return F;
??? }
??? for (i = 0; i <= index; i++)
??? {
??????? head = head->next;
??? }
??? head->value = x;
????
??? return T;
} int change_value(struct Node *head, Type old_value, Type new_value)
{
??? int count = 0;
??? while (head->next != NULL)
??? {
??????? if(head->next->value == old_value)
??????? {
??????????? count = 1;
??????????? head->next->value = new_value;
??????? }
??????? head = head->next;
??? }
??? if (count == 0)
??? {
??????? printf("not find\n");
??? }
????
??? return T;
}
????????????
void search_index(struct Node *head, Type index)
{
??? int i;
??? if (index < 0 || index >= length(head))
??? {
??????? printf("out of range\n");
??? }
??? for (i = 0; i <= index; i++)
??? {
??????? head = head->next;
??? }
??? printf("%d\n",head->value);
????
}
????
void search_value(struct Node *head, Type value)
{
??? int count = 0;
??? while (head->next != NULL)
??? {?
??????? if (head->next->value == value)
??????? {
??????????? printf("%d? ",count);
??????? }
??????? head = head->next;
??????? count++;
??? }
??? printf("\n");
}
?
int length(struct Node *head)
{
??? int count = 0;
??? while (NULL != head->next)
??? {
??????? count++;
??????? head = head->next;
??? }
????
??? return count;
}
??? int print(struct Node *head)
{
??? while (head->next != NULL)
??? {
??????? printf("%d? ", head->next->value);
??????? head = head->next;
??? }
??? printf("\n");
}
程序運(yùn)行結(jié)果如下:
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的c单链表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。