为什么需要指针,不要可以吗
2017-08-09 09:12
253 查看
#include <stdio.h>
void swap(int a ,int b)
{
printf("swap a=%d b=%d\n",a,b);
int t = a;
a = b;
b =t;
printf("swap end a=%d b=%d\n",a,b);
}
int main()
{
int a = 1;
int b = 10;
swap(a,b);
printf("a=%d b=%d\n",a,b);
return 0;
}
root@lqs-ubuntu:/home/lqs/work/datastruct# ./swap
swap a=1 b=10
swap end a=10 b=1
a=1 b=10
#include <stdio.h>
void swap(int *a ,int *b)
{
printf("swap a=%d b=%d\n",*a,*b);
int t = *a;
*a = *b;
*b =t;
printf("end swap a=%d b=%d\n",*a,*b);
}
int main()
{
int a = 1;
int b = 10;
swap(&a,&b);
printf("a=%d b=%d\n",a,b);
return 0;
}
root@lqs-ubuntu:/home/lqs/work/datastruct# ./swap_p
swap a=1 b=10
end swap a=10 b=1
a=10 b=1
root@lqs-ubuntu:/home/lqs/work/datastruct#
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct node
{
DataType data;
struct node *next;
}LinkNode;
LinkNode *create_empty_linklist()
{
LinkNode *head = NULL;
head = (LinkNode *)malloc(sizeof(LinkNode));
head->next = NULL;
return head;
}
int is_empty_linklist(LinkNode *head)
{
return head->next == NULL;
}
int insert_head_linklist(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
temp = (LinkNode *)malloc(sizeof(LinkNode));
temp->data = data;
temp->next = head->next;
head->next = temp;
return 0;
}
int insert_tail_linklist(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
LinkNode *p = head;
temp = (LinkNode *)malloc(sizeof(LinkNode));
temp->data = data;
//找到尾结点
while(p->next)
{
p = p->next;
}
//temp->next = NULL;
temp->next = p->next;
p->next = temp;
return 0;
}
int insert_order_linklist(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
LinkNode *p = head;
temp = (LinkNode *)malloc(sizeof(LinkNode));
temp->data = data;
#if 0
while(p->next)
{
if(p->next->data > data)
{
break;
}
p = p->next;
}
#endif
/*while(p->next != NULL && p->next->data < data)*/
while(p->next && p->next->data < data)
{
p = p->next;
}
temp->next = p->next;
p->next = temp;
return 0;
}
int delete_assign_node(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
LinkNode *p = head;
while(p->next && p->next->data != data)
{
p = p->next;
}
if(p->next == NULL)
{
return -1;
}
temp = p->next;
p->next = p->next->next;
free(temp);
return 0;
}
int print_linklist(LinkNode *head)
{
LinkNode *p = head->next;
while(p)
{
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
return 0;
}
int order_data(LinkNode *head)
{
LinkNode *sa;
LinkNode *sb;
int t ;
for(sa = head->next; sa != NULL ;sa = sa->next)
{
for(sb=sa->next;sb != NULL;sb = sb->next)
{
if(sa->data > sb->data)
{
t = sa->data;
sa->data= sb->data;
sb->data = t;
}
}
}
return 0;
}
int reverse_linklist(LinkNode *head)
{
#if 1
// printf("fun 00\n");
LinkNode *pPrv = head;
LinkNode *Cur = head->next;
LinkNode *pNext = NULL;
head->next = NULL;
while( Cur )
{
pNext = Cur->next;
Cur->next = pPrv->next;
pPrv->next = Cur;
Cur = pNext;
}
return 0;
#endif
#if 0
LinkNode *p = head,*q = NULL;
if(p->next == NULL || p->next->next == NULL)
{
return -1;
}
q = p->next->next;
p->next->next = NULL;
while(q)
{
p = q->next;
q->next = head->next ;
head->next = q;
q = p;
}
return 0;
#endif
}
int main()
{
LinkNode *head = NULL;
int a[] = {1,5,3,4,7,9};
int i = 0;
int ret = 0;
head = create_empty_linklist();
for(i = 0;i < sizeof(a) / sizeof(a[0]);i++)
{
/*insert_head_linklist(head,a[i]);*/
/*insert_tail_linklist(head,a[i]);*/
insert_order_linklist(head,a[i]);
}
print_linklist(head);
#if 0
ret = delete_assign_node(head,5);
if(ret < 0)
{
printf("Assign data %d is not exist.\n",5);
}
print_linklist(head);
#endif
reverse_linklist(head);
#if 0
for(i = 0 ; i < 6 ; i ++)
{
printf("%d ",a[i]);
}
#endif
print_linklist(head);
return 0;
}
root@lqs-ubuntu:/home/lqs/work/datastruct# ./reverse
1 3 4 5 7 9
hello world
9 7 5 4 3 1
root@lqs-ubuntu:/home/lqs/work/datastruct#
void swap(int a ,int b)
{
printf("swap a=%d b=%d\n",a,b);
int t = a;
a = b;
b =t;
printf("swap end a=%d b=%d\n",a,b);
}
int main()
{
int a = 1;
int b = 10;
swap(a,b);
printf("a=%d b=%d\n",a,b);
return 0;
}
root@lqs-ubuntu:/home/lqs/work/datastruct# ./swap
swap a=1 b=10
swap end a=10 b=1
a=1 b=10
#include <stdio.h>
void swap(int *a ,int *b)
{
printf("swap a=%d b=%d\n",*a,*b);
int t = *a;
*a = *b;
*b =t;
printf("end swap a=%d b=%d\n",*a,*b);
}
int main()
{
int a = 1;
int b = 10;
swap(&a,&b);
printf("a=%d b=%d\n",a,b);
return 0;
}
root@lqs-ubuntu:/home/lqs/work/datastruct# ./swap_p
swap a=1 b=10
end swap a=10 b=1
a=10 b=1
root@lqs-ubuntu:/home/lqs/work/datastruct#
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct node
{
DataType data;
struct node *next;
}LinkNode;
LinkNode *create_empty_linklist()
{
LinkNode *head = NULL;
head = (LinkNode *)malloc(sizeof(LinkNode));
head->next = NULL;
return head;
}
int is_empty_linklist(LinkNode *head)
{
return head->next == NULL;
}
int insert_head_linklist(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
temp = (LinkNode *)malloc(sizeof(LinkNode));
temp->data = data;
temp->next = head->next;
head->next = temp;
return 0;
}
int insert_tail_linklist(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
LinkNode *p = head;
temp = (LinkNode *)malloc(sizeof(LinkNode));
temp->data = data;
//找到尾结点
while(p->next)
{
p = p->next;
}
//temp->next = NULL;
temp->next = p->next;
p->next = temp;
return 0;
}
int insert_order_linklist(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
LinkNode *p = head;
temp = (LinkNode *)malloc(sizeof(LinkNode));
temp->data = data;
#if 0
while(p->next)
{
if(p->next->data > data)
{
break;
}
p = p->next;
}
#endif
/*while(p->next != NULL && p->next->data < data)*/
while(p->next && p->next->data < data)
{
p = p->next;
}
temp->next = p->next;
p->next = temp;
return 0;
}
int delete_assign_node(LinkNode *head,DataType data)
{
LinkNode *temp = NULL;
LinkNode *p = head;
while(p->next && p->next->data != data)
{
p = p->next;
}
if(p->next == NULL)
{
return -1;
}
temp = p->next;
p->next = p->next->next;
free(temp);
return 0;
}
int print_linklist(LinkNode *head)
{
LinkNode *p = head->next;
while(p)
{
printf("%d ",p->data);
p = p->next;
}
putchar('\n');
return 0;
}
int order_data(LinkNode *head)
{
LinkNode *sa;
LinkNode *sb;
int t ;
for(sa = head->next; sa != NULL ;sa = sa->next)
{
for(sb=sa->next;sb != NULL;sb = sb->next)
{
if(sa->data > sb->data)
{
t = sa->data;
sa->data= sb->data;
sb->data = t;
}
}
}
return 0;
}
int reverse_linklist(LinkNode *head)
{
#if 1
// printf("fun 00\n");
LinkNode *pPrv = head;
LinkNode *Cur = head->next;
LinkNode *pNext = NULL;
head->next = NULL;
while( Cur )
{
pNext = Cur->next;
Cur->next = pPrv->next;
pPrv->next = Cur;
Cur = pNext;
}
return 0;
#endif
#if 0
LinkNode *p = head,*q = NULL;
if(p->next == NULL || p->next->next == NULL)
{
return -1;
}
q = p->next->next;
p->next->next = NULL;
while(q)
{
p = q->next;
q->next = head->next ;
head->next = q;
q = p;
}
return 0;
#endif
}
int main()
{
LinkNode *head = NULL;
int a[] = {1,5,3,4,7,9};
int i = 0;
int ret = 0;
head = create_empty_linklist();
for(i = 0;i < sizeof(a) / sizeof(a[0]);i++)
{
/*insert_head_linklist(head,a[i]);*/
/*insert_tail_linklist(head,a[i]);*/
insert_order_linklist(head,a[i]);
}
print_linklist(head);
#if 0
ret = delete_assign_node(head,5);
if(ret < 0)
{
printf("Assign data %d is not exist.\n",5);
}
print_linklist(head);
#endif
reverse_linklist(head);
#if 0
for(i = 0 ; i < 6 ; i ++)
{
printf("%d ",a[i]);
}
#endif
print_linklist(head);
return 0;
}
root@lqs-ubuntu:/home/lqs/work/datastruct# ./reverse
1 3 4 5 7 9
hello world
9 7 5 4 3 1
root@lqs-ubuntu:/home/lqs/work/datastruct#
相关文章推荐
- C/C++中,为什么字符串可以赋值给字符指针变量,单引号和双引号的区别
- 为什么static成员的类型可以是类本身?又为什么非static成员被限定声明为其自身类对象的指针或引用?
- 为什么父类指针可以指向子类反之则不行
- 为什么通过空指针(NULL)可以正确调用一些类的成员函数?
- 为什么基类指针(或引用)可以调用派生类的private虚函数
- 为什么基类指针和引用可以指向派生类对象,但是反过来不行?
- 为什么C初始化链表需要二级指针
- 为什么C++中千万不要返回局部对象或变量的引用和指针
- 为什么父类指针可以指向子类反之则不行
- 指针为什么可以当迭代器用
- 为什么整数可以转换为指针
- C++中为什么需要将new对象通过独立语句置入智能指针
- C语言中,为什么字符串可以赋值给字符指针变量
- [疑问]C/C++中为什么在类外利用多态基类指向派生类指针可以调用类的私有成员函数?
- 常量指针,常指针,指针常量,指向常量的指针,为什么常量指针仍可以指向变量
- 为什么通过空指针(NULL)可以正确调用一些类的成员函数?
- 参数需要LPCTSTR为什么可以传入CString
- Command不是也可以对数据库进行insert,update,delete操作吗,为什么还需要DataAdapter。我知道DataAdapter可以用来想AataSet中填充数据,Command不可以吗?
- 为什么需要使用2级指针(转载)
- (编程解决)List和Hashtable都是可以存储数据的,可为什么有时选择List,有时需要Hashtable,这两个有什么区别?