链表例题



#include <iostream>
#include <math.h>
#include <stack>
using namespace std;
//删除动态链表中指定的节点
#if 0
class Node
{
public:
 int number;
 Node *pnext;
 Node* creat_list();
 void show_list(Node *h);
 bool delete_list(Node *phead, int pos, int *pval);
 bool insert_list(Node *phead, int pos, int val);
};
Node * Node::creat_list()
{
 Node *head = NULL, *p1, *p2 = NULL;
 p1 = new Node;
 p1->number = 1;
 head = p1;
 p2 = head;
 for (int i = 2; i <= 13; i++)
 {
  p1 = new Node;
  p1->number = i;
  p2->pnext = p1;
  p2 = p1;
 }
 
 p2->pnext = NULL;
 return head;
}
bool Node::delete_list(Node *phead, int pos, int *pval)
{
 int i = 0;
 Node *p = phead;
 while (p->pnext != NULL && i < pos - 1)
 {
  p = p->pnext;
  i++;
 }
 if (i >(pos - 1) || p->pnext == NULL)
 {
  return false;
 }
 Node *q = p->pnext;
 *pval = q->number;
 //删除p节点后面的节点
 p->pnext = p->pnext->pnext;
 free(q);
 q = NULL;
 cout << "the delete value is" << *pval << endl;
 return true;
}
bool Node::insert_list(Node *phead, int pos, int val)
{
 int i = 0;
 Node *p = phead;
 while ((NULL != p) && (i<pos - 1))
 {
  p = p->pnext;
  i++;
 }
 if ((i > pos - 1) || NULL == p)
 {
  return false;
 }
 Node *pnew = new Node;
 pnew->number = val;
 Node *q = p->pnext;
 p->pnext = pnew;
 pnew->pnext = q;
 return true;
}

void Node::show_list(Node *h)
{
 Node*p;
 p = h;
 while (p != NULL)
 {
  cout << p->number << " ";
  p = p->pnext;
 }
 cout << endl;
}
void main()
{
 int val;
 Node *p = NULL;
 p = p->creat_list();
 p->show_list(p);
 p->delete_list(p, 4, &val);
 p->show_list(p);
 p->insert_list(p,6,60);
 p->show_list(p);
}
#endif
//a b 合并 按学好的升序排列
#if 0
class student
{
public:
 long num;
 float score;
 student *next;
 void print( student * head);
 friend  student* sx(student * head);
};
class student a[5] =
{
 { 1005, 10.23, &a[1] },
 { 1004, 10.54, &a[2] },
 { 1003, 11.55, &a[3] },
 { 1002, 12.32, &a[4] },
 { 1001, 15.65, NULL }
};
class student b[5] =
{
 { 2005, 11.23, &b[1] },
 { 2004, 12.54, &b[2] },
 { 2003, 16.55, &b[3] },
 { 2002, 14.32, &b[4] },
 { 2001, 17.65, NULL }
};
void student::print( student * head)//打印链表的函数
{
 student * p;
 p = head;
 if (head == NULL)
 {
  cout << "该链表为空" << endl;
 }
 else{
  do{
   cout << p->num <<" "<<p->score<<endl;
   p = p->next;
  } while (p != NULL);
 }
}
student* sx( student * head)
{    // 用递归,每次找出原链表中学号最小的元素,插入到新链表的后面。
 student *cursor, *first, *prev, *min;
 first = NULL;
 if (head == NULL)
  return NULL;
 for (cursor = min = head; cursor->next != NULL; cursor = cursor->next)
 {
  if ((cursor->next->num) <  (min->num))
  {
   prev = cursor;
   min = cursor->next;
  }
 }
 first = min;
 if (min == head)
 {
  head = head->next;
 }
 else
 {
  prev->next = min->next;
 }
 first->next = sx(head);
 return first;
}
void main()
{
 a[4].next = &b[0];//将两个链表先连接起来
 student * head;
 head = &a[0];
 head->print(head);
 cout << "num 按升序排列之后的输出结果是" << endl;
 head = sx(head);//将经过升序排列之后的节点链表的首地址,赋值给head
 head->print(head);//打印升序之后的链表
}
#endif
// a b 从a 中删除与b 中相同学号的元素
#if 0
class student
{
public:
 long num;
 float score;
 student *pnext;
 void print(student * head);
 friend student *del(student *head1, student *head2); 
};
void student::print(student * head)//打印链表的函数
{
 student * p;
 p = head;
 while (p != NULL)
 {
  cout << p->num << " " << p->score << endl;
  p = p->pnext;
 }
}

