c语言addtail函数 c语言add

c语言编程

#include stdio.h

成都创新互联-专业网站定制、快速模板网站建设、高性价比韶山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式韶山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖韶山地区。费用合理售后完善,十载实体公司更值得信赖。

#include stdlib.h

#include malloc.h

#include string.h

struct Student

{

char name[20];

int num;

int math;

struct Student *next;

};

struct Student *Create(struct Student *head); /*生成数据链表*/

void saveList(struct Student *head); /*保存链表数据*/

void ShowList(struct Student *head); /*显示链表*/

void AddTail(struct Student *head,struct Student *temp); /*表尾添加元素(*/

struct Student *AddHead(struct Student *head,struct Student *temp); /*表头添加元素*/

struct Student *DeleteSomeone(struct Student *head); //删除元素

void FindSomeone(struct Student *head); //查询元素

void FindMax(struct Student *head,); /*查找并输出 math最大节点的信息*/

void FindMin(struct Student *head,); /*查找并输出 math最小节点的信息 */

void Average(struct Student *head,); /*统计链表节点总数及 math的平均 */

void main()

{

struct Student *head=NULL,*temp=NULL;

int choice;

do

{

printf("1:创建数据链表\n");

printf("2:添加链表单元\n");

printf("3:删除链表单元\n");

printf("4:查询链表单元\n");

printf("5:退出\n");

printf("请输入功能选择:");

scanf("%d",choice);

if(choice==1)

{

head=Create(temp);

ShowList(head);

saveList(head);

}

else if(choice==2)//表头添加元素

{

temp=(struct Student*)malloc(sizeof(struct Student));

printf("姓名:"); scanf("%s",temp-name);

printf("学号:"); scanf("%d",temp-num);

printf("成绩:"); scanf("%d",temp-math);

head=AddHead(head,temp);// head=AddTail(head,temp);

ShowList(head);

saveList(head);

}

else if(choice==3) //删除元素

{

head=DeleteSomeone(head);

ShowList(head);

saveList(head);

}

else if(choice==4) //查找元素

FindSomeone(head);

else if(choice==5)

printf("谢谢使用!\n");

else

printf("非法输入,请输入1~5之间的数!\n");

}while(choice!=5);

}

struct Student *Create(struct Student *head) //不包含打开文件

{

struct Student *pS,*pEnd;

while(1)

{

pS=(struct Student*)malloc(sizeof(struct Student));

printf("姓名:");

scanf("%s",pS-name);

if(strcmp(pS-name,"000"))

{

printf("学号:");

scanf("%d",pS-num);

printf("成绩:");

scanf("%d",pS-math);

}

else

break;

if(head==NULL)

head=pS;

else

pEnd-next=pS;

pEnd=pS;

}

pEnd-next=NULL;

delete pS;

return (head);

}

void ShowList(struct Student *head)//显示链表上的数据

{

printf("学生姓名,成绩如下:\n");

while(head)

{

printf("%20s",head-name);

printf("%6d",head-num);

printf("%3d\n",head-math);

head=head-next;

}

}

void saveList(struct Student *head)//保存链表上的数据

{

FILE *fp;

fp=fopen("e:\\list.txt","w");

if(fp==NULL)

{

printf("无法创建文件e:\\list.txt!");

return;

}

while(head)

{

fprintf(fp,"%s %d %d\n",head-name,head-num,head-math);

head=head-next;

}

fclose(fp);

}

void AddTail(struct Student *head,struct Student *temp)//加在表尾

{

struct Student *last;

while(head)

{

last=head;

head=head-next;

}

last-next=temp;

temp-next=NULL;

}

struct Student *AddHead(struct Student *head,struct Student *temp)//加在表头

{

struct Student *oldhead;

oldhead=head;

head=temp;

head-next=oldhead;

return head;

}

struct Student *DeleteSomeone(struct Student *head)

