c单链表
#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)
#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# 使用winform内嵌浏览器
- 下一篇: PATH环境变量的相关操作