求C语言单链表 源代码
Elemtype data;
struct node *next;
} ListNode, *ListPtr;
typedef struct stuInfo {
char stuName[10]; /*学生姓名*/
int Age /*年龄*/
} ElemType
实现带头结点的单向链表的创建、删除链表、插入结点等操作,并能实现年龄递增的两个单向链表合并一个链表,合并后的链表按年龄递减,可认为同名同年龄是同一个学生,每个学生在合并后的链表中仅出现一次。最后打印输出合并后的链表元素,验证结果的正确性。 展开
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct people
{
char name[10];
int age;
struct people * next;
};
int main()
{
struct people * head=NULL;
struct people * prev , * current;
int flag=1;
while(flag!=0)
{
printf("请输入学生姓名,年龄:(年龄输入0结束所有输入工作)\n");
current=(struct people *)malloc(sizeof(struct people));
if(head==NULL)
head=current;
else
prev->next=current;
current->next=NULL;
scanf("%s",¤t->name);
scanf("%d",¤t->age);
prev=current;
flag=current->age;
}
printf("Output:\n");
if(head==NULL)
printf("无资料。\n");
else
{
current=head;
while(current->next!=NULL)
{
printf("姓名:%s\n年龄:%d\n\n",current->name,current->age);
current=current->next;
}
}
}
至于排序,断开旧链表,将前后指针链接到新的节点就好
如果还有问题欢迎再问哈
推荐于2016-10-18
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
using namespace std;
int pow(int i,int j)
{
int sum=1;
for(int k=1; k<=j; k++)
{
sum*=i;
}
return sum;
}
int jinzhi_16(char *number)
{
char *p=number;
int sum=0;
int len=strlen(number);
int i=0;
int lone;
while(*p)
{
if(toascii(*p)>=65&&toascii(*p)<=70)
{
lone=toascii(*p)-55;
}
else
{
lone=*p-'0';
}
sum+=pow(16,len-i-1)*lone;
i++;
*p++;
}
return sum;
}
int jinzhi_1_10(char *number,int N)
{
char *p=number;
int sum=0;
int len=strlen(number);
int i=0;
int lone;
while(*p)
{
lone=*p-'0';
sum+=pow(N,len-i-1)*lone;
i++;
*p++;
}
return sum;
}
int is_reverse(char *number)
{
int len=strlen(number);
char *save_number=(char *)malloc(sizeof(char) *(strlen(number)));
strcpy(save_number,number);
reverse(save_number,save_number+len-1);
if(strcmp(save_number,number)==0)
{
return true;
}
}
int return_is_16(char *number)
{
char *p=number;
while(*p)
{
if(toascii(*p)>=65&&toascii(*p)<=91)
{
return true;
}
*p++;
}
}
int main()
{
int N;
cin>>N;
getchar();
char *number=(char *)malloc(sizeof(char) * 32);
gets(number);
int num;
if(N==16)
{
num=jinzhi_16(number);
}
else
{
num=jinzhi_1_10(number,N);
}
int count_step=0;
cout<<"STEP="<<count_step;
return 0;
}