链表模板!

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 
 5 using namespace std;
 6 
 7 struct List
 8 {
 9     int val;
10     List *next;
11 };
12 
13 List *head;
14 
15 void Insert(int k,int val)
16 {
17     List *p,*q;
18     p=head;
19     q=(List *)malloc(sizeof(List));
20     for(int i=0;i<k;i++)
21         p=p->next;
22     q->val=val;
23     q->next=p->next;
24     p->next=q;
25 }
26 
27 void Delete(int k)
28 {
29     List *p,*q;
30     p=head;
31     for(int i=0;i<k-1;i++)
32         p=p->next;
33     q=p->next;
34     p->next=q->next;
35     free(q);
36 
37 }
38 int main()
39 {
40 
41     head=(List *)malloc(sizeof(List));
42     head->next=NULL;
43     return 0;
44 }
View Code

 

posted on 2014-10-08 21:11  M.D.LUFFI  阅读(113)  评论(0编辑  收藏  举报

导航