C语言数据结构算法

Python011

C语言数据结构算法,第1张

#include <stdio.h>

#include <stdlib.h>

struct node()//节点

{

int data

node *next

}

main()

{

unsigned n //结点个数

node *head, *p, *q, *tail

scanf("%u", &n)

head=(node *)malloc(6) //分配6b给head

p=head

tail=head

for(int i=1i<=ni++) //输入各节点数据

{

tail=(node *)malloc(6)//分配6b给tail

p->next=tail //接通p与tail

p=tail

scanf("%i", &p->data)//输入

}

p=head->next

q=p

for(int i=1i<=ni++) //输入各节点数据

{

printf("%i , ", &p->data)//输出

p=p->next

free(q) //释放

q=p

}

}

调试过的

没什么大的毛病

#include <stdio.h>

#include <stdlib.h>

#include <iostream>

typedef int ElemType

typedef struct LNode {

ElemType date

struct LNode *next

}linklist,*link

/*构造链表*//////////////////////////////////////

void IinitList(link &L)

{

if(L)delete L

L= (link)malloc(sizeof(LNode))

if (!L) exit(1)

L->next=NULL

cout<<"链表已经建立\n"

}

//////////////////////////////////////////////////////

// /*删除结点*/// //////////////////////////////////////////////

int listdelete(link &L,int i,ElemType &e)

{

link p,qint j

p=Lj=0

while(p->next&&j<i-1)

{

p=p->next++j

}

q=p->next

p->next=q->next

e=q->datefree(q)

cout<<"链表已经删除\n"

return 1

}

////////////////////////////////////////////// /////////

// /*插入结点*/////////////// ///////////////////////

int listinsert(link &L,int i,ElemType e)

{

link p,q

int j

p=Lj=0

while(p&&j<i-1)

{

p=p->next++j

}

q= (link)malloc(sizeof(LNode))

q->date=e

q->next=p->next

p->next=qcout<<"链表已经插入\n"

return 1

}

/////////////////////////////////////////////////////

////*显示数据*///////// ////////////////////////////////

void show(link l)

{ link pint j

p=lj=0

cout<<"链表的值为:\n"

while(p->next)

{

cout<<p->next->date<<endl

p=p->next

}

}

//////////////////////// /////////////////////////////////

//////销毁链表////// ////////////////////////////////////////

void destorylinst(link &L)

{

while(L)

{ link p=L

L=L->next

free(p)

}

L=NULL

}

////// 打印表头///////////////////////////////////////

void print()

{

cout<<"------------------------\n"

cout<<"------------------------\n"

}

////////////////////////////////////////////////////////

///////////////////////////////////////////////////////

////查找结点//// ////////////////////////////////////////

void lookfor(link l,int e)

{

if(l==NULL)

cout<<"链表未建立,请先构造链表\n"

else{

link pint i=0,j=0

p=l->next

cout<<"你查找值的位置是:\n "

while(p)

{ if(p->date==e)

{ j++

cout<<i+1<<endl

}

p=p->nexti++

}cout<<"查找完毕\n"

if(j==0)

cout<<"你查找的值不在链表中 、\n"

} }

void putline(link &l)

{

if(l==NULL ||l->next==NULL )

cout<<"链表未建立或是空的,请先构造链表\n"

else{

link p,q

p=l->next

while(p!=NULL)

{

q=p->next

while(q!=NULL)

{

if(p->date>q->date)

{ ElemType t

t=p->date

p->date=q->date

q->date=t

}

q=q->next

}

p=p->next

} cout<<"链表已经排序 \n"

}

}

/////////////////////////////// //////////////////

///////////////////////////////////////////////////

//////测试函数///// /////////////////////

void main()

{ link L=NULLint k

while(1)

{

cout<<"按0退出\n"<<"按1建立\n"<<"按2插入\n"<<"按3删除\n"

<<"按4清空链表\n"<<"按5查找\n"<<"按6进行排续\n"

print()

int a,i,j

cin>>a

switch(a)

{ case 0: if(L!=NULL)

destorylinst(L)

exit(1)

case 1:

IinitList(L)

k=0

print()

show(L)

cout<<"空的链表\n"

cout<<"链表长度为: "<<k<<endl

print()

cout<<"是否要给链表插入值:y----n\n"

char yy

yy=getchar()

if(yy=='y')

{

cout<<"请输入值!按回车键后输入下一个,输入0再按回车结束\n"

int bb

cin>>bb

while(bb!=0)

{ k++

listinsert(L,k,bb)

cin>>bb

}

print()

show(L) cout<<"链表长度为: "<<k<<endl

}

else break

print()

break

case 2:

if(L!=NULL)

{

cout<<"输入位置:\n"

cin>>i

while(i>k+1 || i<1)

{

cout<<"位置错误,重新输入插入位置\n"

cin>>i

}

cout<<"输入植;\n"

cin>>j

listinsert(L,i,j)

k++

print()

show(L)

cout<<"链表长度为:"<<k<<endl

print()

}

else

{ cout<<"链表不存在,请先建链表\n"

print()}

break

case 3:

if(L!=NULL)

{

cout<<"输入位置:\n"

cin>>i

while(i>k || i<1)

{

cout<<"位置错误,重新输入删除位置\n"

cin>>i

}

listdelete (L,i,j)

cout<<"你删除的是:\n"

cout<<j<<endl

k--print()

show(L)

cout<<"链表长度为:"<<k<<endl

print()

}

else {

cout<<"链表不存在,请先建链表\n"

print()

}

break

case 4:

destorylinst(L)

cout<<"链表已经清空\n"

print()

break

case 5:

print()

cout<<"输入要查找的值;\n"

int z

cin>>z

lookfor(L,z)

print()

break

case 6:

putline(L)

if(L!=NULL)

show(L)

print()

break

default:

break

}

}

delete L

}

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

