标签:地址 -- 输入数据 ret type logs oid nod 需要
#ifndef _NODE #define _NODE typedef struct Node *PtrNode; typedef PtrNode List; typedef PtrNode Position; typedef int ElementType; List InitNode(); List Find(int i, List L); void InSert(ElementType x, int i, List L); void Delete(int i, List L); int PrintNode(List L); List CreateNode(); #endif struct Node { ElementType Data; List Next; }; #include<stdio.h> #include<stdlib.h> #include"Node.h" List InitNode() { //初始化头结点 List L; L = (List)malloc(sizeof(struct Node)); L->Next = NULL; printf("初始化成功\n"); return L; } List Find(int j, List L) { //查找第几处元素的地址 int i = 1; Position P=L->Next; //指向第一个元素 while (P!= NULL&&i < j) { P = P->Next; i++; } if (i == j) return P; else return NULL; } void InSert(ElementType x, int i, List L) { //在第几处插入数据 Position P; List s; if (i == 1) { //插在第一个元素时 s = (List)malloc(sizeof(struct Node)); s->Data = x; //L是头结点 s->Next = L->Next; L->Next = s; return L; } else { P = Find(i - 1, L); //插在其他地方 s = (List)malloc(sizeof(struct Node)); s->Data = x; s->Next = P->Next; P->Next = s; return L; } } void Delete(int i, List L) { //删除第几处的元素 Position s,P; if (i == 1) { //删除第一个元素时 s = L->Next; L->Next = s->Next; free(s); return L; } P = Find(i - 1, L); if (P->Next == NULL) { printf("第%d个结点不存在", i); return NULL; } else { //删除其他处 s = P->Next; P->Next = s->Next; free(s); } } int PrintNode(List L) { //遍历输出 L = L->Next; while (L) { printf("%d\n", L->Data); L = L->Next; } } List CreateNode() { //创建链表 int N,x; Position P,s,r; P = InitNode(); r = P; //r指向最后一个元素 printf("输入要创建几个数据"); scanf("%d", &N); while (N--) { printf("输入数据"); scanf("%d", &x); s = (List)malloc(sizeof(struct Node)); //依次向后插入 s->Data = x; s->Next = r->Next; r->Next = s; r = r->Next; r = s; } return P; } int main() { int a, b,c; List list; list=CreateNode(); printf("输入要插入的数据"); scanf("%d", &a); printf("输入在第几处插入"); scanf("%d", &b); InSert(a, b, list); PrintNode(list); printf("需要删除第几处的数据"); scanf("%d", &c); Delete(c, list); PrintNode(list); }
标签:地址 -- 输入数据 ret type logs oid nod 需要
原文地址:http://www.cnblogs.com/yudongxuan/p/7645020.html