首页 > 技术文章 > C代码实现非循环单链表

makaruila 2015-10-11 16:22 原文

C代码实现非循环单链表,

直接上代码。

  1 # include <stdio.h>
  2 # include <stdlib.h>
  3 # include <malloc.h>
  4 //C代码实现非循环单链表 
  5 
  6 //定义链表结点
  7 typedef struct Node{
  8     int data;//数据域
  9     struct Node* pNext;//指针域 
 10 }* PNODE,NODE; 
 11 
 12 //函数声明
 13  PNODE initHead(void);
 14  void init(PNODE pHead) ;
 15  bool isEmpty(PNODE pHead);
 16  void traverse(PNODE pHead);
 17  int length(PNODE pHead) ;
 18  bool insert(PNODE pHead,int pos,int num);
 19  bool delete_linked(PNODE pHead,int pos);
 20  
 21 int main(void)
 22 {
 23     //确定一个非循环链表只需要一个头结点 
 24     PNODE pHead;
 25     //初始化头结点
 26     pHead = initHead(); 
 27     //给链表赋值
 28     init(pHead);
 29      //遍历
 30     traverse(pHead) ;
 31     printf("\n");
 32     printf("链表的结点为%d个\n",length(pHead));
 33 //    insert(pHead,3,99);
 34     delete_linked(pHead,3);
 35     traverse(pHead) ;
 36     return 0;
 37 }
 38 
 39 //向链表中某个位置增加一个结点 pos>=1
 40 bool insert(PNODE pHead,int pos,int num)
 41 {
 42     int i = 0;
 43     PNODE tem = pHead;
 44     //让tem指向第POS-1个节点
 45     while(NULL!=tem&&i<pos-1) {
 46         tem = tem->pNext;
 47         i++;
 48     }
 49     if(i>pos-1||NULL==tem){
 50         return false;
 51     }
 52     //分配一个新的结点
 53     PNODE pNode = (PNODE)malloc(sizeof(NODE));
 54     if(NULL==pNode){
 55         printf("动态分配内存失败!\n");
 56         exit(-1);
 57     }
 58     pNode->data = num;
 59     //第一种方式
 60 //    pNode->pNext = tem->pNext;
 61 //    tem->pNext = pNode;
 62 //    第二种方式 
 63     PNODE p = tem->pNext;
 64     tem->pNext = pNode;
 65     pNode->pNext = p;
 66     return true;
 67 }
 68 
 69 //删除指定位置的值 
 70 bool delete_linked(PNODE pHead,int pos)
 71 {
 72     int i = 0;
 73     PNODE tem = pHead;
 74     //让tem指向第POS-1个节点
 75     while(NULL!=tem&&i<pos-1) {
 76         tem = tem->pNext;
 77         i++;
 78     }
 79     if(i>pos-1||NULL==tem){
 80         return false;
 81     }
 82     PNODE p = tem->pNext;
 83     tem->pNext = p->pNext;
 84      free(p);
 85     return true;
 86 }
 87     
 88 //链表长度
 89 int length(PNODE pHead) 
 90 {
 91     int count = 0;
 92     PNODE tem = pHead->pNext;
 93     while(NULL!=tem){
 94         count++;
 95         tem = tem->pNext;
 96     }
 97     return count;
 98 }
 99 
100 //初始化头结点
101 PNODE initHead(void)
102 {
103     PNODE pHead = (PNODE)malloc(sizeof(NODE));
104     if(NULL==pHead){
105         printf("申请内存失败\n");
106         exit(-1);
107     }
108     return pHead;
109 }
110 
111 //给链表赋值
112 void init(PNODE pHead) 
113 {
114     int num;//结点的值
115     int len;//结点的个数
116     printf("请输入结点的个数len= ");
117     scanf("%d",&len);
118     int i;
119     PNODE tem = pHead;
120     for(i = 0;i<len;i++){
121         PNODE node = (PNODE)malloc(sizeof(NODE));
122         if(NULL==node){
123             printf("申请内存失败\n");
124             exit(-1);
125         }
126         printf("请输入第%d个节点的值 ",i+1);
127         scanf("%d",&num);
128         node->data = num;
129         tem->pNext = node;
130         node->pNext = NULL;
131         tem = node;
132     }
133 }
134 
135 //判断链表是否为空
136 bool isEmpty(PNODE pHead)
137 {
138     return (NULL==pHead->pNext);
139 }
140 
141 //遍历链表
142 void traverse(PNODE pHead)
143 {
144     if(isEmpty(pHead)){
145         printf("链表为空\n");
146         return;
147     }
148     PNODE tem = pHead->pNext;
149     while(NULL!=tem){
150         printf("%d ",tem->data);
151         tem = tem->pNext;
152     }
153 }
154 
155  

 

推荐阅读