chunlanse2014

导航

数据结构实践---单链表的初始化,建立,插入,查找,删除。

linklist.h

 1 ////////////////////////////////////////////////
 2 /**单链表的初始化,建立,插入,查找,删除*/
 3 ///////////////////////////////////////////////
 4 /*******     linklist.h      **********/
 5 
 6 #ifndef _LINKLIST_H
 7 #define _LINKLIST_H
 8 
 9 
10 #include <cstdio>
11 #include <cstdlib>
12 
13 typedef int ElemType;
14 /////////////////////////////////////////////
15 
16 /**定义结点类型*/
17 typedef struct Node 
18 {
19     ElemType data;        //单链表中的数据域
20     struct Node *next;    //单链表的指针域
21 }Node,*LinkList;
22 
23 /////////////////////////////////////////////
24 /**单链表的初始化*/
25 extern LinkList LinkListInit();
26 /**单链表的建立1,头插法建立单链表*/
27 extern LinkList LinkListCreatH();
28 /**单链表的建立2,尾插法建立单链表*/
29 extern LinkList LinkListCreatT();
30 /**单链表的插入,在链表的第i个位置插入x元素*/
31 extern LinkList LinkListInsert(LinkList L,int i,ElemType x);
32 /**单链表的删除,在链表中删除值为x的元素*/
33 extern LinkList LinkListDelete(LinkList L,ElemType x);
34 /////////////////////////////////////////////////////////////
35 
36 #endif

linklist.cpp

 1 /*********     linklist.cpp   **********/
 2 
 3 #include "linklist.h"
 4 #include <iostream>
 5 using namespace std;
 6 
 7 
 8 ///////////////////////////////////////////////
 9 //单链表的初始化
10 LinkList LinkListInit()
11 {
12     Node *L;
13     L = (Node *)malloc(sizeof(Node));    //申请结点空间
14     if (NULL==L)                    //判断是否有足够的结点空间
15     {
16         cout<<"申请内存空间失败!"<<endl;
17     }
18     L->next = NULL;                    //将next设置为NULL,初始长度为0的单链表
19     return L;
20 }
21 
22 //////////////////////////////////////////////
23 //头插法建立单链表
24 LinkList LinkListCreatH()
25 {
26     Node *L;
27     L = (Node *)malloc(sizeof(Node));    //申请头结点空间
28     L->next=NULL;                        //初始化一个空链表
29 
30     ElemType x;                            //x为链表数据域中的数据
31     while(cin>>x)
32     {
33         Node *p;
34         p=(Node*)malloc(sizeof(Node));    //申请新的结点
35         p->data=x;                        //结点数据域赋值
36         p->next=L->next;                //将结点插入到表头
37         L->next=p;                        //头指针指向新插入的结点形成新链
38     }
39     return L;
40 }
41 
42 ///////////////////////////////////////////////////
43 //尾插法建立单链表
44 LinkList LinkListCreatT()
45 {
46     Node *L;
47     L = (Node *)malloc(sizeof(Node));        //申请头结点空间
48     L->next=NULL;                            //初始化一个空链表
49     Node *r;                                
50     r = L;                                    //r起初指向头结点(存放L的地址),接下来始终指向终端结点
51     ElemType x;
52     while(cin>>x)
53     {
54         Node *p;
55         p = (Node *)malloc(sizeof(Node));        //申请新的结点
56         p->data=x;                                //结点数据域赋值
57         r->next=p;                                //将结点插入到表头
58         r=p;                                    //将r指针指向新插入结点的位置,实现循环插入
59     }
60     r->next=NULL;
61 
62     return L;
63 }
64 
65 ////////////////////////////////////////////////////
66 //单链表的插入
67 LinkList LinkListInsert(LinkList L,int i,ElemType x)
68 {
69     Node *pre;                                    //pre为前驱节点
70     pre = L;                                    //初始指向头结点,从头开始找位置i    
71     int temp = 0;
72     for(temp=1;temp<i;temp++)
73         pre=pre->next;                            //查找第i个位置的前驱结点
74     Node *p;                                    //插入的结点为p
75     p = (Node *)malloc(sizeof(Node));
76     p->data =x;
77     p->next=pre->next;
78     pre->next=p;
79 
80     return L;
81 }
82 
83 ////////////////////////////////////////////////////////
84 //单链表的删除
85 LinkList LinkListDelete(LinkList L,ElemType x)
86 {
87     Node *p,*pre;                    //pre为前驱结点,p为查找的结点
88     p = L;                            //p指向头结点
89     while(p->data!=x)                //查找值为x的元素
90     {
91         pre = p;                    //pre和p总是指向同一个结点                
92         p=p->next;                    //循环查找    
93     }
94     pre->next=p->next;                //删除操作,将其(p)前驱next指向其(p)后继
95     free(p);
96     return L;
97 }

main.cpp

 1 /***********       main.cpp           ***************/
 2 
 3 #include "linklist.h"
 4 #include <iostream>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     LinkList list,start;
10     list=LinkListInit();
11     /*
12     cout<<"请输入单链表的数据"<<endl;
13     list = LinkListCreatH();
14     for (start=list->next;start!=NULL;start=start->next)
15         cout<<start->data<<" ";
16     cout<<endl;
17     */
18 
19     cout<<"请输入单链表的数据"<<endl;
20     list = LinkListCreatT();
21     for (start=list->next;start!=NULL;start=start->next)
22         cout<<start->data<<" ";
23     cout<<endl;
24     
25     int i;
26     ElemType x;
27     cout<<"请输入数据的位置"<<endl;
28     scanf("%d",&i);
29     cout<<"请输入插入数据的值"<<endl;
30     scanf("%d",&x);
31     LinkListInsert(list,i,x);
32     for (start=list->next;start!=NULL;start=start->next)
33         cout<<start->data<<" ";
34     cout<<endl;
35     cout<<"请输入要删除元素的值"<<endl;
36     scanf("%d",&x);
37     LinkListDelete(list,x);
38     for(start=list->next;start!=NULL;start=start->next)
39         cout<<start->data<<" ";
40     cout<<endl;
41 
42     return 0;
43 }

头插法的结果:

 

 

尾插法的结果:

 

posted on 2015-05-19 21:18  chunlanse2014  阅读(2588)  评论(0编辑  收藏  举报