typedef struct list

{

char ch

struct list *next

}List

List *create_list(List *data)

List *insert_list(List *head, List *data)

void print_list(List *head)

void get_list_length(List *head)

void insert_third_withf(List *head)

void delete_first_value(List *head)

void delete_within_A_Z(List *head)

int main()

{

List *head = NULL

List tmp

memset(&tmp, 0x00, sizeof(tmp))

head = create_list(&tmp)

tmp.ch = 'a'

insert_list(head, &tmp)

tmp.ch = 'b'

insert_list(head, &tmp)

tmp.ch = 'A'

insert_list(head, &tmp)

tmp.ch = 'c'

insert_list(head, &tmp)

tmp.ch = 'D'

insert_list(head, &tmp)

tmp.ch = 'd'

insert_list(head, &tmp)

tmp.ch = 'F'

insert_list(head, &tmp)

tmp.ch = 'e'

insert_list(head, &tmp)

tmp.ch = 'h'

insert_list(head, &tmp)

printf("显示\n")

print_list(head)

get_list_length(head)

printf("第三个位置插入f\n")

insert_third_withf(head)

print_list(head)

printf("删除第一个元素\n")

delete_first_value(head)

print_list(head)

printf("删除A-Z\n")

delete_within_A_Z(head)

print_list(head)

return 0

}

List *create_list(List *data)

{

List *newnode = NULL

newnode = (List*)malloc(sizeof(List))

if(NULL == newnode)

{

printf("malloc failed !\n")

return NULL

}

*newnode = *data

newnode->next = NULL

return newnode

}

List *insert_list(List *head, List *data)

{

List *newnode = NULL

if(NULL == head)

{

printf("list null !\n")

return NULL

}

newnode = create_list(data)

while(head->next != NULL)

{

head = head->next

}

head->next = newnode

newnode->next = NULL

return newnode

}

void print_list(List *head)

{

List *tmp = NULL

if(NULL == head)

{

printf("print head null !\n")

return

}

tmp = head

tmp = tmp->next

while(tmp != NULL)

{

printf("ch = %c\n", tmp->ch)

tmp = tmp->next

}

}

void get_list_length(List *head)

{

List *tmp = NULL

int length = 0

if(NULL == head)

{

printf("get_len_list head null !\n")

return

}

tmp = head

while(tmp != NULL)

{

length = length + 1

tmp = tmp->next

}

printf("the list length(包括头结点) = %d\n", length)

}

void insert_third_withf(List *head)

{

List *tmp = NULL

List *value = NULL

List val

int i = 1

if(NULL == head || NULL == head->next)

{

printf("insert_third_...head null !\n")

return

}

memset(&val, 0x00, sizeof(val))

head = head->next

tmp = head

while(tmp != NULL)

{

i = i + 1

if(3 == i)

{

val.ch = 'f'

value = create_list(&val)

value->next = tmp->next

tmp->next = value

break

}

tmp = tmp->next

}

if(i <3)

{

printf("the list is too short !\n")

}

}

void delete_first_value(List *head)

{

List *tmp = NULL

if(NULL == head || NULL == head->next || NULL == head->next->next)

{

printf("delete_fir_val head null !\n")

return

}

tmp = head->next

head->next = head->next->next

free(tmp)

}

void delete_within_A_Z(List *head)

{

List *tmp = NULL

if(NULL == head || head->next == NULL)

{

printf("head null[dele_A_Z] !\n")

return

}

tmp = head

tmp->next = head->next

while(tmp->next != NULL)

{

if(tmp->next->ch >64 &&tmp->next->ch <91)

{

if(NULL != tmp->next->next)

{

free(tmp->next)

tmp->next = tmp->next->next

}

else

{

tmp->next = NULL

break

}

}

tmp = tmp->next

}

}

//楼主只能帮你到这了