{

struct Student *temp;

int xnum,flag=0;

printf("请输入学号:");

scanf("%d",xnum);

if(head==NULL)

{

printf("表为空!")

return head;

}

if(head-num==xnum)

{

temp=head;

head=head-next;

delete temp;

return head;

}

struct Student *first=head;

while(head)

{

if(head-next==NULL)

break;

if(head-next-num==xnum)

{

temp=head-next;

head-next=temp-next;

delete temp;

flag=1;

break;

}

head=head-next;

}

if(flag==0)

printf("未发现!")

return first;

}

void FindSomeone(struct Student *head)//按学号查找

{

int xnum,flag=0;

printf("请输入学号:");

scanf("%d",xnum);

while(head)

{

if(head-num==xnum)

{

printf("%20s",head-name);

printf("%6s",head-num);

printf("%3d",head-math);

flag=1;

break;

}

head=head-next;

}

if(flag==0)

printf("未发现!");

}

C语言生产者消费者进程代码问题

实现一个队列CQueue CQueue提供两个公有成员函数 addTail():往队列尾部增加一个元素 removeHead():读出并移除队列的第一个元素 生产者:两个线程通过调用CQueue::addTail()往队列中增加元素 消费者:一个线程通过调用CQueue::removeHead()从队列中读取元素 #include iostream #include list #include windows.h #include process.h using namespace std; #define P(sem) WaitForSingleObject(sem,INFINITE) #define V(sem) ReleaseSemaphore(sem,1,NULL) class CQueue { public: void addTail();//往队列尾部增加一个元素 void removeHead();//读出并移除队列的第一个元素 private: listint L; }; CQueue buffer;//全局的缓冲区 const int buf_size = 10;//缓冲区大小 static int GOODS_ID = 0;//商品序号 const int producers = 3;//生产者数量 const int consumers = 8;//消费者数量 void ProducerThread(void* param); void ConsumerThread(void* param); HANDLE empty,occupy,op_mutex; int main() { int i; int p_id[producers],c_id[consumers];

c语言单链表问题,请问这个那里错了,本人菜鸟,下面是程序

不知道你有没有调这个程序,很明显的错误是调用SearchX(H);和InsertList(H);这2个函数时,参数少了,你的实现里SearchX有2个参数,InsertList有3个参数,那你调用肯定要传递足够的参数,其实也容易理解:你查找时(即Search时)传递一个链表头,自然也需要一个查找的数据;插入时(即insert时)需要一个表头,一个数据和一个插入位置。

之前写的一个程序,带测试信息,给你参考:我这里是用的全局节点变量,所有在插入时只传了2个参数,一个target目标(不是位置,换成指定位置更简单),一个数据:

#include stdio.h

#include stdlib.h

typedef struct LinkNode

{

int data;

struct LinkNode *next;

}link,*plink;

plink head = NULL;

void addfront()

{

int value;

plink s,p;

printf("please enter the value\n");

scanf("%d",value);

s = malloc(sizeof(link));

if(s==NULL)

{

printf("memory error\n");

return;

}

s-data = value;

if(head == NULL)

{

head = s;

s-next = NULL;

}else{

p = head;

head = s;

s-next = p;

}

}

void addtail()

{

int value;

plink s,p;

s = malloc(sizeof(link));

if(s==NULL)

{

printf("memory error\n");

return;

}

printf("please enter the value\n");

scanf("%d",value);

s-data = value;

if(head == NULL)

{

head = s;

s-next = NULL;

}else{

p = head;

while(p-next != NULL)

{

p = p-next;

}

p-next = s;

s-next = NULL;

}

}

void addAll()

{

int count,i;

printf("please enter the count\n");

scanf("%d",count);

for(i=0;icount;i++)

{

addtail();

}

}

void display()

{

plink p;

p = head;

while(p)

{

printf("%3d",p-data);

p = p-next;

}

printf("\n");

}

plink search(int data)

