当前位置: 代码迷 >> 综合 >> C双链表练习
  详细解决方案

C双链表练习

热度:20   发布时间:2024-02-09 17:19:51.0
#include<stdlib.h>
#include <stdio.h>
#include<string>
#include<iostream>
using namespace std;
typedef int TYPE;//创建节点;
struct Node
{TYPE ID;char name[20];Node *prev;Node *pnext;
};//创建双向列表;
struct List
{Node*head;Node*end;int length;
};//初始化列表
void List_init(List*list)
{list->head = NULL;list->end = NULL;list->length = 0;
}//创建数据
void List_create(List*list, TYPE number, char name1[20])
{Node*temp = (Node*)malloc(sizeof(Node));temp->ID = number;strcpy(temp->name, name1);temp->prev = NULL;temp->pnext = NULL;if (list->head == NULL){list->head = list->end = temp;list->length += 1;}else                                        //尾插{list->end->pnext = temp;temp->prev = list->end;list->end = temp;list->length++;}}//链表头部输出
void List_out(List*list)
{if (list->head == NULL)printf("链表没有数据!");else{Node*temp = list->head;printf("链表节点个数:%d\n", list->length);printf("链表头节点数据输出:\n");while (temp){printf("-->ID:%d,name:%s\n", temp->ID, temp->name);temp = temp->pnext;}printf("\n\n");}
}//名字查找
void List_look(List*list, char name_look[20])
{int position = 0, toltal = 0;if (list->head == NULL)cout << "链表中无数据!" << endl;cout << "查询的名字:" << name_look << ";" << endl;Node*temp = list->head;while (temp){position++;if (strcmp(name_look, temp->name) == 0){cout << "数据在列表第" << position << "位置" << endl;cout << "名字在列表中的ID:" << temp->ID << endl;toltal++;}temp = temp->pnext;}cout << "数据在列表共存在个数" << toltal << endl;
}
//更改数据
void List_change(List*list, TYPE number, char name_change[20])
{int position = 0, toltal = 0;if (list->head == NULL)cout << "链表中无数据!" << endl;cout << "更改的名字:" << name_change << ";" << endl;Node*temp = list->head;while (temp){position++;if (temp->ID == number){cout << "号码在列表第" << position << "位置" << endl;cout << "号码在列表中对应的名字:" << temp->name << endl;strcpy(temp->name, name_change);cout << "更改后的名字" << temp->name << endl;toltal++;}temp = temp->pnext;}cout << "数据在列表共存在个数" << toltal << endl;
}
//插入节点
int List_insert(List*list, TYPE number, char name_insert[20])
{Node*temp = (Node*)malloc(sizeof(Node));temp->ID = number;strcpy(temp->name, name_insert);temp->prev = NULL;temp->pnext = NULL;if (list->length <= 0)printf("链表没有数据!");else if (number == 0)               //头部插入{temp->pnext = list->head;list->head->prev = temp;list->head = temp;list->length++;return 1;}else if (number == list->length + 1){temp->prev = list->end;list->end->pnext = temp;list->end = temp;list->length++;return 1;}else if (number < list->length + 1 && number>0){int i = 1;Node*node1 = list->head;/*Node*node2 = node1->pnext;*/while (i != number){node1 = node1->pnext;i++;}Node*node2 = node1->pnext;node1->pnext = temp;temp->prev = node1;temp->pnext = node2;node2->prev = temp;list->length++;return 1;}else{cout << "数据插入位置错误!" << endl;}}//删除列表中数据int List_dele(List*list, char name_dele[20])
{int i = 0;Node*p1, *p2;Node*temp = list->head;if (list->head == NULL)cout << "列表无数据" << endl;while (temp){if (strcmp(temp->name, name_dele) == 0){if (temp->pnext==NULL&&temp->prev==NULL){List_init(list);				}else if (temp->prev==NULL&&temp->pnext!=NULL){list->head = temp->pnext;list->head->prev = NULL;				}else if(temp->pnext==NULL&&temp->prev!=NULL){list->end = temp->prev;list->end->pnext = NULL;				}else{p1 = temp->prev;p2 = temp->pnext;p1->pnext = p2;p2->prev = p1;}list->length--;i = 1;}		temp = temp->pnext;}free(temp);if (i == 0)cout << "列表中没有匹配的数据" << endl; 	return i;
}//销毁列表void List_burn(List*list)
{Node*temp = list->end;while (temp){Node*t = temp;temp = temp->prev;free(t);}List_init(list);
}int main()
{TYPE testID = 0;char testname[20] = { NULL };List*test = (List*)malloc(sizeof(List));List_init(test);//初始化List_create(test, 11, "aaa");   //添加数据List_create(test, 22, "bbb");List_create(test, 33, "eee");List_create(test, 44, "ddd");List_create(test, 55, "eee");List_out(test);    //链表输出List_look(test, "ccc");   //链表查看cout << endl;List_change(test, 44, "dddd");   //链表更改cout << endl;List_out(test);cout << endl;if (List_insert(test, 1, "insertname") == 1)List_out(test); //链表插入if (List_dele(test, "eee") >0)List_out(test);      //链表删除cout << endl;cout << endl;List_burn(test);   //链表销毁List_out(test);getchar();getchar();return 0;
}

 

  相关解决方案