- // ConsoleApplication1.cpp : 定义控制台应用程序的入口点.
- //
- #include "stdafx.h"
- #include"iostream"
- using namespace std;
- typedef int data;
- typedef struct node_list
- {
- data info;;
- node_list *next;
- }node;
- node *init();
- node *insert_head(node *head, data x);
- node *insert_rear(node *head, data x);
- node *insert_x(node *head, data x, data y);
- node *del_x(node *head, data x);
- int print(node *p);
- int _tmain(int argc, _TCHAR* argv[])
- {
- node *p = init();
- p=insert_head(p,1);
- for (int i = 0; i <10;i++)
- p = insert_rear(p, i);
- p=insert_x(p, 5, 1000);
- print(p);
- p = del_x(p,1000);
- print(p);
- return 0;
- }
- node *init()
- {
- return NULL;
- }
- node *insert_head(node *head,data x)
- {
- head = (node*)malloc(sizeof(node));
- head->info = x;
- head->next=NULL;
- return head;
- }
- node *insert_rear(node *head, data x)
- {
- node *q = head;
- while (q->next!= NULL)
- q = q->next;
- node *p = (node *)malloc(sizeof(node));
- q->next = p;
- q->next->info = x;
- p->next = NULL;// 注意初始化
- return head;
- }
- int print(node *p)
- {
- if (p == NULL)
- cout <<"It is empty!" << endl;
- else
- while (p!=NULL)
- {
- cout << p->info <<endl;
- p = p->next;
- }
- return 0;
- }
- node *insert_x(node *head, data x,data y)
- {
- node *p = head;
- node *q = (node *)malloc(sizeof(node));
- q->info = y;
- while (p->info != x)
- p = p->next;
- q->next = p->next;
- p->next = q;
- return head;
- }
- node *del_x(node *head, data x)
- {
- node *q = head;
- while (q->next->info != x)
- q = q->next;
- q->next = q->next->next;
- return head;
- }
指针没有初始化, 找了半天问题, MD!
来源: http://www.bubuko.com/infodetail-3325993.html