单链表

本文详细介绍了链表的基本操作,包括创建、打印、反转及释放内存等,并提供了完整的C++实现代码。通过具体实例展示了如何利用链表进行数据存储与管理。

 

 

  1 #include <iostream>
  2 #include <string>
  3 #include <stdarg.h>
  4 #include <cstdio>
  5 #include <assert.h>
  6 #include <cstring>
  7 #include <error.h>
  8 #include <unistd.h>
  9 #include <sys/types.h>
 10 #include <signal.h>
 11 #include <pthread.h>
 12 #include <stdio.h>
 13 #include <unistd.h>
 14 #include <stdlib.h>
 15 #include <pthread.h>
 16 #include <semaphore.h>
 17 
 18 using namespace std;
 19 typedef int ElemType;
 20 typedef struct Node{
 21     Node *next;
 22     ElemType data;
 23 }STNode;
 24 bool ismalloc_suc(Node *p){
 25     bool flag;
 26     if(NULL == p)
 27     {
 28         flag = false;
 29         perror("malloc failed!");
 30         exit(1);
 31     }
 32     else {
 33         flag = true;
 34     }
 35     return flag;
 36 }
 37 
 38 //the head set null,usefull to insert ,dele 尾插法
 39 Node *create_node()
 40 {
 41     Node *head;
 42     head = NULL;
 43     head = (Node *)malloc(sizeof(Node));
 44     assert(ismalloc_suc(head));
 45     head->next = NULL;
 46     Node *p1;
 47     p1=NULL;
 48 
 49     p1 = head;
 50 
 51     int x;
 52     //tail insert
 53     while (scanf("%d",&x) != EOF) {
 54         Node *p2;
 55         p2=(Node *)malloc(sizeof(Node));
 56         //assert(ismalloc_suc(head));
 57         assert(ismalloc_suc(p2));
 58         p2->data = x;
 59         p1->next = p2;
 60         p1 = p2;
 61     }
 62 
 63     p1->next = NULL;
 64     cout << "insert sqlist succeed!" << endl;
 65     return head;
 66 }
 67 
 68 void print_node(Node *head)
 69 {
 70     if(NULL == head->next)
 71     {
 72         cout << "the sqlist is empty!";
 73     }
 74     else {
 75         while (NULL != head->next) {
 76             cout << head->next->data<<" ";
 77             head = head->next;
 78         }
 79         cout << '\n';
 80     }
 81 }
 82 
 83 int free_node(Node *head)
 84 {
 85     Node *p;
 86     while (head) {
 87         p = head->next;
 88         free(head);
 89         head = p;
 90     }
 91     return 0;
 92     //    if(head == NULL) return 0;
 93     //    Node *p;
 94     //    p = head->next;
 95     //    while (p != NULL) {
 96     //        head->next = p->next;
 97     //        free(p);
 98     //        p = head->next;
 99 
100     //    }
101     //    free(head);
102     //    head = NULL;
103     return 0;
104 }
105 
106 Node *reverse_node(Node *head)
107 {
108     if(NULL == head || NULL == head->next)
109         return head;
110     Node *p;
111     p = head->next;
112 
113     Node *q = p->next;
114     p->next = NULL;
115 
116     Node *tmp;
117     while (q != NULL) {
118         tmp = q->next;
119         q->next = p;
120         p = q;
121         q = tmp;
122     }
123     head->next = p;
124     return head;
125 }
126 //只遍历一次,求出中间节点
127 ElemType getMidElem(Node *head){
128     Node *p;
129     Node *q;
130     if(NULL == head || NULL == head->next)
131         return 0;
132 
133     p = head->next;
134     q = head->next->next;
135     while (p != NULL && q != NULL) {
136         if(q->next == NULL) break;
137         p = p->next;
138         q = q->next->next;
139     }
140     return p->data;
141 }
142 
143 int main() {
144     cout << sizeof(STNode) << endl;
145 //    STNode n;
146 //    printf("%#x %#x\n", &n.data, &n.next);//注意定义时顺序的异同
147     freopen("../tmp", "r", stdin);
148     cout<<"input a sqlist"<<endl;
149     Node *head = create_node();
150     print_node(head);
151 
152     cout << "after reverse!\n";
153     Node *rhead= reverse_node(head);
154     print_node(rhead);
155     free_node(head);
156     exit(EXIT_SUCCESS);
157 }

 

 



 

转载于:https://www.cnblogs.com/guxuanqing/p/4921985.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值