{

int i = 0;

plink p;

if(head == NULL)

{

printf("single link is empty\n");

return NULL;

}

p = head;

while(p != NULL)

{

i++;

if(p-data == data)

{

printf("the number %d is at %d\n",data,i);

return p;

}else

{

p = p-next;

}

}

return NULL;

}

void insert(int target,int insertvalue)

{

plink p, q, s;

if(head == NULL)

{

printf("single link is empty\n");

return ;

}

s = (plink)malloc(sizeof(link));

if(s == NULL)

{

printf("memory error!\n");

return ;

}

s-data = insertvalue;

if(head-data == target)

{

s-next = head;

head = s;

return;

}else

{

p = q = head;

while(p != NULL)

{

if(p-data == target)

{

q-next = s;

s-next = p;

printf("insert successful\n");

return ;

}else

{

q = p;

p = p-next;

}

}

}

printf("insert failed!\n");

}

void delete(int data)

{

plink p, q;

if(head == NULL)

{

printf("single link is empty\n");

return ;

}

if(head-data == data)

{

p = head-next;

free(head);

head = p;

printf("delete successful\n");

return;

}else

{

p = q = head;

while(p != NULL)

{

if(p-data == data)

{

q-next = p-next;

free(p);

printf("delete successful\n");

return ;

}else

{

q = p;

p = p-next;

}

}

}

printf("delete failed!\n");

}

void deleteAll(int data)

{

plink p, q;

if(head == NULL)

{

printf("single link is empty\n");

return ;

}

p = q = head;

while(p != NULL)

{

if(head-data == data)

{

p = head-next;

free(head);

head = p;

}else if(p-data == data)

{

q-next = p-next;

free(p);

p = q-next;

}else

{

q = p;

p = p-next;

}

}

}

int main(void)

{

int operation;

printf("please enter your operation\n");

do{

printf("\t1.add a number at the first\n");

printf("\t2.add a number at the last\n");

printf("\t3.add multi number\n");

printf("\t4.display the singlelink\n");

printf("\t5.search a number\n");

printf("\t6.insert the insertvalue at the target\n");

printf("\t7.delete the value\n");

printf("\t8.delete all of the value\n");

printf("\t9.quit\n");

scanf("%d",operation);

switch(operation)

{

case 1:addfront();break;

case 2:addtail();break;

case 3:addAll();break;

case 4:display();break;

case 5:

{int value;

printf("enter the search number\n");

scanf("%d",value);

search(value);break;}

case 6:

{int target,insertvalue;

printf("enter the target and insertvalue \n");

scanf("%d%d",target,insertvalue);

insert(target,insertvalue);}

break;

case 7:

{int value;

printf("enter the value to delete \n");

scanf("%d",value);

delete(value);}

break;

case 8:

{int value;

printf("enter the value to delete all \n");

scanf("%d",value);

deleteAll(value);}

break;

case 9:return;

default:printf("error input\n");

}

}while(1);

}

C语言处理中文文本的问题,利用队列

用C++实现了一个,但是代码很乱,惨不忍睹,LZ勉强看一下吧……====================================#includestdio.h

#include

string.hconst

int

MAX_QUEUE_SIZE

=

1024;

int

g_nCount

=

0; //当前队列总数

int

g_nCurIndex

=

0; //当前出队列索引

char

*g_pStrQueue[MAX_QUEUE_SIZE]

=

{0};//入队列

void

AddTail(char

*pszText)

