20.5.9(C語言)鏈表創(chuàng)建,輸出,結(jié)點地址取值,值取結(jié)點地址,插入新值,刪除值

//(C語言)鏈表創(chuàng)建,輸出,結(jié)點地址取值,值取結(jié)點地址,插入新值,刪除值

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define error 0

typedef struct Node

{

? ? int data;

? ? struct Node *next;

} LNode, *LinkList;

LinkList CreateList_R(LNode *L, int n)

{

? ? int i;

? ? LNode *p, *r;

? ? L->next = NULL; //建立一個帶頭結(jié)點的空鏈表

? ? r = L;? ? ? ? ? //尾指針r指向頭結(jié)點

? ? for (i = 0; i < n; i++)

? ? {

? ? ? ? p = (LNode *)malloc(sizeof(LNode));

? ? ? ? printf("請輸入第%d個數(shù)", i + 1);

? ? ? ? scanf("%d", &p->data);

? ? ? ? p->next = NULL; //建立一個帶頭結(jié)點的空鏈表

? ? ? ? r->next = p;

? ? ? ? r = p;

? ? }

? ? r->next = L;

? ? return L;

}

void print(LNode *L)

{

? ? LNode *r;

? ? r = L;

? ? while (r->next != L) //r所指節(jié)點不能為空

? ? {

? ? ? ? r = r->next;? ? ? ? ? ? //r后移指向下一個節(jié)點

? ? ? ? printf("%d ", r->data); //輸出r所指的節(jié)點的值

? ? }

? ? printf("\n");

}

LinkList GetElem(LNode *L, int i, int e)

{

? ? int j = 1;

? ? LNode *p;

? ? p = L->next;

? ? while (p->next != L && j < i)

? ? {

? ? ? ? p = p->next;

? ? ? ? j++;

? ? }

? ? if (!p || j > i)

? ? {

? ? ? ? printf("error");

? ? ? ? return error;

? ? }

? ? e = p->data;

? ? printf("輸出要找的數(shù)第%d個數(shù):%d\n", i, e);

? ? return 0;

}

LinkList LocateElem(LNode *L, int e)

{

? ? LNode *p;

? ? int i = 1;

? ? p = L->next;

? ? while (p && p->data != e)

? ? {

? ? ? ? p = p->next;

? ? ? ? i++;

? ? }

? ? printf("要查找的數(shù)據(jù)的結(jié)點地址為:%d\n", i);

? ? return p;

}

LinkList ListInsert(LNode *L, int i, int e)

{

? ? LNode *p;

? ? int j = 0;

? ? p = L;

? ? while (p && (j < i - 1))

? ? {

? ? ? ? p = p->next;

? ? ? ? j++;

? ? }

? ? if (!p || j > i - 1)

? ? {

? ? ? ? return error;

? ? }

? ? LNode *s;

? ? s = (LNode *)malloc(sizeof(LNode));

? ? s->data = e;

? ? s->next = p->next;

? ? p->next = s;

? ? printf("輸出插入數(shù)后的新鏈表:");

? ? LNode *r;

? ? r = L;

? ? while (r->next != L) //r所指節(jié)點不能為空

? ? {

? ? ? ? r = r->next;? ? ? ? ? ? //r后移指向下一個節(jié)點

? ? ? ? printf("%d ", r->data); //輸出r所指的節(jié)點的值

? ? }

? ? printf("\n");

? ? return 0;

}

LinkList ListDelete(LNode *L, int i)

{

? ? LNode *p, *q;

? ? p = L;

? ? int j = 0;

? ? while ((p->next) && (j < i - 1))

? ? {

? ? ? ? p = p->next;

? ? ? ? j++;

? ? }

? ? q = p->next;

? ? p->next = q->next;

? ? free(q);

? ? printf("輸出刪除數(shù)后的新鏈表:");

? ? LNode *r;

? ? r = L;

? ? while (r->next != L) //r所指節(jié)點不能為空

? ? {

? ? ? ? r = r->next;? ? ? ? ? ? //r后移指向下一個節(jié)點

? ? ? ? printf("%d ", r->data); //輸出r所指的節(jié)點的值

? ? }

? ? printf("\n");

? ? return 0;

}

int main()

{

? ? LNode *A;

? ? int length_A, i, e;

? ? A = (LNode *)malloc(sizeof(LNode));

? ? printf("請輸入需要輸入的數(shù)據(jù)個數(shù):");

? ? scanf("%d", &length_A);

? ? CreateList_R(A, length_A);

? ? printf("輸出剛才輸入的數(shù)據(jù):");

? ? print(A);

? ? printf("請輸入需要取的數(shù)據(jù)序號:");

? ? scanf("%d", &i);

? ? GetElem(A, i, e);

? ? printf("請輸入需要取的數(shù)據(jù)值:");

? ? scanf("%d", &e);

? ? LocateElem(A, e);

? ? printf("輸入需要插入的數(shù)據(jù)的位置和值(空格隔開):");

? ? scanf("%d %d", &i, &e);

? ? ListInsert(A, i, e);

? ? printf("輸入需要刪除的數(shù)據(jù)的位置:");

? ? scanf("%d", &i);

? ? ListDelete(A, i);

? ? return 0;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容