今天学了线性表,花了一天时间看顺序表和链表,顺序表有些看懂了。而单链表感觉有些复杂,勉强写了一个赋值1-9的程序,感觉还是理解不透彻。
/********************************************************** File Name: Author: xxx Date:2016-12-03 Description: 创建单链表并赋值,要求遍历就能输出1-9 Fuction List: ************************************************************/ #include <stdio.h> #include <stdlib.h> #define ok 0 #define error -1 #define malloc_error -2 typedef int Elementtype; typedef struct node { Elementtype data; // 结点的数据 struct node *next; // 结点指针 }Node; typedef Node* PNode; // 重命名结点指针类型 // 头插法创建链表 int Creatlist_head(PNode *phead,Elementtype data) { // 创建一个新的结点 PNode p = (PNode)malloc(sizeof(Node)/sizeof(char)); if (p == NULL) { return malloc_error; } // 将新数据赋给新结点 p->data = data; p->next = *phead; *phead = p; } // 打印 void Deplay(PNode head) { if(head == NULL) { return; } PNode temp = head; while(temp) { printf("M",temp->data); temp = temp->next; } printf("\n"); } int main() { PNode head = NULL; int i; for (i = 0; i < 10; i++) { // 头插法创建链表 Creatlist_head(&head,i); } Deplay(head); return 0; }
