1個小時學會單鏈表,C語言數據結構專題之單鏈表的課程代碼

課程代碼資料

1個小時學會單鏈表,C語言數據結構專題之單鏈表的課程代碼

1個小時學會單鏈表,C語言數據結構專題之單鏈表的課程代碼

#include <stdio.h>
#include <stdlib.h>
struct Node{
int data; //數據域
struct Node* next; //指針域
};
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//headNode 成為了結構體變量
//變量使用前必須被初始化
//headNode->data = 1;
headNode->next = NULL;
return headNode;
}

//創建結點
struct Node* createNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
while (pMove)
{
printf("%d\t", pMove->data);
pMove = pMove->next;
}
printf("\n");
}
//插入結點,參數:插入那個鏈表,插入結點的數據是多少
void insertNodeByHead(struct Node* headNode, int data)
{
//1創建插入的結點
struct Node* newNode = createNode(data);

newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteNodeByAppoin(struct Node* headNode, int posData)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
printf("無法刪除鏈表為空\n");
else
{
while (posNode->data != posData)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("沒有找到相關信息,無法刪除\n");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main()
{
struct Node* list = createList();
insertNodeByHead(list, 1);
insertNodeByHead(list, 2);
insertNodeByHead(list, 3);
printList(list);
deleteNodeByAppoin(list, 2);
printList(list);
system("pause");
return 0;
}
/<stdlib.h>/<stdio.h>

簡單實用:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
int num;

int math;
};
struct Node{
struct student data; //數據域
struct Node* next; //指針域
};
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//headNode 成為了結構體變量
//變量使用前必須被初始化
//headNode->data = 1;
headNode->next = NULL;
return headNode;
}
//創建結點
struct Node* createNode(struct student data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
printf("name\tnum\tmath\n");
while (pMove)
{
printf("%s\t%d\t%d\n", pMove->data.name, pMove->data.num, pMove->data.math);
pMove = pMove->next;
}
printf("\n");
}
//插入結點,參數:插入那個鏈表,插入結點的數據是多少
void insertNodeByHead(struct Node* headNode, struct student data)
{
//1創建插入的結點
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteNodeByAppoinNum(struct Node* headNode, int num)
{

struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
printf("無法刪除鏈表為空\n");
else
{
while (posNode->data.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("沒有找到相關信息,無法刪除\n");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main()
{
struct Node* list = createList();
struct student info;
while (1)
{
printf("請輸入學生的姓名 學號 數學成績:");
setbuf(stdin, NULL);
scanf("%s%d%d", info.name, &info.num, &info.math);
insertNodeByHead(list, info);
printf("continue(Y/N)?\n");
setbuf(stdin, NULL);
int choice = getchar();
if (choice == 'N' || choice == 'n')
{
break;
}
}
printList(list);
printf("請輸入要刪除的學生的學號:");
scanf("%d", &info.num);
deleteNodeByAppoinNum(list, info.num);
printList(list);
system("pause");
return 0;
}

/<stdlib.h>/<stdio.h>

相關視頻敬請關注,私信"視頻"可得。


分享到:


相關文章: