åè代ç å¦ä¸ï¼ï¼ææä¸æï¼æé纳ï¼ï¼
/*
å建线æ§é¡ºåºè¡¨å
¶æ大é¿åº¦ä¸º20 ï¼
å¨é¡ºåºè¡¨ä¸èç¹çå¼ä¸º{1ï¼4ï¼6ï¼7ï¼10}ï¼
ç¶åå¨ä¸æ 为2çä½ç½®æå
¥5æå°è¾åºï¼
ç¶åå é¤å¼ä¸º7çå
ç´ å¹¶æå°è¾åºã
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node
{
int a;
struct node *next;
} NODE;
// å建顺åºé¾è¡¨ï¼é¿åº¦listSizeï¼å¹¶åå§å
NODE *createlist(NODE *head, int listSize, int arr[]);
// æå
¥ä¸ä¸ªèç¹
int insertnode(NODE *head, int index, int val);
// å é¤ä¸ä¸ªèç¹
int delnode(NODE *head, int val);
// æå°é¾è¡¨ä¿¡æ¯
void printlist(NODE *head);
// 主å½æ°
int main(int argc, char const *argv[])
{
int arr[5] = {1, 4, 6, 7, 10};
int arrlen;
arrlen = (int)sizeof(arr) / (int)sizeof(int);
NODE *head = NULL;
head = createlist(head, arrlen, arr);
if (NULL == head)
{
printf("Failed to create list . Program Eixt . \n");
return 0;
}
printf("\n");
printlist(head);
if (1 == insertnode(head, 2, 5))
{
printf("\nInserted a node before index 2,\n");
printlist(head);
}
else
{
printf("\nFailed to Insert a node .\n");
}
if (1 == delnode(head, 7))
{
printf("\nDeleted a node with value of 7,\n");
printlist(head);
}
else
{
printf("\nNo node deleted : value is not exist.\n");
}
return 0;
}
// å建顺åºé¾è¡¨ï¼é¿åº¦listSizeï¼å¹¶åå§å
NODE *createlist(NODE *head, int listSize, int arr[])
{
head = (NODE *)malloc(sizeof(NODE));
if (NULL == head)
{
printf("Oops ! Malloc Failed ! Program Exit !\n");
return NULL;
}
NODE *p, *q;
p = head;
for (int i = 0; i < listSize; i++)
{
q = (NODE *)malloc(sizeof(NODE));
if (NULL == q)
{
printf("Oops ! Malloc Failed ! Program Exit !\n");
return NULL;
}
q->next = NULL;
if (i < 5)
{
q->a = arr[i];
}
p->next = q;
p = q;
q = q->next;
}
return head;
}
// æå
¥ä¸ä¸ªèç¹
int insertnode(NODE *head, int index, int val)
{
NODE *p = head;
NODE *q = NULL;
int i, count = 0;
while (NULL != p->next)
{
count++;
p = p->next;
}
if (index > count || index < 0)
{
printf("Length of list is %d , please enter proper index to add node. [0,%d]\n", count, count);
printf("Nothing has been added.\n");
return 0;
}
p = head->next;
q = head;
for (i = 0; i <= index; i++)
{
if (index == i )
{
NODE *n = (NODE *)malloc(sizeof(NODE));
if (NULL == n)
{
printf("Oops ! Malloc Failed ! Program Exit !\n");
return 0;
}
n->a = val;
n->next = p;
q->next = n;
return 1;
}
p = p->next;
q = q->next;
}
return 0;
}
// å é¤ä¸ä¸ªèç¹
int delnode(NODE *head, int val)
{
NODE *p, *q;
p = head->next;
q = head;
while (p != NULL)
{
if (val == p->a)
{
p = p->next;
q->next = p;
return 1;
}
else
{
p = p->next;
q = q->next;
}
}
return 0;
}
// æå°é¾è¡¨ä¿¡æ¯
void printlist(NODE *head)
{
NODE *p;
p = head->next;
int i = 0;
if (NULL != p)
{
printf("Print List info as below:\n");
while (NULL != p)
{
printf("Node at Index %d , value is %d .\n", i, p->a);
i++;
p = p->next;
}
}
else
{
printf("List is empty.\n");
}
}
Linuxç¯å¢è¿è¡æªå¾ï¼
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/b151f8198618367a17f478d520738bd4b31ce5bd?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
以ä¸ï¼å¦æ帮å©ï¼ç¦è¯·ç¹é纳ï¼è°¢è°¢ï¼