Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
单链表
#include <stdio.h> #include <stdlib.h> #define null 0 typedef struct Lnode { int data; struct Lnode *next; } Lnode, *LinkList; //初始化链表 bool InitList(LinkList &L ) { L = (Lnode *)malloc(sizeof(Lnode)); if (L == null) return false; L -> next = null; return true; } //按位序在链表中插入 bool ListInsert(LinkList &L, int i, int e) { if (i < 1) return false; Lnode *p; int j = 0; p = L; while (p != null && j < i - 1) { p = p->next; j++; } Lnode *q = (Lnode *)malloc(sizeof(Lnode)); q -> data = e; q ->next = p -> next; p -> next = q; free(p); return true; } //指定结点后插操作 bool ListNextInsert(Lnode *p, int e) { if (p == null) return false; Lnode *q = (Lnode *)malloc(sizeof(Lnode)); q ->data = e; q ->next = p ->next; p ->next = q; free(q); return true; } //指定结点的前插操作 bool ListPriorInsert(Lnode *p, int e) { if (p == null ) return false; Lnode *q = (Lnode *)malloc(sizeof(Lnode)); q -> next = p -> next; q ->data = p ->data; p ->next = q; p ->data = e; return true; } //按位序在链表中删除元素 bool ListDelete(LinkList L, int i, int &e) { if (L == null ) return false; Lnode *p = (Lnode *)malloc(sizeof(Lnode)); p = L; int j = 0; while (p != null && j < i - 1) { p = p -> next; j ++; } Lnode *q = p ->next; e = q -> data; p -> next = q ->next; return true; } //指定结点后删除元素 bool ListNextDelete(Lnode *p, int &e) { if (p == null) return false; Lnode *q = p -> next; e = q ->data; p -> next = q ->next; free(q); return true; } //指定结点前删除元素 bool ListPriorDelete(Lnode *p, int &e) { if (p == null ) return false; Lnode *q = p ->next; p ->data = q ->data; p ->next = q -> next; free(q); return true; } //头插法建立单链表 LinkList List_HeadList(LinkList &L) { int x; L = (Lnode *)malloc(sizeof(Lnode)); L -> next = null; scanf("%d", &x); while (x != 8989) { Lnode *q = (Lnode *)malloc(sizeof(Lnode)); q -> data = x; q ->next = L -> next ; L -> next = q; scanf("%d", &x); } return L; } //尾插法建立单链表 LinkList List_TailList(LinkList &L) { int x; L = (Lnode *)malloc(sizeof(Lnode)); Lnode *r = L; scanf("%d", &x); while (x != 999) { Lnode *q = (Lnode *)malloc(sizeof(Lnode)); q -> data = x; r -> next = q; r = q; scanf("%d", &x); } r -> next = null; return L; } //计算单链表的长度 int Length(LinkList L) { int len = 0; if ( L == null) return 0; while (L -> next != null) { L = L -> next; len ++; } return len; } int main() { LinkList L; InitList(L); List_TailList(L); printf("%d", Length(L)); return 0; }
run
|
edit
|
history
|
help
0
Linq FirstOrDefault() null check in c#
Problem_binary
30272 Program Ex1
Create RFReader HardwareConfig
Generics Base class constraints Doubts
Palindrome String
Arrays Solved
SHA Tester 2.1
asxasxd
sdfrgthyju8765rb dcd vxs dd