linklist 链表的实现

#include <stdio.h>
#include <stdlib.h>


typedef struct _link_node_{
int data;
struct _link_node_ *next;
}LinkNode;


LinkNode *creat_linklist(void);
LinkNode *creat_linknode(int value);
int insert_linklist(LinkNode *head, int value);
int show_linklist(LinkNode *head);
//int search_linklist(LinkNode *head, int value);
LinkNode *search_linklist(LinkNode *head, int value);
int modify_linklist(LinkNode *head, int obj, int value);
int delet_linklist(LinkNode *head, int obj);
int free_linklist(LinkNode *head);


int main()
{
LinkNode *head = NULL;
LinkNode *locate = NULL;


head = creat_linklist();


insert_linklist(head, 100);
insert_linklist(head, 200);
locate = search_linklist(head, 1000);
if(locate)
printf("locate: %d\n", locate->data);
modify_linklist(head, 1000, 1000);
show_linklist(head);
delet_linklist(head, 200);
show_linklist(head);
free_linklist(head);
return 0;
}


LinkNode *creat_linknode(int value)
{
LinkNode *node = NULL;


node = (LinkNode *)malloc(sizeof(LinkNode));
node->data = value;
node->next = NULL;


return node;
}


LinkNode *creat_linklist(void)
{
return creat_linknode(0);
}


int insert_linklist(LinkNode *head, int value)
{
LinkNode *node = NULL;


node = creat_linknode(value);
node->next = head->next;
head->next = node;


return 0;
}


int show_linklist(LinkNode *head)
{
LinkNode *p = head->next;


while(NULL != p)
{
printf("%3d ", p->data);
p = p->next;
}
putchar('\n');
return 0;
}


#if 0
int search_linklist(LinkNode *head, int value)
{
int nu = 1;
LinkNode *p = head->next;


while(NULL != p)
{
if(p->data == value)
return nu;
nu ++;
p = p->next;
}
return -1;
}
#else


LinkNode *search_linklist(LinkNode *head, int value)
{
LinkNode *p = head->next;


while(NULL != p)
{
if(p->data == value)
return p;
p = p->next;
}
return NULL;
}
#endif


int modify_linklist(LinkNode *head, int obj, int value)
{
LinkNode *locate = NULL;


locate = search_linklist(head, obj);
if(NULL == locate)
return -1;
locate->data = value;


return 0;
}


int delet_linklist(LinkNode *head, int obj)
{
LinkNode *p = head;
LinkNode *tmp = NULL;


while(NULL != p->next && p->next->data != obj)
p = p->next;


if(NULL == p->next)
return -1;
tmp = p->next;
p->next = tmp->next;
free(tmp);
return 0;
}


int free_linklist(LinkNode *head)
{
LinkNode *tmp = NULL,
*p = head;


while(NULL != p)
{
tmp = p;
p = p->next;
free(tmp);
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C++链表实现进程管理的示例代码: ```cpp #include <iostream> #include <string> #include <algorithm> using namespace std; struct Process { string proname; int time, pri; bool status; }; struct Node { Process val; Node* next; Node(Process x) : val(x), next(NULL) {} }; class Linklist { public: Node* head; Linklist(); void Insert(Process val, int pos); void Remove(Process val); void Print(); int Find(Process val); int Length(); ~Linklist(); private: int length; }; Linklist::Linklist() { head = NULL; length = 0; } void Linklist::Insert(Process val, int pos) { if (pos < 0 || pos > length) { cout << "Invalid position!" << endl; return; } Node* newNode = new Node(val); if (pos == 0) { newNode->next = head; head = newNode; } else { Node* curr = head; for (int i = 0; i < pos - 1; i++) { curr = curr->next; } newNode->next = curr->next; curr->next = newNode; } length++; } void Linklist::Remove(Process val) { Node* curr = head; Node* prev = NULL; while (curr != NULL) { if (curr->val.proname == val.proname) { if (prev == NULL) { head = curr->next; } else { prev->next = curr->next; } delete curr; length--; return; } prev = curr; curr = curr->next; } cout << "Process not found!" << endl; } void Linklist::Print() { Node* curr = head; while (curr != NULL) { cout << "Process Name: " << curr->val.proname << endl; cout << "Priority: " << curr->val.pri << endl; cout << "Running Time: " << curr->val.time << endl; cout << "Status: " << (curr->val.status ? "Running" : "Not Running") << endl; cout << endl; curr = curr->next; } } int Linklist::Find(Process val) { Node* curr = head; int pos = 0; while (curr != NULL) { if (curr->val.proname == val.proname) { return pos; } curr = curr->next; pos++; } return -1; } int Linklist::Length() { return length; } Linklist::~Linklist() { Node* curr = head; Node* next = NULL; while (curr != NULL) { next = curr->next; delete curr; curr = next; } } int main() { Linklist processList; Process p1 = { "Process1", 10, 2, true }; Process p2 = { "Process2", 5, 1, false }; Process p3 = { "Process3", 8, 3, true }; processList.Insert(p1, 0); processList.Insert(p2, 1); processList.Insert(p3, 2); processList.Print(); processList.Remove(p2); processList.Print(); int pos = processList.Find(p3); if (pos != -1) { cout << "Process found at position: " << pos << endl; } else { cout << "Process not found!" << endl; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值