c語言——連結串列的基本操作
typedef int ElementType;
typedef struct ListNode{
int k;
ElementType element;
struct ListNode *m_pNext;
}ListNode,*PNODE;
//在k處插入元素
ListNode *FindKthToTail(ListNode *pListHead, unsigned int k){
if (pListHead == NULL)
{
return NULL;
}
ListNode *pAhead = pListHead;
ListNode *pBehind = NULL;
for (int i = 0; i < k - 1; i++)
{
pAhead = pAhead->m_pNext;
}
pBehind = pListHead;
while (pAhead->m_pNext != NULL)
{
pAhead = pAhead->m_pNext;
pBehind = pBehind->m_pNext;
}
return pBehind;
}
//建立連結串列
PNODE CreateList(int lens){
int data;
PNODE pHead = (PNODE)malloc(sizeof(ListNode));
if (pHead == NULL)
{
printf("分配空間失敗請重新分配\n");
exit(-1);
}
PNODE pTail = pHead;
pTail->m_pNext = NULL;
printf("開始分配連結串列個數\n");
for (int i = 0; i < lens; i++)
{
PNODE pNew = (PNODE)malloc(sizeof(ListNode));
if (pNew == NULL)
{
printf("空間分配失敗\n");
exit(-1);
}
printf("請輸入第%d個數據", i + 1);
scanf_s("%d", &data);
pNew->element = data;
pTail->m_pNext = pNew;
pNew->m_pNext=NULL;
pTail = pTail->m_pNext;
}
printf("建立連結串列成功\n");
return pHead;
};
//將兩個連結串列連線在一起
PNODE ADDList(ListNode *head1, ListNode *head2){
ListNode *pr;
pr = head1;
while(pr->m_pNext!=NULL)
{
pr = pr->m_pNext;
}
pr->m_pNext= head2->m_pNext;
/*head2 = NULL;*/
/*printf("輸出合併後的兩個連結串列\n");*/
/*while (pr->m_pNext != NULL)
{
printf("%d", pr->m_pNext->element);
pr = pr->m_pNext;
}*/
return head1;
};
//連結串列查詢
PNODE findList(ListNode *list,int val)
{
PNODE p = list;
if (p->m_pNext == NULL)
{
return;
}
/*int val;*/
int num = 0;
/*printf("輸入要查詢的數:\n");
scanf_s("%d", &val);*/
while(p !=NULL&& p->element!=val)
{
p = p->m_pNext;
num++;
}
if (p != NULL)
{
printf("這個數在第%d個數:", num + 1);
}
else
{
printf("沒有找到這個節點");
}
printf("\n");
}
//連結串列插入
void InsertList(ListNode *list, int pos, int val)
{
int position = 0;
PNODE p = list;
while (p->m_pNext != NULL && position < pos)
{
p = p->m_pNext;
position++;
}
PNODE tmp = (PNODE)malloc(sizeof(PNODE));
tmp->element = val;
tmp->m_pNext = p->m_pNext;
p->m_pNext = tmp;
}
//連結串列刪除
void deleteAllListNode(ListNode *list)
{
PNODE p,tmp;
p = list->m_pNext;
list->m_pNext = NULL;
while (p != NULL)
{
tmp = p->m_pNext;
free(p);//刪除第一個節點
p = tmp;
}
printf("連結串列刪除成功");
}
//刪除指定節點
void deleListNode(ListNode *list, int pos)
{
int position = 0;
PNODE p;
p = list;
while (p != NULL && position < pos-1)
{
p = p->m_pNext;
position++;
}
PNODE tmp = p->m_pNext;
p->m_pNext = tmp->m_pNext;
free(tmp);
tmp = NULL;
}
// 定義連結串列遍歷函式
void TraverseList(ListNode *List) {
PNODE P = List->m_pNext; // 首節點賦值給臨時節點P
printf("遍歷連結串列的值為:");
if (P == NULL)
printf("連結串列為空");
while (P != NULL) //當臨時節點P不為尾節點時,輸出當前節點值
{
printf("%d ", P->element);
P = P->m_pNext;
}
printf("\n");
}
int main() {
int lens = 6;
printf("建立列表1\n");
PNODE list1 = CreateList(lens);
printf("建立列表2\n");
PNODE list2 = CreateList(lens);
int k = 2;
struct ListNode *t;
printf("找到倒數第k個數\n");
t = FindKthToTail(list1, k);
printf("倒數第%d個數是%d\n", k, t->element);
PNODE list;
printf("合併兩個列表\n");
list = ADDList(list1, list2);
printf("遍歷列表\n");
TraverseList(list);
printf("找到第數字為3\n");
findList(list, 3);
printf("在第六個地方插入\n");
InsertList(list, 6, 11111);
TraverseList(list);
printf("在第六個地方刪除\n");
deleListNode(list, 6);
TraverseList(list);
printf("刪除整個列表\n");
deleteAllListNode(list);
return 0;
}