C++ 链表实现
来源:互联网 发布:网上值机软件 编辑:程序博客网 时间:2024/05/22 08:21
#include<iostream>#include<malloc.h>using namespace std;typedef struct node{int date;node* next;}Node;Node* Create(){int i = 0;Node *head,*p,*q;int x=0;head = new Node;q=head;while (1){cout<<"输入数据 :";cin>>x;if (x == 0){break;}p = new Node;p->date = x;q->next=p;q=p;}q->next = NULL;return head;}int length(Node *head){int len =0;Node *p;p = head->next;while (p){++len;p = p->next;}return len;}void print(Node *head){Node *p;if (head->next==NULL){cout<<"链表为空!"<<endl;return;}p=head->next;while (p){cout<<p->date<<"-->";p = p->next;}}Node* search_node_by_number(Node* head,int pos){int len=length(head);if (pos==0){return head;}if (len==0){cout<<"只有头结点"<<endl;return head;}if (pos<0||pos>len){cout<<endl;cout<<"pos大于了链表长度或者 pos是个小于0的数"<<endl;system("pause");exit(-1);}Node *p = head->next;int j=1;while (p!=NULL&&j<pos){p = p->next;j++;}return p;}void search_node_by_value(Node* head,int value){int len = length(head);if (len == 0){cout<<"链表只有头节点"<<endl;return;} int count = 1;Node * p = head->next;while (p!=NULL&&p->date!= value){p=p->next;++count;}if (p==NULL){cout<<"找不到。。啊"<<endl;system("pause");return;}if (p->date== value){cout<<"在第"<<count<<"个位置找到的100"<<endl;return ;}}Node* inset_node(Node* head,int pos,int data){int len = length(head);if (pos<0||pos>len){cout<<endl;cout<<"pos大于了链表长度或者pos是个小于0的数"<<endl;system("pause");exit(-1);}Node *p,*s;p = search_node_by_number(head,pos-1);if (p ==NULL){cout<<"参数错误" <<endl;;system("pause");exit(-1);}else{s = new Node;s->date = data;s->next = p->next;p->next = s;}return head;}Node * delete_node(Node* head,int pos){int len = length(head);if (pos<0||pos>len){cout<<endl;cout<<"pos大于了链表长度或者pos是个小于0的数"<<endl;system("pause");exit(-1);}if (len==0){cout<<"这链表只有头节点"<<endl;return head;}Node *p ,*s;p = search_node_by_number(head,pos-1);if (p!=NULL&&p->next!=NULL){s = p->next;p->next = s->next;delete s;}return head;}Node* RLink(Node* head){Node *p,*q,*r;if (head==NULL||head->next==NULL){return NULL;}p = head->next;q = p->next;p->next = NULL;while (q != NULL){r = q->next;q->next = p;p=q;q=r;}head->next = p;return head;}int main(){Node *head=Create();cout<<" 链表总长度 :"<<length(head)<<endl;print(head);Node* find_value_by_number;find_value_by_number=search_node_by_number(head,5);cout<<endl;cout<<"找到的值为:"<<find_value_by_number->date<<endl;search_node_by_value(head,100);cout<<"插入数据后 :"<<endl;inset_node(head,3,88);print(head);cout<<endl;cout<<"删除数据后:"<<endl;delete_node(head,3);print(head);cout<<endl;cout<<endl<<"反转后的链表";print(head);system("pause");return 0;}
0 0
- C链表实现
- c链表实现
- C/C++/JAVA 双向链表实现
- 【库】c实现链表
- C/C++链表实现
- c语言实现链表
- c的链表实现
- C的链表实现
- C语言实现链表
- 静态链表 C实现
- c链表的实现
- c语言-链表实现
- C语言链表实现。
- C实现链表操作
- 简单链表(C实现)
- 链表堆栈C实现
- C语言实现链表
- C++:单向链表实现
- bencode.BTL.BTFailure: invalid bencoded value (data after valid prefix)
- 我能坚持多久-5月3日
- Java之GUI编程(二)——布局管理器
- python for android : BeautifulSoup 有 bug
- 【DataStructure&AlgorithmInJava】Ch02-PersonArrayDemo
- C++ 链表实现
- JSTL 表达式与 EL 语言
- java常用正则表达式
- R文本情感分析(3)
- linux内存释放
- 开启Android程序时检测网络与GPS是否打开
- Java中IO流知识点总结
- codechef Sums in a Triangle题解
- codechef Turbo Sort 题解