You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.5 KiB
153 lines
3.5 KiB
#include<stdio.h>
|
|
|
|
// 定义链表
|
|
struct Node
|
|
{
|
|
int data;
|
|
struct Node* next;
|
|
};
|
|
struct Node* head;
|
|
|
|
// 在链表头节点插入一个值为x的节点
|
|
void Insert(int x){
|
|
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
|
|
temp->data = x;
|
|
temp->next = head;
|
|
// if(head != NULL)temp->next = head;
|
|
head = temp;
|
|
}
|
|
|
|
// 迭代法实现反转列表
|
|
struct Node* Reverse1(struct Node* head){
|
|
struct Node *Current,*Prev,*next;
|
|
Current = head;
|
|
Prev = NULL;
|
|
while(Current != NULL){
|
|
next = Current->next;
|
|
Current->next = Prev;
|
|
Prev = Current;
|
|
Current = next;
|
|
}
|
|
head = Prev;
|
|
return head;
|
|
}
|
|
|
|
// 递归法实现反转列表
|
|
void Reverse2(struct Node* p){
|
|
if(p->next ==NULL){
|
|
head = p;
|
|
return;
|
|
}
|
|
Reverse2(p->next);
|
|
struct Node* q = p->next;
|
|
q->next = p;
|
|
p->next = NULL;
|
|
}
|
|
|
|
// 链表内指定区间反转 调试好了
|
|
struct Node* reverseBetween(struct Node* head, int m, int n ) {
|
|
// write code here
|
|
if(head == NULL)return head;
|
|
if(head->next == NULL)return head;
|
|
if(m == n)return head;
|
|
struct Node *Current,*Prev,*next,*start,*start_last;
|
|
int i;
|
|
Current = head;
|
|
Prev = NULL;
|
|
next = NULL;
|
|
// 先找到开始位置
|
|
for (i=1; i<m; i++) {
|
|
next = Current->next;
|
|
// Current->next = Prev;
|
|
Prev = Current;
|
|
Current = next;
|
|
}
|
|
// 标记
|
|
start_last = Prev;
|
|
start = Current;
|
|
// 反转
|
|
for (i=0; i<(n-m+1); i++) {
|
|
next = Current->next;
|
|
Current->next = Prev;
|
|
Prev = Current;
|
|
Current = next;
|
|
}
|
|
// 头尾节点重指向
|
|
if(start != head){
|
|
start->next = next;
|
|
start_last->next = Prev;//start!=head的情况下,需要保留start上一个指针
|
|
}
|
|
else {
|
|
start->next = next;
|
|
head = Prev;//start==head的情况下,直接将head指向待反转的最后一个
|
|
}
|
|
return head;
|
|
}
|
|
|
|
//打印链表的所有值
|
|
void Print(){
|
|
struct Node* temp = head;
|
|
printf("List is:");
|
|
while(temp){
|
|
printf("%d",temp->data);
|
|
temp = temp->next;
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void Print2(struct Node*p){
|
|
if(p ==NULL){
|
|
printf("\n");
|
|
return;
|
|
}
|
|
// 正序打印
|
|
// printf("%d",p->data);
|
|
// Print2(p->next);
|
|
|
|
// 反转打印
|
|
printf("%d",p->data);
|
|
Print2(p->next);
|
|
}
|
|
|
|
//计算数组的大小,传递数组其实传的地址会改变原来的值
|
|
int RetureLen(int array[], int size){
|
|
array[0] = 0;
|
|
return size;
|
|
}
|
|
|
|
int main(){
|
|
int a;
|
|
char b;
|
|
char str[10] = "zzwxxy";
|
|
char str2[10];
|
|
int array[] = {1,2,3,4,5,6,7,8,9};
|
|
int len = RetureLen(array, sizeof(array) / sizeof(array[0]));
|
|
printf("%d", array[0]);
|
|
|
|
// printf("please input string\n");
|
|
// gets(str2);
|
|
// printf("input string is:\n%s", str2);
|
|
}
|
|
|
|
|
|
/*
|
|
* 定义字符串方式
|
|
char string[] = "zhang";
|
|
char string[] = {'z','h','a','n','g'};
|
|
char str[] = {“zhang”};
|
|
*/
|
|
|
|
/*
|
|
scanf和gets的区别:
|
|
使用方法:scanf("%s", str2); gets(str2);
|
|
scanf :当遇到回车,空格和tab键会自动在字符串后面添加’\0’,但是回车,空格和tab键仍会留在输入的缓冲区中。
|
|
gets:可接受回车键之前输入的所有字符,并用’\0’替代 ‘\n’.回车键不会留在输入缓冲区中
|
|
|
|
printf()和puts()的区别:
|
|
printf("input string is:\n%s", str2); 和 puts(str2);
|
|
*/
|
|
|
|
/*
|
|
计算数组长度:int length = sizeof(array) / sizeof(array[0]);
|
|
如果数组是字符串的话:#include<string.h> int length = strlen(strArray);
|
|
*/
|