student *del(student *head1, student *head2)
{
 student *pa = head1;
 student *pb = head2;
 student *prev = NULL;
 if (pa == NULL)
 {
  cout << "链表A为空" << endl;
  return head1;
 }
 while (pa != NULL)
 {
  if (pa->num == pb->num)
  {
   if (pa == head1)
   {
    head1 = pa->pnext;
   }
   else
   {
    prev->pnext = prev->pnext->pnext;
    pb = pb->pnext;
   }
   return head1;
  }
  else
  {
   prev = pa;
  }
  pa = pa->pnext;
  pb = pb->pnext;
 }
 return head1;
}
class student a[5] =
{
 { 1005, 10.23, &a[1] },
 { 1004, 10.54, &a[2] },
 { 1003, 11.55, &a[3] },
 { 1002, 12.32, &a[4] },
 { 1001, 15.65, NULL }
};
class student b[5] =
{
 { 2005, 11.23, &b[1] },
 { 1004, 12.54, &b[2] },
 { 2003, 16.55, &b[3] },
 { 2002, 14.32, &b[4] },
 { 2001, 17.65, NULL }
};
void main()
{
 student * head;
 head = &a[0];
 head->print(head);
 cout << "b链表的数据为 " << endl;
 head = &b[0];
 head->print(head);
 cout << "删除相同元素之后" << endl;
 head = del(&a[0],&b[0]);
 head->print(head);
}
#endif
//如果链表中输入一个年龄   该链表包含此年龄则删除该节点
#if 0
#include<stdio.h>
class student
{
public:
 int num;
 char name[10];
 int age;
 student *next;
    friend student * del(student *head);
 void print(student * head);
};
void student::print(student * head)//打印链表的函数
{
 student * p;
 p = head;
 while (p != NULL)
 {
  cout << p->num << " " << p->name <<" "<<p->age<< endl;
  p = p->next;
 }
}
student * del(student *head)
{
 int n;
 student *p = head;
 student *prev = NULL;
 printf("请输入年龄:");
 scanf("%d", &n);
 while (p != NULL)
 {
  if (p->age == n)
  {
   head = p->next;
   p = p->next;
   
  }
  else if (p->next->age == n)
  {
   cout << "被删除的数为" << n << endl;
   p->next = p->next->next;
   break;
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个C语言的双向循环链表例题: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; struct Node* prev; }; void insert(struct Node** head, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; if (*head == NULL) { newNode->next = newNode; newNode->prev = newNode; *head = newNode; } else { struct Node* tail = (*head)->prev; newNode->next = *head; (*head)->prev = newNode; newNode->prev = tail; tail->next = newNode; } } void display(struct Node* head) { if (head == NULL) { printf("List is empty.\n"); return; } struct Node* current = head; do { printf("%d ", current->data); current = current->next; } while (current != head); printf("\n"); } int main() { struct Node* head = NULL; // 插入节点 insert(&head, 1); insert(&head, 2); insert(&head, 3); // 显示链表 display(head); return 0; } ``` 这个例题演示了如何创建并打印一个双向循环链表。首先,我们创建了一个结构体 `Node`,其包含数据 `data`、下一个节点指针 `next` 和前一个节点指针 `prev`。然后,我们定义了一个插入函数 `insert`,用于将新节点插入到链表。最后,我们定义了一个显示函数 `display`,用于打印链表的所有节点。在 `main` 函数,我们创建一个空链表,然后插入了三个节点,并使用 `display` 函数打印链表的内容。 这个例题只是对双向循环链表的基本操作进行了演示,你可以根据需要修改和扩展这个代码来实现更复杂的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [链表图解(双向、循环链表+链表增删)](https://blog.csdn.net/qq_52189899/article/details/121552785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值