{

if

(g_nCount

==

MAX_QUEUE_SIZE)

{

printf("队列已满!\n");

return;

} int

nStrLen

=

strlen(pszText);

//如果所添加的字符串都是空格则不入队列

bool

fFind

=

false;

for

(int

i

=

0;

i

nStrLen;

i

++)

{

if

(pszText[i]

!=

'

')

{

fFind

=

true;

break;

}

}

if

(!fFind)

{

return;

} g_pStrQueue[g_nCount]

=

new

char[nStrLen

+

1]; if

(g_pStrQueue[g_nCount])

{

strcpy(g_pStrQueue[g_nCount],

pszText);

g_nCount++;

}

else

{

printf("内存申请失败!\n");

}

}//出队列

void

GetHead(char

*pszText)

{

if

(g_nCurIndex

==

g_nCount)

{

printf("队列为空!\n");

return;

}

strcpy(pszText,

g_pStrQueue[g_nCurIndex]);

delete

[]

g_pStrQueue[g_nCurIndex];

g_pStrQueue[g_nCurIndex]

=

NULL;

g_nCurIndex++;

}//取队列当前元素个数

int

GetQueueSize()

{

return

g_nCount

-

g_nCurIndex;

}//清空队列

void

ClearQueue()

{

for

(int

i

=

0;

i

MAX_QUEUE_SIZE;

i

++)

{

if

(g_pStrQueue[i])

{

delete

[]

g_pStrQueue[i];

g_pStrQueue[i]

=

NULL;

}

}

g_nCurIndex

=

0;

g_nCount

=

0;

}void

main()

{

const

int

nMaxFileLen

=

102400;

int

nFileLen

=

0;

char

*pszText

=

new

char[nMaxFileLen];

FILE

*pFile

=

fopen("a.txt",

"r"); ClearQueue(); if

(!pFile)

{

printf("文件打开失败!\n");

return;

}

if

(!pszText)

{

printf("内存申请失败!");

return;

}

memset(pszText,

0,

nMaxFileLen); nFileLen

=

fread(pszText,

1,

nMaxFileLen,

pFile);

fclose(pFile); pFile

=

fopen("output.txt",

"w");

if

(!pFile)

{

printf("文件打开失败!\n");

return;

} char

*nPos1

=

NULL,

*nPos2

=

NULL;

char

szBuff[1024];

nPos1

=

strstr(pszText,

"[");

nPos2

=

pszText; while

(nPos1

!=

NULL)

{

char

*pTmpPos1,

*pTmpPos2;

memset(szBuff,

0,

1024);

memcpy(szBuff,

nPos2,

nPos1

-

nPos2);

AddTail(szBuff);

//[*]左边

nPos2

=

strstr(nPos1,

"]");

if

(nPos2

==

NULL)

{

break;

} memset(szBuff,

0,

1024);

memcpy(szBuff,

nPos1,

nPos2

-

nPos1

+

1);

pTmpPos1

=

strstr(nPos2,

szBuff);

//寻找[*]

if

(pTmpPos1

==

NULL)

{

break;

} pTmpPos1

+=

strlen(szBuff);

pTmpPos2

=

strstr(pTmpPos1,

"\n");

if

(pTmpPos2

==

NULL)

{

pTmpPos2

=

pszText

+

strlen(pszText);

} memset(szBuff,

0,

1024);

memcpy(szBuff,

pTmpPos1,

pTmpPos2

-

pTmpPos1);

AddTail(szBuff);

//[*]右边

nPos2++; nPos1

=

strstr(nPos2,

"[");

char

*pTemp

=

strstr(pszText,

"\n");

if

(nPos1

pTemp)

{

break;

}

}

int

nQueueCount

=

GetQueueSize();

for

(int

i

=

0;

i

nQueueCount;

i+=2)

{

char

szStr[1024]

=

{0};

memset(szBuff,

0,

1024);

GetHead(szBuff);

strcat(szStr,

szBuff);

strcat(szStr,

"

");

memset(szBuff,

0,

1024);

GetHead(szBuff);

strcat(szStr,

szBuff);

strcat(szStr,

"\r\n");

fwrite(szStr,

1,

strlen(szStr),

pFile);

printf(szStr);

}

fclose(pFile);

}


网站标题:c语言addtail函数 c语言add
标题网址:http://ybzwz.com/article/ddsedsi.html