当前位置: 首页 > news >正文

揭阳网站制作个人在线做网站免费

揭阳网站制作,个人在线做网站免费,浅谈旅游网站的规划与建设,网页设计与动态网站开发目录 链表基本操作 删除重复元素 查找倒数第N个节点 查找中间节点 约瑟夫环 循环链表 合并有序链表 逆置链表 逆置链表(双向链表) 链表基本操作 //linklist.c#include "linklist.h" #include <stdlib.h>struct node *head NULL; struct node *tail…

目录

链表基本操作

删除重复元素 

查找倒数第N个节点

查找中间节点

约瑟夫环

循环链表

合并有序链表

逆置链表

逆置链表(双向链表)


链表基本操作

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned char elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;
}void insert_node(int pos, unsigned char elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == NULL)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == NULL)tail = pre;free(p);}
}void print_linklist(void)
{struct node *p;for(p = head; p; p = p->next)printf("%c", p->elem);printf("\n");
}int search(unsigned char elem)
{struct node *p;for(p = head; p; p = p->next)if(p->elem == elem)return 1;return 0;
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>struct node
{unsigned char elem;struct node *next;
};void create_list(unsigned char elem);
void insert_node(int pos, unsigned char elem);
void delete_node(int pos);
void print_linklist(void);
int search(unsigned char elem);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{create_list('A');create_list('B');create_list('C');create_list('D');print_linklist();delete_node(0);print_linklist();insert_node(0, 'F');insert_node(2, 'Z');print_linklist();if(search('C'))printf("the elem is found in the linklist\n");elseprintf("Can not find it\n");return 0;
}

删除重复元素 

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == NULL)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == NULL)tail = pre;free(p);}
}void print_linklist(struct node *linklist_head)
{struct node *p;for(p = linklist_head; p; p = p->next)printf("%5d", p->elem);printf("\n");
}int search(unsigned int elem)
{struct node *p;for(p = head; p; p = p->next)if(p->elem == elem)return 1;return 0;
}void delete_repeat(struct node *head)
{int flag[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};struct node *p = head;struct node *q = NULL;flag[p->elem] = 1;while(p->next != NULL){if(flag[p->next->elem] == 0){flag[p->next->elem] = 1;p = p->next;}else{q = p->next;p->next = q->next;free(q);}}
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>extern struct node *head;
extern struct node *tail;struct node
{unsigned int elem;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(struct node *linklist_head);
int search(unsigned int elem);
void delete_repeat(struct node *head);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{create_list(1);create_list(2);create_list(8);create_list(2);create_list(3);create_list(9);create_list(4);create_list(6);create_list(4);create_list(8);create_list(7);create_list(5);create_list(2);create_list(9);create_list(6);print_linklist(head);delete_repeat(head);print_linklist(head);return 0;
}

查找倒数第N个节点

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == NULL)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == NULL)tail = pre;free(p);}
}void print_linklist(struct node *linklist_head)
{struct node *p;for(p = linklist_head; p; p = p->next)printf("%5d", p->elem);printf("\n");
}int search(unsigned int elem)
{struct node *p;for(p = head; p; p = p->next)if(p->elem == elem)return 1;return 0;
}int find_mid(struct node *linklist_head)
{struct node *p;struct node *q;p = q = linklist_head;while(p != NULL && p->next != NULL){p = p->next->next;q = q->next;}return q->elem;
}int find_last_nth(struct node *linklist_head, int n)
{int i;struct node *p;struct node *q;p = q = linklist_head;for(i = 0; i < n-1; i++)p = p->next;while(p->next != NULL){p = p->next;q = q->next;}return q->elem;
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>extern struct node *head;
extern struct node *tail;struct node
{unsigned int elem;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(struct node *linklist_head);
int search(unsigned int elem);
int find_mid(struct node *linklist_head);
int find_last_nth(struct node *linklist_head, int n);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{int n;create_list(1);create_list(2);create_list(3);create_list(4);create_list(5);create_list(6);create_list(7);create_list(8);create_list(9);create_list(10);print_linklist(head);printf("Please enter the last one you want to show:");scanf("%d", &n);printf("the last n :%d\n", find_last_nth(head, n));return 0;
}

查找中间节点

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == NULL)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == NULL)tail = pre;free(p);}
}void print_linklist(struct node *linklist_head)
{struct node *p;for(p = linklist_head; p; p = p->next)printf("%5d", p->elem);printf("\n");
}int search(unsigned int elem)
{struct node *p;for(p = head; p; p = p->next)if(p->elem == elem)return 1;return 0;
}int find_mid(struct node *linklist_head)
{struct node *p;struct node *q;p = q = linklist_head;while(p != NULL && p->next != NULL){p = p->next->next;q = q->next;}return q->elem;
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>extern struct node *head;
extern struct node *tail;struct node
{unsigned int elem;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(struct node *linklist_head);
int search(unsigned int elem);
int find_mid(struct node *linklist_head);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{create_list(1);create_list(2);create_list(3);create_list(4);create_list(5);create_list(6);create_list(7);create_list(8);create_list(9);create_list(10);print_linklist(head);printf("mid = %d\n", find_mid(head));return 0;
}

约瑟夫环

约瑟夫问题是个著名的问题:N个人围成一圈,第一个人从1开始报数,报M的将被杀掉,下一个人接着从1开始报。如此反复,最后剩下一个,求最后的胜利者。
例如只有三个人,把他们叫做A、B、C,他们围成一圈,从A开始报数,假设报2的人被杀掉。

●首先A开始报数,他报1。侥幸逃过一劫。
●然后轮到B报数,他报2。非常惨,他被杀了
●C接着从1开始报数
●接着轮到A报数,他报2。也被杀死了。
●最终胜利者是C

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;tail->next = head;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;tail->next = head;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == head)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);tail->next = head;}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == head)tail = pre;free(p);}
}void print_linklist(void)
{struct node *p;p = head;//	for(p = head; p != head; p = p->next)
//		printf("%d", p->elem);do{printf("%5d", p->elem);p = p->next;}while(p != head);printf("\n");
}int search(unsigned int elem)
{struct node *p;p = head;//	for(p = head; p; p = p->next)
//		if(p->elem == elem)
//			return 1;do{if(p->elem == elem)return 1;p = p->next;}while(p != head);return 0;
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>extern struct node *head;
extern struct node *tail;struct node
{unsigned int elem;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(void);
int search(unsigned int elem);#endif
//main.c#include <stdio.h>
#include "linklist.h"
#include <stdlib.h>int main(void)
{int n, k, m;int i;struct node *p, *q;printf("Please enter the number of person:");scanf("%d", &n);for(i = 1; i <= n; i++)create_list(i);print_linklist();p = head;printf("Please enter the start num:");scanf("%d", &k);while(--k)p = p->next;
//	printf("p->elem = %d\n", p->elem);printf("Please enter the m:");scanf("%d", &m);if(1 == m){for(i = 0; i < n; i++){printf("%3d", p->elem);p = p->next;}printf("\n");}else{while(n--){for(i = 1; i < m - 1; i++)p = p->next;q = p;p = p->next;printf("%3d", p->elem);q->next = p->next;free(p);p = p->next;}printf("\n");}return 0;
}

循环链表

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;tail->next = head;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;tail->next = head;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == head)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);tail->next = head;}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == head)tail = pre;free(p);}
}void print_linklist(void)
{struct node *p;p = head;//	for(p = head; p != head; p = p->next)
//		printf("%d", p->elem);do{printf("%5d", p->elem);p = p->next;}while(p != head);printf("\n");
}int search(unsigned int elem)
{struct node *p;p = head;//	for(p = head; p; p = p->next)
//		if(p->elem == elem)
//			return 1;do{if(p->elem == elem)return 1;p = p->next;}while(p != head);return 0;
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>struct node
{unsigned int elem;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(void);
int search(unsigned int elem);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{create_list(1);create_list(2);create_list(3);create_list(4);create_list(5);create_list(6);create_list(7);print_linklist();insert_node(0, 11);print_linklist();delete_node(6);print_linklist();delete_node(0);print_linklist();delete_node(0);print_linklist();delete_node(4);print_linklist();insert_node(0, 8);print_linklist();insert_node(5, 9);print_linklist();insert_node(2, 13);print_linklist();return 0;
}

合并有序链表

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == NULL)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == NULL)tail = pre;free(p);}
}void print_linklist(struct node *linklist_head)
{struct node *p;for(p = linklist_head; p; p = p->next)printf("%5d", p->elem);printf("\n");
}int search(unsigned int elem)
{struct node *p;for(p = head; p; p = p->next)if(p->elem == elem)return 1;return 0;
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>extern struct node *head;
extern struct node *tail;struct node
{unsigned int elem;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(struct node *linklist_head);
int search(unsigned int elem);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{struct node *head1 = NULL;struct node *head2 = NULL;struct node *p = NULL;  //head1struct node *q = NULL;	//head2create_list(1);create_list(9);create_list(13);create_list(27);head1 = head;print_linklist(head1);head = NULL;create_list(3);create_list(5);create_list(14);create_list(81);create_list(88);create_list(95);create_list(99);head2 = head;print_linklist(head2);head = NULL;p = head1;q = head2;while(p && q){if(p->elem <= q->elem){if(head == NULL)head = p;elsetail->next = p;tail = p;p = p->next;}else{if(head == NULL)head = q;elsetail->next = q;tail = q;q = q->next;}}tail->next = p?p:q;print_linklist(head);return 0;
}

逆置链表

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->next = NULL;if(head == NULL)head = p;elsetail->next = p;tail = p;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head = p;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->next = pre->next;pre->next = p;if(p->next == NULL)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;free(pre);}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next == NULL)tail = pre;free(p);}
}void print_linklist(struct node *linklist_head)
{struct node *p;for(p = linklist_head; p; p = p->next)printf("%5d", p->elem);printf("\n");
}int search(unsigned int elem)
{struct node *p;for(p = head; p; p = p->next)if(p->elem == elem)return 1;return 0;
}int find_mid(struct node *linklist_head)
{struct node *p;struct node *q;p = q = linklist_head;while(p != NULL && p->next != NULL){p = p->next->next;q = q->next;}return q->elem;
}int find_last_nth(struct node *linklist_head, int n)
{int i;struct node *p;struct node *q;p = q = linklist_head;for(i = 0; i < n-1; i++)p = p->next;while(p->next != NULL){p = p->next;q = q->next;}return q->elem;
}void reverse_linklist(struct node *linklist_head)
{struct node *p, *n;p = linklist_head->next;linklist_head->next = NULL;while(p->next != NULL){n = p->next;p->next = linklist_head;linklist_head = p;p = n;}p->next = linklist_head;linklist_head = p;head = linklist_head;
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>extern struct node *head;
extern struct node *tail;struct node
{unsigned int elem;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(struct node *linklist_head);
int search(unsigned int elem);
int find_mid(struct node *linklist_head);
int find_last_nth(struct node *linklist_head, int n);
void reverse_linklist(struct node *linklist_head);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{int n;create_list(1);create_list(2);create_list(3);create_list(4);create_list(5);create_list(6);create_list(7);create_list(8);create_list(9);create_list(10);print_linklist(head);reverse_linklist(head);print_linklist(head);return 0;
}

逆置链表(双向链表)

//linklist.c#include "linklist.h"
#include <stdlib.h>struct node *head = NULL;
struct node *tail = NULL;void create_list(unsigned int elem)
{struct node *p = (struct node *)malloc(sizeof(struct node));p->elem = elem;p->pre = NULL;p->next = NULL;if(head == NULL)head = p;else{tail->next = p;p->pre = tail;}tail = p;
}void insert_node(int pos, unsigned int elem)
{struct node *pre;pre = head;int i = 0;struct node *p = (struct node *)malloc(sizeof(struct node));if(pos == 0){p->elem = elem;p->next = head;head->pre = p;p->pre = NULL;head = p;}else{while(i < pos - 1){pre = pre->next;i++;}p->elem = elem;p->pre = pre;p->next = pre->next;if(p->next != NULL)pre->next->pre = p; pre->next = p;if(p->next == NULL)tail = p;}
}void delete_node(int pos)
{struct node *pre, *p;pre = head;int i = 0;if(pos == 0){head = head->next;head->pre = NULL;free(pre);}else{while(i < pos - 1){pre = pre->next;i++;}p = pre->next;pre->next = p->next;if(p->next != NULL)p->next->pre = pre;else//if(p->next == NULL)tail = pre;free(p);}
}void print_linklist(void)
{struct node *p;for(p = head; p; p = p->next)printf("%5d", p->elem);printf("\n");
}int search(unsigned int elem)
{struct node *p;for(p = head; p; p = p->next)if(p->elem == elem)return 1;return 0;
}void reverse_print_linklist(void)
{struct node *p;for(p = tail; p; p = p->pre)printf("%5d", p->elem);printf("\n");
}
//linklist.h#ifndef LINKLIST_H__
#define LINKLIST_H__#include <stdio.h>struct node
{unsigned int elem;struct node *pre;struct node *next;
};void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(void);
int search(unsigned int elem);
void reverse_print_linklist(void);#endif
//main.c#include <stdio.h>
#include "linklist.h"int main(void)
{create_list(1);create_list(2);create_list(3);create_list(4);create_list(5);create_list(6);create_list(7);print_linklist();reverse_print_linklist();
/*insert_node(0, 11);print_linklist();delete_node(6);print_linklist();delete_node(0);print_linklist();delete_node(0);print_linklist();delete_node(4);print_linklist();insert_node(0, 8);print_linklist();insert_node(5, 9);print_linklist();insert_node(2, 13);print_linklist();
*/	return 0;
}

http://www.hengruixuexiao.com/news/10015.html

相关文章:

  • 吉林市哪有做网站的做直销去哪里找客户
  • 苏州相城区做网站公司西安网络优化培训机构公司
  • 茂名专业网站建设如何制作网页广告
  • 创造自己的网站舆情网站
  • 橙光音乐一家做音乐的网站企业网站排名优化方案
  • 网站制作客户资料网站收录检测
  • 做网站失败宁波网站推广专业服务
  • 大连手机自适应网站建设价格迅速上排名网站优化
  • 协会网站建设方案开发客户的70个渠道
  • 亚马逊网站开发使用的什么方式百度统计数据
  • wordpress修改永久链接后无法访问使用最佳搜索引擎优化工具
  • 陕西省住房和城乡建设厅网站首页长沙百度关键词排名
  • 网站开发需要学什么百度推广登录后台
  • 小型网站开发济南网站制作平台
  • 网站首图怎么做网络营销总结
  • 百度网站托管安全优化大师下载
  • 温州做网站制作哪家好百度扫一扫
  • 网站平台建设方案策划书seo怎么做?
  • 网页设计个人网站设计seo外包公司费用
  • 成都网站开发等项目外包公司百度地图导航2022最新版
  • 网站开发配置表格百度广告公司联系方式
  • wordpress双语网站信息流推广渠道
  • 中小型网站建设方案什么网站百度收录快
  • 做门户网站用什么台州seo排名扣费
  • 淄博企业网站建设有限公司百度知道合伙人官网登录入口
  • 网站制作计划打开2345网址大全
  • 自己做网站可以赚钱吗深度优化
  • 柏枫谈做网站都需要学什么百度安装到桌面
  • 无锡网站建设选千客云网络电商营销推广有哪些?
  • 网站开发与服务器交互营业推广怎么写